Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Format.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Locale/Data.php';
27 
36 {
37  const STANDARD = 'auto';
38 
39  private static $_options = array('date_format' => null,
40  'number_format' => null,
41  'format_type' => 'iso',
42  'fix_date' => false,
43  'locale' => null,
44  'cache' => null,
45  'disableCache' => null,
46  'precision' => null);
47 
64  public static function setOptions(array $options = array())
65  {
66  self::$_options = self::_checkOptions($options) + self::$_options;
67  return self::$_options;
68  }
69 
79  private static function _checkOptions(array $options = array())
80  {
81  if (count($options) == 0) {
82  return self::$_options;
83  }
84  foreach ($options as $name => $value) {
85  $name = strtolower($name);
86  if ($name !== 'locale') {
87  if (gettype($value) === 'string') {
88  $value = strtolower($value);
89  }
90  }
91 
92  switch($name) {
93  case 'number_format' :
95  $locale = self::$_options['locale'];
96  if (isset($options['locale'])) {
97  $locale = $options['locale'];
98  }
99  $options['number_format'] = Zend_Locale_Data::getContent($locale, 'decimalnumber');
100  } else if ((gettype($value) !== 'string') and ($value !== NULL)) {
101  #require_once 'Zend/Locale/Exception.php';
102  $stringValue = (string)(is_array($value) ? implode(' ', $value) : $value);
103  throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. "
104  . "Format '$stringValue' must be a valid number format string.");
105  }
106  break;
107 
108  case 'date_format' :
110  $locale = self::$_options['locale'];
111  if (isset($options['locale'])) {
112  $locale = $options['locale'];
113  }
114  $options['date_format'] = Zend_Locale_Format::getDateFormat($locale);
115  } else if ((gettype($value) !== 'string') and ($value !== NULL)) {
116  #require_once 'Zend/Locale/Exception.php';
117  $stringValue = (string)(is_array($value) ? implode(' ', $value) : $value);
118  throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. "
119  . "Format '$stringValue' must be a valid ISO or PHP date format string.");
120  } else {
121  if (((isset($options['format_type']) === true) and ($options['format_type'] == 'php')) or
122  ((isset($options['format_type']) === false) and (self::$_options['format_type'] == 'php'))) {
124  }
125  }
126  break;
127 
128  case 'format_type' :
129  if (($value != 'php') && ($value != 'iso')) {
130  #require_once 'Zend/Locale/Exception.php';
131  throw new Zend_Locale_Exception("Unknown date format type '$value'. Only 'iso' and 'php'"
132  . " are supported.");
133  }
134  break;
135 
136  case 'fix_date' :
137  if (($value !== true) && ($value !== false)) {
138  #require_once 'Zend/Locale/Exception.php';
139  throw new Zend_Locale_Exception("Enabling correction of dates must be either true or false"
140  . "(fix_date='$value').");
141  }
142  break;
143 
144  case 'locale' :
146  break;
147 
148  case 'cache' :
149  if ($value instanceof Zend_Cache_Core) {
151  }
152  break;
153 
154  case 'disablecache' :
155  if (null !== $value) {
157  }
158  break;
159 
160  case 'precision' :
161  if ($value === NULL) {
162  $value = -1;
163  }
164 
165  if (($value < -1) || ($value > 30)) {
166  #require_once 'Zend/Locale/Exception.php';
167  throw new Zend_Locale_Exception("'$value' precision is not a whole number less than 30.");
168  }
169  break;
170 
171  default:
172  #require_once 'Zend/Locale/Exception.php';
173  throw new Zend_Locale_Exception("Unknown option: '$name' = '$value'");
174  break;
175 
176  }
177  }
178 
179  return $options;
180  }
181 
198  public static function convertNumerals($input, $from, $to = null)
199  {
200  if (!self::_getUniCodeSupport()) {
201  trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
202  }
203 
204  $from = strtolower($from);
205  $source = Zend_Locale_Data::getContent('en', 'numberingsystem', $from);
206  if (empty($source)) {
207  #require_once 'Zend/Locale/Exception.php';
208  throw new Zend_Locale_Exception("Unknown script '$from'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
209  }
210 
211  if ($to !== null) {
212  $to = strtolower($to);
213  $target = Zend_Locale_Data::getContent('en', 'numberingsystem', $to);
214  if (empty($target)) {
215  #require_once 'Zend/Locale/Exception.php';
216  throw new Zend_Locale_Exception("Unknown script '$to'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
217  }
218  } else {
219  $target = '0123456789';
220  }
221 
222  for ($x = 0; $x < 10; ++$x) {
223  $asource[$x] = "/" . iconv_substr($source, $x, 1, 'UTF-8') . "/u";
224  $atarget[$x] = iconv_substr($target, $x, 1, 'UTF-8');
225  }
226 
227  return preg_replace($asource, $atarget, $input);
228  }
229 
246  public static function getNumber($input, array $options = array())
247  {
248  $options = self::_checkOptions($options) + self::$_options;
249  if (!is_string($input)) {
250  return $input;
251  }
252 
253  if (!self::isNumber($input, $options)) {
254  #require_once 'Zend/Locale/Exception.php';
255  throw new Zend_Locale_Exception('No localized value in ' . $input . ' found, or the given number does not match the localized format');
256  }
257 
258  // Get correct signs for this locale
259  $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
260  // Change locale input to be default number
261  if (($input[0] == $symbols['minus']) && ('-' != $input[0])) {
262  $input = '-' . substr($input, 1);
263  }
264 
265  $input = str_replace($symbols['group'],'', $input);
266  if (strpos($input, $symbols['decimal']) !== false) {
267  if ($symbols['decimal'] != '.') {
268  $input = str_replace($symbols['decimal'], ".", $input);
269  }
270 
271  $pre = substr($input, strpos($input, '.') + 1);
272  if ($options['precision'] === null) {
273  $options['precision'] = strlen($pre);
274  }
275 
276  if (strlen($pre) >= $options['precision']) {
277  $input = substr($input, 0, strlen($input) - strlen($pre) + $options['precision']);
278  }
279 
280  if (($options['precision'] == 0) && ($input[strlen($input) - 1] == '.')) {
281  $input = substr($input, 0, -1);
282  }
283  }
284 
285  return $input;
286  }
287 
300  public static function toNumber($value, array $options = array())
301  {
302  // load class within method for speed
303  #require_once 'Zend/Locale/Math.php';
304 
307  $options = self::_checkOptions($options) + self::$_options;
308  $options['locale'] = (string) $options['locale'];
309 
310  // Get correct signs for this locale
311  $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
312  $oenc = self::_getEncoding();
313  self::_setEncoding('UTF-8');
314 
315  // Get format
316  $format = $options['number_format'];
317  if ($format === null) {
318  $format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
319  $format = self::_seperateFormat($format, $value, $options['precision']);
320 
321  if ($options['precision'] !== null) {
323  }
324  } else {
325  // seperate negative format pattern when available
326  $format = self::_seperateFormat($format, $value, $options['precision']);
327  if (strpos($format, '.')) {
328  if (is_numeric($options['precision'])) {
330  } else {
331  if (substr($format, iconv_strpos($format, '.') + 1, 3) == '###') {
332  $options['precision'] = null;
333  } else {
334  $options['precision'] = iconv_strlen(iconv_substr($format, iconv_strpos($format, '.') + 1,
335  iconv_strrpos($format, '0') - iconv_strpos($format, '.')));
336  $format = iconv_substr($format, 0, iconv_strpos($format, '.') + 1) . '###'
337  . iconv_substr($format, iconv_strrpos($format, '0') + 1);
338  }
339  }
340  } else {
342  $options['precision'] = 0;
343  }
345  }
346 
347  if (iconv_strpos($format, '0') === false) {
348  self::_setEncoding($oenc);
349  #require_once 'Zend/Locale/Exception.php';
350  throw new Zend_Locale_Exception('Wrong format... missing 0');
351  }
352 
353  // get number parts
354  $pos = iconv_strpos($value, '.');
355  if ($pos !== false) {
356  if ($options['precision'] === null) {
357  $precstr = iconv_substr($value, $pos + 1);
358  } else {
359  $precstr = iconv_substr($value, $pos + 1, $options['precision']);
360  if (iconv_strlen($precstr) < $options['precision']) {
361  $precstr = $precstr . str_pad("0", ($options['precision'] - iconv_strlen($precstr)), "0");
362  }
363  }
364  } else {
365  if ($options['precision'] > 0) {
366  $precstr = str_pad("0", ($options['precision']), "0");
367  }
368  }
369 
370  if ($options['precision'] === null) {
371  if (isset($precstr)) {
372  $options['precision'] = iconv_strlen($precstr);
373  } else {
374  $options['precision'] = 0;
375  }
376  }
377 
378  // get fraction and format lengths
379  if (strpos($value, '.') !== false) {
380  $number = substr((string) $value, 0, strpos($value, '.'));
381  } else {
382  $number = $value;
383  }
384 
386  $prec = Zend_Locale_Math::floatalize($prec);
387  $prec = Zend_Locale_Math::normalize($prec);
388  if (iconv_strpos($prec, '-') !== false) {
389  $prec = iconv_substr($prec, 1);
390  }
391 
392  if (($prec == 0) and ($options['precision'] > 0)) {
393  $prec = "0.0";
394  }
395 
396  if (($options['precision'] + 2) > iconv_strlen($prec)) {
397  $prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
398  }
399 
400  if (iconv_strpos($number, '-') !== false) {
401  $number = iconv_substr($number, 1);
402  }
403  $group = iconv_strrpos($format, ',');
404  $group2 = iconv_strpos ($format, ',');
405  $point = iconv_strpos ($format, '0');
406  // Add fraction
407  $rest = "";
408  if (iconv_strpos($format, '.')) {
409  $rest = iconv_substr($format, iconv_strpos($format, '.') + 1);
410  $length = iconv_strlen($rest);
411  for($x = 0; $x < $length; ++$x) {
412  if (($rest[0] == '0') || ($rest[0] == '#')) {
413  $rest = iconv_substr($rest, 1);
414  }
415  }
416  $format = iconv_substr($format, 0, iconv_strlen($format) - iconv_strlen($rest));
417  }
418 
419  if ($options['precision'] == '0') {
420  if (iconv_strrpos($format, '-') != 0) {
421  $format = iconv_substr($format, 0, $point)
422  . iconv_substr($format, iconv_strrpos($format, '#') + 2);
423  } else {
424  $format = iconv_substr($format, 0, $point);
425  }
426  } else {
427  $format = iconv_substr($format, 0, $point) . $symbols['decimal']
428  . iconv_substr($prec, 2);
429  }
430 
431  $format .= $rest;
432  // Add seperation
433  if ($group == 0) {
434  // no seperation
435  $format = $number . iconv_substr($format, $point);
436  } else if ($group == $group2) {
437  // only 1 seperation
438  $seperation = ($point - $group);
439  for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
440  if (iconv_substr($number, 0, $x - $seperation) !== "") {
441  $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group']
442  . iconv_substr($number, $x - $seperation);
443  }
444  }
445  $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
446  } else {
447 
448  // 2 seperations
449  if (iconv_strlen($number) > ($point - $group)) {
450  $seperation = ($point - $group);
451  $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group']
452  . iconv_substr($number, iconv_strlen($number) - $seperation);
453 
454  if ((iconv_strlen($number) - 1) > ($point - $group + 1)) {
455  $seperation2 = ($group - $group2 - 1);
456  for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
457  $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group']
458  . iconv_substr($number, $x - $seperation2);
459  }
460  }
461 
462  }
463  $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
464  }
465  // set negative sign
466  if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
467  if (iconv_strpos($format, '-') === false) {
468  $format = $symbols['minus'] . $format;
469  } else {
470  $format = str_replace('-', $symbols['minus'], $format);
471  }
472  }
473 
474  self::_setEncoding($oenc);
475  return (string) $format;
476  }
477 
484  private static function _seperateFormat($format, $value, $precision)
485  {
486  if (iconv_strpos($format, ';') !== false) {
487  if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $precision) < 0) {
488  $tmpformat = iconv_substr($format, iconv_strpos($format, ';') + 1);
489  if ($tmpformat[0] == '(') {
490  $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
491  } else {
492  $format = $tmpformat;
493  }
494  } else {
495  $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
496  }
497  }
498 
499  return $format;
500  }
501 
502 
510  public static function isNumber($input, array $options = array())
511  {
512  if (!self::_getUniCodeSupport()) {
513  trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
514  }
515 
516  $options = self::_checkOptions($options) + self::$_options;
517 
518  // Get correct signs for this locale
519  $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
520 
521  $regexs = Zend_Locale_Format::_getRegexForType('decimalnumber', $options);
522  $regexs = array_merge($regexs, Zend_Locale_Format::_getRegexForType('scientificnumber', $options));
523  if (!empty($input) && ($input[0] == $symbols['decimal'])) {
524  $input = 0 . $input;
525  }
526  foreach ($regexs as $regex) {
527  preg_match($regex, $input, $found);
528  if (isset($found[0])) {
529  return true;
530  }
531  }
532 
533  return false;
534  }
535 
544  private static function _getRegexForType($type, $options)
545  {
546  $decimal = Zend_Locale_Data::getContent($options['locale'], $type);
547  $decimal = preg_replace('/[^#0,;\.\-Ee]/u', '',$decimal);
548  $patterns = explode(';', $decimal);
549 
550  if (count($patterns) == 1) {
551  $patterns[1] = '-' . $patterns[0];
552  }
553 
554  $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
555 
556  foreach($patterns as $pkey => $pattern) {
557  $regex[$pkey] = '/^';
558  $rest = 0;
559  $end = null;
560  if (strpos($pattern, '.') !== false) {
561  $end = substr($pattern, strpos($pattern, '.') + 1);
562  $pattern = substr($pattern, 0, -strlen($end) - 1);
563  }
564 
565  if (strpos($pattern, ',') !== false) {
566  $parts = explode(',', $pattern);
567  $count = count($parts);
568  foreach($parts as $key => $part) {
569  switch ($part) {
570  case '#':
571  case '-#':
572  if ($part[0] == '-') {
573  $regex[$pkey] .= '[' . preg_quote($symbols['minus']) . '-]{0,1}';
574  } else {
575  $regex[$pkey] .= '[' . $symbols['plus'] . '+]{0,1}';
576  }
577 
578  if (($parts[$key + 1]) == '##0') {
579  $regex[$pkey] .= '[0-9]{1,3}';
580  } else if (($parts[$key + 1]) == '##') {
581  $regex[$pkey] .= '[0-9]{1,2}';
582  } else {
583  throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 1):"' . $pattern . '"');
584  }
585  break;
586  case '##':
587  if ($parts[$key + 1] == '##0') {
588  $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{2})*';
589  } else {
590  throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 2):"' . $pattern . '"');
591  }
592  break;
593  case '##0':
594  if ($parts[$key - 1] == '##') {
595  $regex[$pkey] .= '[0-9]';
596  } else if (($parts[$key - 1] == '#') || ($parts[$key - 1] == '-#')) {
597  $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{3})*';
598  } else {
599  throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 3):"' . $pattern . '"');
600  }
601  break;
602  case '#0':
603  if ($key == 0) {
604  $regex[$pkey] .= '[0-9]*';
605  } else {
606  throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 4):"' . $pattern . '"');
607  }
608  break;
609  }
610  }
611  }
612 
613  if (strpos($pattern, 'E') !== false) {
614  if (($pattern == '#E0') || ($pattern == '#E00')) {
615  $regex[$pkey] .= '[' . $symbols['plus']. '+]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['plus']. '+]{0,1}[0-9]{1,}';
616  } else if (($pattern == '-#E0') || ($pattern == '-#E00')) {
617  $regex[$pkey] .= '[' . preg_quote($symbols['minus']) . '-]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . preg_quote($symbols['minus']) . '-]{0,1}[0-9]{1,}';
618  } else {
619  throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 5):"' . $pattern . '"');
620  }
621  }
622 
623  if (!empty($end)) {
624  if ($end == '###') {
625  $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}';
626  } else if ($end == '###-') {
627  $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}[' . preg_quote($symbols['minus']) . '-]';
628  } else {
629  throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 6):"' . $pattern . '"');
630  }
631  }
632 
633  $regex[$pkey] .= '$/u';
634  }
635 
636  return $regex;
637  }
638 
646  public static function getFloat($input, array $options = array())
647  {
648  return floatval(self::getNumber($input, $options));
649  }
650 
659  public static function toFloat($value, array $options = array())
660  {
661  $options['number_format'] = Zend_Locale_Format::STANDARD;
662  return self::toNumber($value, $options);
663  }
664 
673  public static function isFloat($value, array $options = array())
674  {
675  return self::isNumber($value, $options);
676  }
677 
694  public static function getInteger($input, array $options = array())
695  {
696  $options['precision'] = 0;
697  return intval(self::getFloat($input, $options));
698  }
699 
707  public static function toInteger($value, array $options = array())
708  {
709  $options['precision'] = 0;
710  $options['number_format'] = Zend_Locale_Format::STANDARD;
711  return self::toNumber($value, $options);
712  }
713 
721  public static function isInteger($value, array $options = array())
722  {
723  if (!self::isNumber($value, $options)) {
724  return false;
725  }
726 
727  if (self::getInteger($value, $options) == self::getFloat($value, $options)) {
728  return true;
729  }
730 
731  return false;
732  }
733 
746  public static function convertPhpToIsoFormat($format)
747  {
748  if ($format === null) {
749  return null;
750  }
751 
752  $convert = array(
753  'd' => 'dd' , 'D' => 'EE' , 'j' => 'd' , 'l' => 'EEEE',
754  'N' => 'eee' , 'S' => 'SS' , 'w' => 'e' , 'z' => 'D' ,
755  'W' => 'ww' , 'F' => 'MMMM', 'm' => 'MM' , 'M' => 'MMM' ,
756  'n' => 'M' , 't' => 'ddd' , 'L' => 'l' , 'o' => 'YYYY',
757  'Y' => 'yyyy', 'y' => 'yy' , 'a' => 'a' , 'A' => 'a' ,
758  'B' => 'B' , 'g' => 'h' , 'G' => 'H' , 'h' => 'hh' ,
759  'H' => 'HH' , 'i' => 'mm' , 's' => 'ss' , 'e' => 'zzzz',
760  'I' => 'I' , 'O' => 'Z' , 'P' => 'ZZZZ', 'T' => 'z' ,
761  'Z' => 'X' , 'c' => 'yyyy-MM-ddTHH:mm:ssZZZZ', 'r' => 'r',
762  'U' => 'U',
763  );
764  $escaped = false;
765  $inEscapedString = false;
766  $converted = array();
767  foreach (str_split($format) as $char) {
768  if (!$escaped && $char == '\\') {
769  // Next char will be escaped: let's remember it
770  $escaped = true;
771  } elseif ($escaped) {
772  if (!$inEscapedString) {
773  // First escaped string: start the quoted chunk
774  $converted[] = "'";
775  $inEscapedString = true;
776  }
777  // Since the previous char was a \ and we are in the quoted
778  // chunk, let's simply add $char as it is
779  $converted[] = $char;
780  $escaped = false;
781  } elseif ($char == "'") {
782  // Single quotes need to be escaped like this
783  $converted[] = "''";
784  } else {
785  if ($inEscapedString) {
786  // Close the single-quoted chunk
787  $converted[] = "'";
788  $inEscapedString = false;
789  }
790  // Convert the unescaped char if needed
791  if (isset($convert[$char])) {
792  $converted[] = $convert[$char];
793  } else {
794  $converted[] = $char;
795  }
796  }
797  }
798 
799  return implode($converted);
800  }
801 
810  private static function _parseDate($date, $options)
811  {
812  if (!self::_getUniCodeSupport()) {
813  trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
814  }
815 
816  $options = self::_checkOptions($options) + self::$_options;
817  $test = array('h', 'H', 'm', 's', 'y', 'Y', 'M', 'd', 'D', 'E', 'S', 'l', 'B', 'I',
818  'X', 'r', 'U', 'G', 'w', 'e', 'a', 'A', 'Z', 'z', 'v');
819 
820  $format = $options['date_format'];
821  $number = $date; // working copy
822  $result['date_format'] = $format; // save the format used to normalize $number (convenience)
823  $result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
824 
825  $oenc = self::_getEncoding();
826  self::_setEncoding('UTF-8');
827  $day = iconv_strpos($format, 'd');
828  $month = iconv_strpos($format, 'M');
829  $year = iconv_strpos($format, 'y');
830  $hour = iconv_strpos($format, 'H');
831  $min = iconv_strpos($format, 'm');
832  $sec = iconv_strpos($format, 's');
833  $am = null;
834  if ($hour === false) {
835  $hour = iconv_strpos($format, 'h');
836  }
837  if ($year === false) {
838  $year = iconv_strpos($format, 'Y');
839  }
840  if ($day === false) {
841  $day = iconv_strpos($format, 'E');
842  if ($day === false) {
843  $day = iconv_strpos($format, 'D');
844  }
845  }
846 
847  if ($day !== false) {
848  $parse[$day] = 'd';
849  if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
850  (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
851  // erase day string
852  $daylist = Zend_Locale_Data::getList($options['locale'], 'day');
853  foreach($daylist as $key => $name) {
854  if (iconv_strpos($number, $name) !== false) {
855  $number = str_replace($name, "EEEE", $number);
856  break;
857  }
858  }
859  }
860  }
861  $position = false;
862 
863  if ($month !== false) {
864  $parse[$month] = 'M';
865  if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
866  (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
867  // prepare to convert month name to their numeric equivalents, if requested,
868  // and we have a $options['locale']
870  'month'));
871  if ($position === false) {
873  'month', array('gregorian', 'format', 'abbreviated')));
874  }
875  }
876  }
877  if ($year !== false) {
878  $parse[$year] = 'y';
879  }
880  if ($hour !== false) {
881  $parse[$hour] = 'H';
882  }
883  if ($min !== false) {
884  $parse[$min] = 'm';
885  }
886  if ($sec !== false) {
887  $parse[$sec] = 's';
888  }
889 
890  if (empty($parse)) {
891  self::_setEncoding($oenc);
892  #require_once 'Zend/Locale/Exception.php';
893  throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
894  }
895  ksort($parse);
896 
897  // get daytime
898  if (iconv_strpos($format, 'a') !== false) {
899  if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'am'))) !== false) {
900  $am = true;
901  } else if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'pm'))) !== false) {
902  $am = false;
903  }
904  }
905 
906  // split number parts
907  $split = false;
908  preg_match_all('/\d+/u', $number, $splitted);
909 
910  if (count($splitted[0]) == 0) {
911  self::_setEncoding($oenc);
912  #require_once 'Zend/Locale/Exception.php';
913  throw new Zend_Locale_Exception("No date part in '$date' found.");
914  }
915  if (count($splitted[0]) == 1) {
916  $split = 0;
917  }
918  $cnt = 0;
919  foreach($parse as $key => $value) {
920 
921  switch($value) {
922  case 'd':
923  if ($split === false) {
924  if (count($splitted[0]) > $cnt) {
925  $result['day'] = $splitted[0][$cnt];
926  }
927  } else {
928  $result['day'] = iconv_substr($splitted[0][0], $split, 2);
929  $split += 2;
930  }
931  ++$cnt;
932  break;
933  case 'M':
934  if ($split === false) {
935  if (count($splitted[0]) > $cnt) {
936  $result['month'] = $splitted[0][$cnt];
937  }
938  } else {
939  $result['month'] = iconv_substr($splitted[0][0], $split, 2);
940  $split += 2;
941  }
942  ++$cnt;
943  break;
944  case 'y':
945  $length = 2;
946  if ((iconv_substr($format, $year, 4) == 'yyyy')
947  || (iconv_substr($format, $year, 4) == 'YYYY')) {
948  $length = 4;
949  }
950 
951  if ($split === false) {
952  if (count($splitted[0]) > $cnt) {
953  $result['year'] = $splitted[0][$cnt];
954  }
955  } else {
956  $result['year'] = iconv_substr($splitted[0][0], $split, $length);
957  $split += $length;
958  }
959 
960  ++$cnt;
961  break;
962  case 'H':
963  if ($split === false) {
964  if (count($splitted[0]) > $cnt) {
965  $result['hour'] = $splitted[0][$cnt];
966  }
967  } else {
968  $result['hour'] = iconv_substr($splitted[0][0], $split, 2);
969  $split += 2;
970  }
971  ++$cnt;
972  break;
973  case 'm':
974  if ($split === false) {
975  if (count($splitted[0]) > $cnt) {
976  $result['minute'] = $splitted[0][$cnt];
977  }
978  } else {
979  $result['minute'] = iconv_substr($splitted[0][0], $split, 2);
980  $split += 2;
981  }
982  ++$cnt;
983  break;
984  case 's':
985  if ($split === false) {
986  if (count($splitted[0]) > $cnt) {
987  $result['second'] = $splitted[0][$cnt];
988  }
989  } else {
990  $result['second'] = iconv_substr($splitted[0][0], $split, 2);
991  $split += 2;
992  }
993  ++$cnt;
994  break;
995  }
996  }
997 
998  // AM/PM correction
999  if ($hour !== false) {
1000  if (($am === true) and ($result['hour'] == 12)){
1001  $result['hour'] = 0;
1002  } else if (($am === false) and ($result['hour'] != 12)) {
1003  $result['hour'] += 12;
1004  }
1005  }
1006 
1007  if ($options['fix_date'] === true) {
1008  $result['fixed'] = 0; // nothing has been "fixed" by swapping date parts around (yet)
1009  }
1010 
1011  if ($day !== false) {
1012  // fix false month
1013  if (isset($result['day']) and isset($result['month'])) {
1014  if (($position !== false) and ((iconv_strpos($date, $result['day']) === false) or
1015  (isset($result['year']) and (iconv_strpos($date, $result['year']) === false)))) {
1016  if ($options['fix_date'] !== true) {
1017  self::_setEncoding($oenc);
1018  #require_once 'Zend/Locale/Exception.php';
1019  throw new Zend_Locale_Exception("Unable to parse date '$date' using '" . $format
1020  . "' (false month, $position, $month)");
1021  }
1022  $temp = $result['day'];
1023  $result['day'] = $result['month'];
1024  $result['month'] = $temp;
1025  $result['fixed'] = 1;
1026  }
1027  }
1028 
1029  // fix switched values d <> y
1030  if (isset($result['day']) and isset($result['year'])) {
1031  if ($result['day'] > 31) {
1032  if ($options['fix_date'] !== true) {
1033  self::_setEncoding($oenc);
1034  #require_once 'Zend/Locale/Exception.php';
1035  throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
1036  . $format . "' (d <> y)");
1037  }
1038  $temp = $result['year'];
1039  $result['year'] = $result['day'];
1040  $result['day'] = $temp;
1041  $result['fixed'] = 2;
1042  }
1043  }
1044 
1045  // fix switched values M <> y
1046  if (isset($result['month']) and isset($result['year'])) {
1047  if ($result['month'] > 31) {
1048  if ($options['fix_date'] !== true) {
1049  self::_setEncoding($oenc);
1050  #require_once 'Zend/Locale/Exception.php';
1051  throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
1052  . $format . "' (M <> y)");
1053  }
1054  $temp = $result['year'];
1055  $result['year'] = $result['month'];
1056  $result['month'] = $temp;
1057  $result['fixed'] = 3;
1058  }
1059  }
1060 
1061  // fix switched values M <> d
1062  if (isset($result['month']) and isset($result['day'])) {
1063  if ($result['month'] > 12) {
1064  if ($options['fix_date'] !== true || $result['month'] > 31) {
1065  self::_setEncoding($oenc);
1066  #require_once 'Zend/Locale/Exception.php';
1067  throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
1068  . $format . "' (M <> d)");
1069  }
1070  $temp = $result['day'];
1071  $result['day'] = $result['month'];
1072  $result['month'] = $temp;
1073  $result['fixed'] = 4;
1074  }
1075  }
1076  }
1077 
1078  if (isset($result['year'])) {
1079  if (((iconv_strlen($result['year']) == 2) && ($result['year'] < 10)) ||
1080  (((iconv_strpos($format, 'yy') !== false) && (iconv_strpos($format, 'yyyy') === false)) ||
1081  ((iconv_strpos($format, 'YY') !== false) && (iconv_strpos($format, 'YYYY') === false)))) {
1082  if (($result['year'] >= 0) && ($result['year'] < 100)) {
1083  if ($result['year'] < 70) {
1084  $result['year'] = (int) $result['year'] + 100;
1085  }
1086 
1087  $result['year'] = (int) $result['year'] + 1900;
1088  }
1089  }
1090  }
1091 
1092  self::_setEncoding($oenc);
1093  return $result;
1094  }
1095 
1104  protected static function _replaceMonth(&$number, $monthlist)
1105  {
1106  // If $locale was invalid, $monthlist will default to a "root" identity
1107  // mapping for each month number from 1 to 12.
1108  // If no $locale was given, or $locale was invalid, do not use this identity mapping to normalize.
1109  // Otherwise, translate locale aware month names in $number to their numeric equivalents.
1110  $position = false;
1111  if ($monthlist && $monthlist[1] != 1) {
1112  foreach($monthlist as $key => $name) {
1113  if (($position = iconv_strpos($number, $name, 0, 'UTF-8')) !== false) {
1114  $number = str_ireplace($name, $key, $number);
1115  return $position;
1116  }
1117  }
1118  }
1119 
1120  return false;
1121  }
1122 
1130  public static function getDateFormat($locale = null)
1131  {
1132  $format = Zend_Locale_Data::getContent($locale, 'date');
1133  if (empty($format)) {
1134  #require_once 'Zend/Locale/Exception.php';
1135  throw new Zend_Locale_Exception("failed to receive data from locale $locale");
1136  }
1137 
1138  return $format;
1139  }
1140 
1154  public static function getDate($date, array $options = array())
1155  {
1156  $options = self::_checkOptions($options) + self::$_options;
1157  if (empty($options['date_format'])) {
1158  $options['format_type'] = 'iso';
1159  $options['date_format'] = self::getDateFormat($options['locale']);
1160  }
1161 
1162  return self::_parseDate($date, $options);
1163  }
1164 
1174  public static function checkDateFormat($date, array $options = array())
1175  {
1176  try {
1177  $date = self::getDate($date, $options);
1178  } catch (Exception $e) {
1179  return false;
1180  }
1181 
1182  if (empty($options['date_format'])) {
1183  $options['format_type'] = 'iso';
1184  $options['date_format'] = self::getDateFormat(isset($options['locale']) ? $options['locale'] : null);
1185  }
1186  $options = self::_checkOptions($options) + self::$_options;
1187 
1188  // day expected but not parsed
1189  if ((iconv_strpos($options['date_format'], 'd', 0, 'UTF-8') !== false) and (!isset($date['day']) or ($date['day'] === ""))) {
1190  return false;
1191  }
1192 
1193  // month expected but not parsed
1194  if ((iconv_strpos($options['date_format'], 'M', 0, 'UTF-8') !== false) and (!isset($date['month']) or ($date['month'] === ""))) {
1195  return false;
1196  }
1197 
1198  // year expected but not parsed
1199  if (((iconv_strpos($options['date_format'], 'Y', 0, 'UTF-8') !== false) or
1200  (iconv_strpos($options['date_format'], 'y', 0, 'UTF-8') !== false)) and (!isset($date['year']) or ($date['year'] === ""))) {
1201  return false;
1202  }
1203 
1204  // second expected but not parsed
1205  if ((iconv_strpos($options['date_format'], 's', 0, 'UTF-8') !== false) and (!isset($date['second']) or ($date['second'] === ""))) {
1206  return false;
1207  }
1208 
1209  // minute expected but not parsed
1210  if ((iconv_strpos($options['date_format'], 'm', 0, 'UTF-8') !== false) and (!isset($date['minute']) or ($date['minute'] === ""))) {
1211  return false;
1212  }
1213 
1214  // hour expected but not parsed
1215  if (((iconv_strpos($options['date_format'], 'H', 0, 'UTF-8') !== false) or
1216  (iconv_strpos($options['date_format'], 'h', 0, 'UTF-8') !== false)) and (!isset($date['hour']) or ($date['hour'] === ""))) {
1217  return false;
1218  }
1219 
1220  return true;
1221  }
1222 
1230  public static function getTimeFormat($locale = null)
1231  {
1232  $format = Zend_Locale_Data::getContent($locale, 'time');
1233  if (empty($format)) {
1234  #require_once 'Zend/Locale/Exception.php';
1235  throw new Zend_Locale_Exception("failed to receive data from locale $locale");
1236  }
1237  return $format;
1238  }
1239 
1252  public static function getTime($time, array $options = array())
1253  {
1254  $options = self::_checkOptions($options) + self::$_options;
1255  if (empty($options['date_format'])) {
1256  $options['format_type'] = 'iso';
1257  $options['date_format'] = self::getTimeFormat($options['locale']);
1258  }
1259  return self::_parseDate($time, $options);
1260  }
1261 
1269  public static function getDateTimeFormat($locale = null)
1270  {
1271  $format = Zend_Locale_Data::getContent($locale, 'datetime');
1272  if (empty($format)) {
1273  #require_once 'Zend/Locale/Exception.php';
1274  throw new Zend_Locale_Exception("failed to receive data from locale $locale");
1275  }
1276  return $format;
1277  }
1278 
1291  public static function getDateTime($datetime, array $options = array())
1292  {
1293  $options = self::_checkOptions($options) + self::$_options;
1294  if (empty($options['date_format'])) {
1295  $options['format_type'] = 'iso';
1296  $options['date_format'] = self::getDateTimeFormat($options['locale']);
1297  }
1298  return self::_parseDate($datetime, $options);
1299  }
1300 
1307  protected static function _getUniCodeSupport()
1308  {
1309  return (@preg_match('/\pL/u', 'a')) ? true : false;
1310  }
1311 
1318  protected static function _getEncoding()
1319  {
1320  $oenc = PHP_VERSION_ID < 50600
1321  ? iconv_get_encoding('internal_encoding')
1322  : ini_get('default_charset');
1323 
1324  return $oenc;
1325  }
1326 
1334  protected static function _setEncoding($encoding)
1335  {
1336  if (PHP_VERSION_ID < 50600) {
1337  iconv_set_encoding('internal_encoding', $encoding);
1338  } else {
1339  ini_set('default_charset', $encoding);
1340  }
1341  }
1342 }
static getFloat($input, array $options=array())
Definition: Format.php:646
static $sub
Definition: Math.php:41
static $comp
Definition: Math.php:45
static toFloat($value, array $options=array())
Definition: Format.php:659
static convertPhpToIsoFormat($format)
Definition: Format.php:746
static getList($locale, $path, $value=false)
Definition: Data.php:318
ini_set($varName, $newValue)
static getDate($date, array $options=array())
Definition: Format.php:1154
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static setOptions(array $options=array())
Definition: Format.php:64
static isFloat($value, array $options=array())
Definition: Format.php:673
static floatalize($value)
Definition: Math.php:144
$number
Definition: details.phtml:22
$pattern
Definition: website.php:22
static getContent($locale, $path, $value=false)
Definition: Data.php:968
$source
Definition: source.php:23
$count
Definition: recent.phtml:13
static _replaceMonth(&$number, $monthlist)
Definition: Format.php:1104
$target
Definition: skip.phtml:8
static isNumber($input, array $options=array())
Definition: Format.php:510
$group
Definition: sections.phtml:16
static getDateFormat($locale=null)
Definition: Format.php:1130
static _getUniCodeSupport()
Definition: Format.php:1307
$type
Definition: item.phtml:13
static convertNumerals($input, $from, $to=null)
Definition: Format.php:198
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
$pos
Definition: list.phtml:42
static isInteger($value, array $options=array())
Definition: Format.php:721
static _getEncoding()
Definition: Format.php:1318
static toInteger($value, array $options=array())
Definition: Format.php:707
static findLocale($locale=null)
Definition: Locale.php:1740
static getDateTimeFormat($locale=null)
Definition: Format.php:1269
static _setEncoding($encoding)
Definition: Format.php:1334
static disableCache($flag)
Definition: Data.php:1576
static getDateTime($datetime, array $options=array())
Definition: Format.php:1291
static setCache(Zend_Cache_Core $cache)
Definition: Data.php:1527
static getInteger($input, array $options=array())
Definition: Format.php:694
static normalize($value)
Definition: Math.php:174
static getTime($time, array $options=array())
Definition: Format.php:1252
static toNumber($value, array $options=array())
Definition: Format.php:300
static round($op1, $precision=0)
Definition: Math.php:65
static checkDateFormat($date, array $options=array())
Definition: Format.php:1174
static getTimeFormat($locale=null)
Definition: Format.php:1230
static getNumber($input, array $options=array())
Definition: Format.php:246
if(!isset($_GET['name'])) $name
Definition: log.php:14