web/lib/Zend/Locale/Format.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Locale
    16  * @package    Zend_Locale
    17  * @subpackage Format
    17  * @subpackage Format
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @version    $Id: Format.php 24807 2012-05-15 12:10:42Z adamlundrigan $
    19  * @version    $Id$
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * include needed classes
    24  * include needed classes
    27 
    27 
    28 /**
    28 /**
    29  * @category   Zend
    29  * @category   Zend
    30  * @package    Zend_Locale
    30  * @package    Zend_Locale
    31  * @subpackage Format
    31  * @subpackage Format
    32  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    32  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    34  */
    34  */
    35 class Zend_Locale_Format
    35 class Zend_Locale_Format
    36 {
    36 {
    37     const STANDARD   = 'auto';
    37     const STANDARD   = 'auto';
    40                                      'number_format' => null,
    40                                      'number_format' => null,
    41                                      'format_type'   => 'iso',
    41                                      'format_type'   => 'iso',
    42                                      'fix_date'      => false,
    42                                      'fix_date'      => false,
    43                                      'locale'        => null,
    43                                      'locale'        => null,
    44                                      'cache'         => null,
    44                                      'cache'         => null,
    45                                      'disableCache'  => false,
    45                                      'disableCache'  => null,
    46                                      'precision'     => null);
    46                                      'precision'     => null);
    47 
    47 
    48     /**
    48     /**
    49      * Sets class wide options, if no option was given, the actual set options will be returned
    49      * Sets class wide options, if no option was given, the actual set options will be returned
    50      * The 'precision' option of a value is used to truncate or stretch extra digits. -1 means not to touch the extra digits.
    50      * The 'precision' option of a value is used to truncate or stretch extra digits. -1 means not to touch the extra digits.
    57      * with the default date format for a locale, use array('date_format' => null, 'locale' => $locale) for their options.
    57      * with the default date format for a locale, use array('date_format' => null, 'locale' => $locale) for their options.
    58      *
    58      *
    59      * @param  array  $options  Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
    59      * @param  array  $options  Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
    60      *                          locale = Zend_Locale | locale string, precision = whole number between -1 and 30
    60      *                          locale = Zend_Locale | locale string, precision = whole number between -1 and 30
    61      * @throws Zend_Locale_Exception
    61      * @throws Zend_Locale_Exception
    62      * @return Options array if no option was given
    62      * @return array if no option was given
    63      */
    63      */
    64     public static function setOptions(array $options = array())
    64     public static function setOptions(array $options = array())
    65     {
    65     {
    66         self::$_options = self::_checkOptions($options) + self::$_options;
    66         self::$_options = self::_checkOptions($options) + self::$_options;
    67         return self::$_options;
    67         return self::$_options;
    72      * See {@link setOptions()} for details.
    72      * See {@link setOptions()} for details.
    73      *
    73      *
    74      * @param  array  $options  Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
    74      * @param  array  $options  Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
    75      *                          locale = Zend_Locale | locale string, precision = whole number between -1 and 30
    75      *                          locale = Zend_Locale | locale string, precision = whole number between -1 and 30
    76      * @throws Zend_Locale_Exception
    76      * @throws Zend_Locale_Exception
    77      * @return Options array if no option was given
    77      * @return array if no option was given
    78      */
    78      */
    79     private static function _checkOptions(array $options = array())
    79     private static function _checkOptions(array $options = array())
    80     {
    80     {
    81         if (count($options) == 0) {
    81         if (count($options) == 0) {
    82             return self::$_options;
    82             return self::$_options;
   150                         Zend_Locale_Data::setCache($value);
   150                         Zend_Locale_Data::setCache($value);
   151                     }
   151                     }
   152                     break;
   152                     break;
   153 
   153 
   154                 case 'disablecache' :
   154                 case 'disablecache' :
   155                     Zend_Locale_Data::disableCache($value);
   155                     if (null !== $value) {
       
   156                         Zend_Locale_Data::disableCache($value);
       
   157                     }
   156                     break;
   158                     break;
   157 
   159 
   158                 case 'precision' :
   160                 case 'precision' :
   159                     if ($value === NULL) {
   161                     if ($value === NULL) {
   160                         $value = -1;
   162                         $value = -1;
   254         }
   256         }
   255 
   257 
   256         // Get correct signs for this locale
   258         // Get correct signs for this locale
   257         $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
   259         $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
   258         // Change locale input to be default number
   260         // Change locale input to be default number
   259         if ((strpos($input, $symbols['minus']) !== false) ||
   261         if (($input[0] == $symbols['minus']) && ('-' != $input[0])) {
   260             (strpos($input, '-') !== false)) {
   262             $input = '-' . substr($input, 1);
   261             $input = strtr($input, array($symbols['minus'] => '', '-' => ''));
       
   262             $input = '-' . $input;
       
   263         }
   263         }
   264 
   264 
   265         $input = str_replace($symbols['group'],'', $input);
   265         $input = str_replace($symbols['group'],'', $input);
   266         if (strpos($input, $symbols['decimal']) !== false) {
   266         if (strpos($input, $symbols['decimal']) !== false) {
   267             if ($symbols['decimal'] != '.') {
   267             if ($symbols['decimal'] != '.') {
   290      * The seperation and fraction sign is used from the set locale.
   290      * The seperation and fraction sign is used from the set locale.
   291      * ##0.#  -> 12345.12345 -> 12345.12345
   291      * ##0.#  -> 12345.12345 -> 12345.12345
   292      * ##0.00 -> 12345.12345 -> 12345.12
   292      * ##0.00 -> 12345.12345 -> 12345.12
   293      * ##,##0.00 -> 12345.12345 -> 12,345.12
   293      * ##,##0.00 -> 12345.12345 -> 12,345.12
   294      *
   294      *
   295      * @param   string  $input    Localized number string
   295      * @param   string  $value    Localized number string
   296      * @param   array   $options  Options: number_format, locale, precision. See {@link setOptions()} for details.
   296      * @param   array   $options  Options: number_format, locale, precision. See {@link setOptions()} for details.
   297      * @return  string  locale formatted number
   297      * @return  string  locale formatted number
   298      * @throws Zend_Locale_Exception
   298      * @throws Zend_Locale_Exception
   299      */
   299      */
   300     public static function toNumber($value, array $options = array())
   300     public static function toNumber($value, array $options = array())
   307         $options           = self::_checkOptions($options) + self::$_options;
   307         $options           = self::_checkOptions($options) + self::$_options;
   308         $options['locale'] = (string) $options['locale'];
   308         $options['locale'] = (string) $options['locale'];
   309 
   309 
   310         // Get correct signs for this locale
   310         // Get correct signs for this locale
   311         $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
   311         $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
   312         $oenc = iconv_get_encoding('internal_encoding');
   312         $oenc = self::_getEncoding();
   313         iconv_set_encoding('internal_encoding', 'UTF-8');
   313         self::_setEncoding('UTF-8');
   314 
   314         
   315         // Get format
   315         // Get format
   316         $format = $options['number_format'];
   316         $format = $options['number_format'];
   317         if ($format === null) {
   317         if ($format === null) {
   318             $format  = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
   318             $format  = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
   319             $format  = self::_seperateFormat($format, $value, $options['precision']);
   319             $format  = self::_seperateFormat($format, $value, $options['precision']);
   343             }
   343             }
   344             $value = Zend_Locale_Math::normalize($value);
   344             $value = Zend_Locale_Math::normalize($value);
   345         }
   345         }
   346 
   346 
   347         if (iconv_strpos($format, '0') === false) {
   347         if (iconv_strpos($format, '0') === false) {
   348             iconv_set_encoding('internal_encoding', $oenc);
   348             self::_setEncoding($oenc);
   349             require_once 'Zend/Locale/Exception.php';
   349             require_once 'Zend/Locale/Exception.php';
   350             throw new Zend_Locale_Exception('Wrong format... missing 0');
   350             throw new Zend_Locale_Exception('Wrong format... missing 0');
   351         }
   351         }
   352 
   352 
   353         // get number parts
   353         // get number parts
   469             } else {
   469             } else {
   470                 $format = str_replace('-', $symbols['minus'], $format);
   470                 $format = str_replace('-', $symbols['minus'], $format);
   471             }
   471             }
   472         }
   472         }
   473 
   473 
   474         iconv_set_encoding('internal_encoding', $oenc);
   474         self::_setEncoding($oenc);
   475         return (string) $format;
   475         return (string) $format;
   476     }
   476     }
   477 
   477 
       
   478     /**
       
   479      * @param string $format
       
   480      * @param string $value
       
   481      * @param int $precision
       
   482      * @return string
       
   483      */
   478     private static function _seperateFormat($format, $value, $precision)
   484     private static function _seperateFormat($format, $value, $precision)
   479     {
   485     {
   480         if (iconv_strpos($format, ';') !== false) {
   486         if (iconv_strpos($format, ';') !== false) {
   481             if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $precision) < 0) {
   487             if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $precision) < 0) {
   482                 $tmpformat = iconv_substr($format, iconv_strpos($format, ';') + 1);
   488                 $tmpformat = iconv_substr($format, iconv_strpos($format, ';') + 1);
   529 
   535 
   530     /**
   536     /**
   531      * Internal method to convert cldr number syntax into regex
   537      * Internal method to convert cldr number syntax into regex
   532      *
   538      *
   533      * @param  string $type
   539      * @param  string $type
       
   540      * @param  array  $options Options: locale. See {@link setOptions()} for details.
   534      * @return string
   541      * @return string
       
   542      * @throws Zend_Locale_Exception
   535      */
   543      */
   536     private static function _getRegexForType($type, $options)
   544     private static function _getRegexForType($type, $options)
   537     {
   545     {
   538         $decimal  = Zend_Locale_Data::getContent($options['locale'], $type);
   546         $decimal  = Zend_Locale_Data::getContent($options['locale'], $type);
   539         $decimal  = preg_replace('/[^#0,;\.\-Ee]/u', '',$decimal);
   547         $decimal  = preg_replace('/[^#0,;\.\-Ee]/u', '',$decimal);
   629     }
   637     }
   630 
   638 
   631     /**
   639     /**
   632      * Alias for getNumber
   640      * Alias for getNumber
   633      *
   641      *
   634      * @param   string  $value    Number to localize
   642      * @param   string  $input    Number to localize
   635      * @param   array   $options  Options: locale, precision. See {@link setOptions()} for details.
   643      * @param   array   $options  Options: locale, precision. See {@link setOptions()} for details.
   636      * @return  float
   644      * @return  float
   637      */
   645      */
   638     public static function getFloat($input, array $options = array())
   646     public static function getFloat($input, array $options = array())
   639     {
   647     {
   656 
   664 
   657     /**
   665     /**
   658      * Returns if a float was found
   666      * Returns if a float was found
   659      * Alias for isNumber()
   667      * Alias for isNumber()
   660      *
   668      *
   661      * @param   string  $input    Localized number string
   669      * @param   string $value  Localized number string
   662      * @param   array   $options  Options: locale. See {@link setOptions()} for details.
   670      * @param   array $options Options: locale. See {@link setOptions()} for details.
   663      * @return  boolean           Returns true if a number was found
   671      * @return  boolean        Returns true if a number was found
   664      */
   672      */
   665     public static function isFloat($value, array $options = array())
   673     public static function isFloat($value, array $options = array())
   666     {
   674     {
   667         return self::isNumber($value, $options);
   675         return self::isNumber($value, $options);
   668     }
   676     }
   704     }
   712     }
   705 
   713 
   706     /**
   714     /**
   707      * Returns if a integer was found
   715      * Returns if a integer was found
   708      *
   716      *
   709      * @param   string  $input    Localized number string
   717      * @param  string $value Localized number string
   710      * @param   array   $options  Options: locale. See {@link setOptions()} for details.
   718      * @param  array $options Options: locale. See {@link setOptions()} for details.
   711      * @return  boolean           Returns true if a integer was found
   719      * @return boolean Returns true if a integer was found
   712      */
   720      */
   713     public static function isInteger($value, array $options = array())
   721     public static function isInteger($value, array $options = array())
   714     {
   722     {
   715         if (!self::isNumber($value, $options)) {
   723         if (!self::isNumber($value, $options)) {
   716             return false;
   724             return false;
   739     {
   747     {
   740         if ($format === null) {
   748         if ($format === null) {
   741             return null;
   749             return null;
   742         }
   750         }
   743 
   751 
   744         $convert = array('d' => 'dd'  , 'D' => 'EE'  , 'j' => 'd'   , 'l' => 'EEEE', 'N' => 'eee' , 'S' => 'SS'  ,
   752         $convert = array(
   745                          'w' => 'e'   , 'z' => 'D'   , 'W' => 'ww'  , 'F' => 'MMMM', 'm' => 'MM'  , 'M' => 'MMM' ,
   753             'd' => 'dd'  , 'D' => 'EE'  , 'j' => 'd'   , 'l' => 'EEEE',
   746                          'n' => 'M'   , 't' => 'ddd' , 'L' => 'l'   , 'o' => 'YYYY', 'Y' => 'yyyy', 'y' => 'yy'  ,
   754             'N' => 'eee' , 'S' => 'SS'  , 'w' => 'e'   , 'z' => 'D'   ,
   747                          'a' => 'a'   , 'A' => 'a'   , 'B' => 'B'   , 'g' => 'h'   , 'G' => 'H'   , 'h' => 'hh'  ,
   755             'W' => 'ww'  , 'F' => 'MMMM', 'm' => 'MM'  , 'M' => 'MMM' ,
   748                          'H' => 'HH'  , 'i' => 'mm'  , 's' => 'ss'  , 'e' => 'zzzz', 'I' => 'I'   , 'O' => 'Z'   ,
   756             'n' => 'M'   , 't' => 'ddd' , 'L' => 'l'   , 'o' => 'YYYY',
   749                          'P' => 'ZZZZ', 'T' => 'z'   , 'Z' => 'X'   , 'c' => 'yyyy-MM-ddTHH:mm:ssZZZZ',
   757             'Y' => 'yyyy', 'y' => 'yy'  , 'a' => 'a'   , 'A' => 'a'   ,
   750                          'r' => 'r'   , 'U' => 'U');
   758             'B' => 'B'   , 'g' => 'h'   , 'G' => 'H'   , 'h' => 'hh'  ,
   751         $values = str_split($format);
   759             'H' => 'HH'  , 'i' => 'mm'  , 's' => 'ss'  , 'e' => 'zzzz',
   752         foreach ($values as $key => $value) {
   760             'I' => 'I'   , 'O' => 'Z'   , 'P' => 'ZZZZ', 'T' => 'z'   ,
   753             if (isset($convert[$value]) === true) {
   761             'Z' => 'X'   , 'c' => 'yyyy-MM-ddTHH:mm:ssZZZZ', 'r' => 'r',
   754                 $values[$key] = $convert[$value];
   762             'U' => 'U',
   755             }
   763         );
   756         }
   764         $escaped = false;
   757 
   765         $inEscapedString = false;
   758         return join($values);
   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);
   759     }
   800     }
   760 
   801 
   761     /**
   802     /**
   762      * Parse date and split in named array fields
   803      * Parse date and split in named array fields
   763      *
   804      *
   764      * @param   string  $date     Date string to parse
   805      * @param  string $date    Date string to parse
   765      * @param   array   $options  Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
   806      * @param  array  $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
   766      * @return  array             Possible array members: day, month, year, hour, minute, second, fixed, format
   807      * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
       
   808      * @throws Zend_Locale_Exception
   767      */
   809      */
   768     private static function _parseDate($date, $options)
   810     private static function _parseDate($date, $options)
   769     {
   811     {
   770         if (!self::_getUniCodeSupport()) {
   812         if (!self::_getUniCodeSupport()) {
   771             trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
   813             trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
   778         $format = $options['date_format'];
   820         $format = $options['date_format'];
   779         $number = $date; // working copy
   821         $number = $date; // working copy
   780         $result['date_format'] = $format; // save the format used to normalize $number (convenience)
   822         $result['date_format'] = $format; // save the format used to normalize $number (convenience)
   781         $result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
   823         $result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
   782 
   824 
   783         $oenc = iconv_get_encoding('internal_encoding');
   825         $oenc = self::_getEncoding();
   784         iconv_set_encoding('internal_encoding', 'UTF-8');
   826         self::_setEncoding('UTF-8');
   785         $day   = iconv_strpos($format, 'd');
   827         $day   = iconv_strpos($format, 'd');
   786         $month = iconv_strpos($format, 'M');
   828         $month = iconv_strpos($format, 'M');
   787         $year  = iconv_strpos($format, 'y');
   829         $year  = iconv_strpos($format, 'y');
   788         $hour  = iconv_strpos($format, 'H');
   830         $hour  = iconv_strpos($format, 'H');
   789         $min   = iconv_strpos($format, 'm');
   831         $min   = iconv_strpos($format, 'm');
   844         if ($sec !== false) {
   886         if ($sec !== false) {
   845             $parse[$sec] = 's';
   887             $parse[$sec] = 's';
   846         }
   888         }
   847 
   889 
   848         if (empty($parse)) {
   890         if (empty($parse)) {
   849             iconv_set_encoding('internal_encoding', $oenc);
   891             self::_setEncoding($oenc);
   850             require_once 'Zend/Locale/Exception.php';
   892             require_once 'Zend/Locale/Exception.php';
   851             throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
   893             throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
   852         }
   894         }
   853         ksort($parse);
   895         ksort($parse);
   854 
   896 
   864         // split number parts
   906         // split number parts
   865         $split = false;
   907         $split = false;
   866         preg_match_all('/\d+/u', $number, $splitted);
   908         preg_match_all('/\d+/u', $number, $splitted);
   867 
   909 
   868         if (count($splitted[0]) == 0) {
   910         if (count($splitted[0]) == 0) {
   869             iconv_set_encoding('internal_encoding', $oenc);
   911             self::_setEncoding($oenc);
   870             require_once 'Zend/Locale/Exception.php';
   912             require_once 'Zend/Locale/Exception.php';
   871             throw new Zend_Locale_Exception("No date part in '$date' found.");
   913             throw new Zend_Locale_Exception("No date part in '$date' found.");
   872         }
   914         }
   873         if (count($splitted[0]) == 1) {
   915         if (count($splitted[0]) == 1) {
   874             $split = 0;
   916             $split = 0;
   970             // fix false month
  1012             // fix false month
   971             if (isset($result['day']) and isset($result['month'])) {
  1013             if (isset($result['day']) and isset($result['month'])) {
   972                 if (($position !== false) and ((iconv_strpos($date, $result['day']) === false) or
  1014                 if (($position !== false) and ((iconv_strpos($date, $result['day']) === false) or
   973                                                (isset($result['year']) and (iconv_strpos($date, $result['year']) === false)))) {
  1015                                                (isset($result['year']) and (iconv_strpos($date, $result['year']) === false)))) {
   974                     if ($options['fix_date'] !== true) {
  1016                     if ($options['fix_date'] !== true) {
   975                         iconv_set_encoding('internal_encoding', $oenc);
  1017                         self::_setEncoding($oenc);
   976                         require_once 'Zend/Locale/Exception.php';
  1018                         require_once 'Zend/Locale/Exception.php';
   977                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '" . $format
  1019                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '" . $format
   978                             . "' (false month, $position, $month)");
  1020                             . "' (false month, $position, $month)");
   979                     }
  1021                     }
   980                     $temp = $result['day'];
  1022                     $temp = $result['day'];
   986 
  1028 
   987             // fix switched values d <> y
  1029             // fix switched values d <> y
   988             if (isset($result['day']) and isset($result['year'])) {
  1030             if (isset($result['day']) and isset($result['year'])) {
   989                 if ($result['day'] > 31) {
  1031                 if ($result['day'] > 31) {
   990                     if ($options['fix_date'] !== true) {
  1032                     if ($options['fix_date'] !== true) {
   991                         iconv_set_encoding('internal_encoding', $oenc);
  1033                         self::_setEncoding($oenc);
   992                         require_once 'Zend/Locale/Exception.php';
  1034                         require_once 'Zend/Locale/Exception.php';
   993                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  1035                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
   994                                                       . $format . "' (d <> y)");
  1036                                                       . $format . "' (d <> y)");
   995                     }
  1037                     }
   996                     $temp = $result['year'];
  1038                     $temp = $result['year'];
  1002 
  1044 
  1003             // fix switched values M <> y
  1045             // fix switched values M <> y
  1004             if (isset($result['month']) and isset($result['year'])) {
  1046             if (isset($result['month']) and isset($result['year'])) {
  1005                 if ($result['month'] > 31) {
  1047                 if ($result['month'] > 31) {
  1006                     if ($options['fix_date'] !== true) {
  1048                     if ($options['fix_date'] !== true) {
  1007                         iconv_set_encoding('internal_encoding', $oenc);
  1049                         self::_setEncoding($oenc);
  1008                         require_once 'Zend/Locale/Exception.php';
  1050                         require_once 'Zend/Locale/Exception.php';
  1009                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  1051                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  1010                                                       . $format . "' (M <> y)");
  1052                                                       . $format . "' (M <> y)");
  1011                     }
  1053                     }
  1012                     $temp = $result['year'];
  1054                     $temp = $result['year'];
  1018 
  1060 
  1019             // fix switched values M <> d
  1061             // fix switched values M <> d
  1020             if (isset($result['month']) and isset($result['day'])) {
  1062             if (isset($result['month']) and isset($result['day'])) {
  1021                 if ($result['month'] > 12) {
  1063                 if ($result['month'] > 12) {
  1022                     if ($options['fix_date'] !== true || $result['month'] > 31) {
  1064                     if ($options['fix_date'] !== true || $result['month'] > 31) {
  1023                         iconv_set_encoding('internal_encoding', $oenc);
  1065                         self::_setEncoding($oenc);
  1024                         require_once 'Zend/Locale/Exception.php';
  1066                         require_once 'Zend/Locale/Exception.php';
  1025                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  1067                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  1026                                                       . $format . "' (M <> d)");
  1068                                                       . $format . "' (M <> d)");
  1027                     }
  1069                     }
  1028                     $temp = $result['day'];
  1070                     $temp = $result['day'];
  1045                     $result['year'] = (int) $result['year'] + 1900;
  1087                     $result['year'] = (int) $result['year'] + 1900;
  1046                 }
  1088                 }
  1047             }
  1089             }
  1048         }
  1090         }
  1049 
  1091 
  1050         iconv_set_encoding('internal_encoding', $oenc);
  1092         self::_setEncoding($oenc);
  1051         return $result;
  1093         return $result;
  1052     }
  1094     }
  1053 
  1095 
  1054     /**
  1096     /**
  1055      * Search $number for a month name found in $monthlist, and replace if found.
  1097      * Search $number for a month name found in $monthlist, and replace if found.
  1179     }
  1221     }
  1180 
  1222 
  1181     /**
  1223     /**
  1182      * Returns the default time format for $locale.
  1224      * Returns the default time format for $locale.
  1183      *
  1225      *
  1184      * @param  string|Zend_Locale  $locale  OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1226      * @param  string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1185      * @return string  format
  1227      * @return string  format
       
  1228      * @throws Zend_Locale_Exception
  1186      */
  1229      */
  1187     public static function getTimeFormat($locale = null)
  1230     public static function getTimeFormat($locale = null)
  1188     {
  1231     {
  1189         $format = Zend_Locale_Data::getContent($locale, 'time');
  1232         $format = Zend_Locale_Data::getContent($locale, 'time');
  1190         if (empty($format)) {
  1233         if (empty($format)) {
  1194         return $format;
  1237         return $format;
  1195     }
  1238     }
  1196 
  1239 
  1197     /**
  1240     /**
  1198      * Returns an array with 'hour', 'minute', and 'second' elements extracted from $time
  1241      * Returns an array with 'hour', 'minute', and 'second' elements extracted from $time
  1199      * according to the order described in $format.  For a format of 'H:m:s', and
  1242      * according to the order described in $format.  For a format of 'H:i:s', and
  1200      * an input of 11:20:55, getTime() would return:
  1243      * an input of 11:20:55, getTime() would return:
  1201      * array ('hour' => 11, 'minute' => 20, 'second' => 55)
  1244      * array ('hour' => 11, 'minute' => 20, 'second' => 55)
  1202      * The optional $locale parameter may be used to help extract times from strings
  1245      * The optional $locale parameter may be used to help extract times from strings
  1203      * containing both a time and a day or month name.
  1246      * containing both a time and a day or month name.
  1204      *
  1247      *
  1217     }
  1260     }
  1218 
  1261 
  1219     /**
  1262     /**
  1220      * Returns the default datetime format for $locale.
  1263      * Returns the default datetime format for $locale.
  1221      *
  1264      *
  1222      * @param  string|Zend_Locale  $locale  OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1265      * @param  string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1223      * @return string  format
  1266      * @return string  format
       
  1267      * @throws Zend_Locale_Exception
  1224      */
  1268      */
  1225     public static function getDateTimeFormat($locale = null)
  1269     public static function getDateTimeFormat($locale = null)
  1226     {
  1270     {
  1227         $format = Zend_Locale_Data::getContent($locale, 'datetime');
  1271         $format = Zend_Locale_Data::getContent($locale, 'datetime');
  1228         if (empty($format)) {
  1272         if (empty($format)) {
  1232         return $format;
  1276         return $format;
  1233     }
  1277     }
  1234 
  1278 
  1235     /**
  1279     /**
  1236      * Returns an array with 'year', 'month', 'day', 'hour', 'minute', and 'second' elements
  1280      * Returns an array with 'year', 'month', 'day', 'hour', 'minute', and 'second' elements
  1237      * extracted from $datetime according to the order described in $format.  For a format of 'd.M.y H:m:s',
  1281      * extracted from $datetime according to the order described in $format.  For a format of 'd.M.y H:i:s',
  1238      * and an input of 10.05.1985 11:20:55, getDateTime() would return:
  1282      * and an input of 10.05.1985 11:20:55, getDateTime() would return:
  1239      * array ('year' => 1985, 'month' => 5, 'day' => 10, 'hour' => 11, 'minute' => 20, 'second' => 55)
  1283      * array ('year' => 1985, 'month' => 5, 'day' => 10, 'hour' => 11, 'minute' => 20, 'second' => 55)
  1240      * The optional $locale parameter may be used to help extract times from strings
  1284      * The optional $locale parameter may be used to help extract times from strings
  1241      * containing both a time and a day or month name.
  1285      * containing both a time and a day or month name.
  1242      *
  1286      *
  1262      */
  1306      */
  1263     protected static function _getUniCodeSupport()
  1307     protected static function _getUniCodeSupport()
  1264     {
  1308     {
  1265         return (@preg_match('/\pL/u', 'a')) ? true : false;
  1309         return (@preg_match('/\pL/u', 'a')) ? true : false;
  1266     }
  1310     }
       
  1311 
       
  1312     /**
       
  1313      * Internal method to retrieve the current encoding via the ini setting
       
  1314      * default_charset for PHP >= 5.6 or iconv_get_encoding otherwise.
       
  1315      *
       
  1316      * @return string
       
  1317      */
       
  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 
       
  1327     /**
       
  1328      * Internal method to set the encoding via the ini setting
       
  1329      * default_charset for PHP >= 5.6 or iconv_set_encoding otherwise.
       
  1330      *
       
  1331      * @param string $encoding
       
  1332      * @return void
       
  1333      */
       
  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     }
  1267 }
  1342 }