示例#1
0
 /**
  * Returns the calculated month
  *
  * @param  string                          $calc    Calculation to make
  * @param  string|integer|array|\Zend\Date\Date  $month   Month to calculate with, if null the actual month is taken
  * @param  string|\Zend\Locale\Locale              $locale  Locale for parsing input
  * @return integer|\Zend\Date\Date  new time
  * @throws \Zend\Date\Exception
  */
 private function _month($calc, $month, $locale)
 {
     if ($month === null) {
         throw new Exception\InvalidArgumentException('parameter $month must be set, null is not allowed');
     }
     if ($locale === null) {
         $locale = $this->getLocale();
     }
     if ($month instanceof Date) {
         // extract month from object
         $found = $month->toString(self::MONTH_SHORT, 'iso', $locale);
     } else {
         if (is_numeric($month)) {
             $found = $month;
         } else {
             if (is_array($month)) {
                 if (isset($month['month']) === true) {
                     $month = $month['month'];
                 } else {
                     throw new Exception\InvalidArgumentException("no month given in array");
                 }
             } else {
                 $monthlist = Cldr::getList($locale, 'month');
                 $monthlist2 = Cldr::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
                 $monthlist = array_merge($monthlist, $monthlist2);
                 $found = 0;
                 $cnt = 0;
                 foreach ($monthlist as $key => $value) {
                     if (strtoupper($value) == strtoupper($month)) {
                         $found = $key % 12 + 1;
                         break;
                     }
                     ++$cnt;
                 }
                 if ($found == 0) {
                     foreach ($monthlist2 as $key => $value) {
                         if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
                             $found = $key + 1;
                             break;
                         }
                         ++$cnt;
                     }
                 }
                 if ($found == 0) {
                     throw new Exception\InvalidArgumentException("unknown month name ({$month})");
                 }
             }
         }
     }
     $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
     if ($calc != 'cmp') {
         return $this;
     }
     return $return;
 }
示例#2
0
 /**
  * Returns an array with translated yes strings
  *
  * @param  string|\Zend\Locale\Locale $locale (Optional) Locale for language translation
  * @return array
  */
 public static function getQuestion($locale = null)
 {
     $locale = self::findLocale($locale);
     $quest = Data\Cldr::getList($locale, 'question');
     $yes = explode(':', $quest['yes']);
     $no = explode(':', $quest['no']);
     $quest['yes'] = $yes[0];
     $quest['yesarray'] = $yes;
     $quest['no'] = $no[0];
     $quest['noarray'] = $no;
     $quest['yesexpr'] = self::_prepareQuestionString($yes);
     $quest['noexpr'] = self::_prepareQuestionString($no);
     return $quest;
 }
示例#3
0
 /**
  * test for reading intervalformat from locale
  * expected array
  */
 public function testUnit()
 {
     $value = Cldr::getList('de_AT', 'unit');
     $result = array('day' => array('one' => '{0} Tag', 'other' => '{0} Tage'), 'hour' => array('one' => '{0} Stunde', 'other' => '{0} Stunden'), 'minute' => array('one' => '{0} Minute', 'other' => '{0} Minuten'), 'month' => array('one' => '{0} Monat', 'other' => '{0} Monate'), 'second' => array('one' => '{0} Sekunde', 'other' => '{0} Sekunden'), 'week' => array('one' => '{0} Woche', 'other' => '{0} Wochen'), 'year' => array('one' => '{0} Jahr', 'other' => '{0} Jahre'));
     $this->assertEquals($result, $value);
     $value = Cldr::getContent('de_AT', 'unit', array('day', 'one'));
     $this->assertEquals('{0} Tag', $value);
 }
示例#4
0
文件: Format.php 项目: nsenkevich/zf2
 /**
  * Parse date and split in named array fields
  *
  * @param   string  $date     Date string to parse
  * @param   array   $options  Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  * @throws \Zend\Locale\Exception\InvalidArgumentException
  * @return  array             Possible array members: day, month, year, hour, minute, second, fixed, format
  */
 private static function _parseDate($date, $options)
 {
     if (!self::_getUniCodeSupport()) {
         trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
     }
     if (!is_string($date)) {
         throw new Exception\InvalidArgumentException('Invalid date provided; must be string, ' . gettype($date) . ' provided');
     }
     $options = self::_checkOptions($options) + self::$_options;
     $test = array('h', 'H', 'm', 's', 'y', 'Y', 'M', 'd', 'D', 'E', 'S', 'l', 'B', 'I', 'X', 'r', 'U', 'G', 'w', 'e', 'a', 'A', 'Z', 'z', 'v');
     $format = $options['date_format'];
     $number = $date;
     // working copy
     $result['date_format'] = $format;
     // save the format used to normalize $number (convenience)
     $result['locale'] = $options['locale'];
     // save the locale used to normalize $number (convenience)
     $oenc = iconv_get_encoding('internal_encoding');
     iconv_set_encoding('internal_encoding', 'UTF-8');
     $day = iconv_strpos($format, 'd');
     $month = iconv_strpos($format, 'M');
     $year = iconv_strpos($format, 'y');
     $hour = iconv_strpos($format, 'H');
     $min = iconv_strpos($format, 'm');
     $sec = iconv_strpos($format, 's');
     $am = null;
     if ($hour === false) {
         $hour = iconv_strpos($format, 'h');
     }
     if ($year === false) {
         $year = iconv_strpos($format, 'Y');
     }
     if ($day === false) {
         $day = iconv_strpos($format, 'E');
         if ($day === false) {
             $day = iconv_strpos($format, 'D');
         }
     }
     if ($day !== false) {
         $parse[$day] = 'd';
         if (!empty($options['locale']) && $options['locale'] !== 'root' && (!is_object($options['locale']) || (string) $options['locale'] !== 'root')) {
             // erase day string
             $daylist = Cldr::getList($options['locale'], 'day');
             if (is_scalar($number)) {
                 foreach ($daylist as $key => $name) {
                     if (iconv_strpos($number, $name) !== false) {
                         $number = str_replace($name, "EEEE", $number);
                         break;
                     }
                 }
             }
         }
     }
     $position = false;
     if ($month !== false) {
         $parse[$month] = 'M';
         if (!empty($options['locale']) && $options['locale'] !== 'root' && (!is_object($options['locale']) || (string) $options['locale'] !== 'root')) {
             // prepare to convert month name to their numeric equivalents, if requested,
             // and we have a $options['locale']
             $position = self::_replaceMonth($number, Cldr::getList($options['locale'], 'month'));
             if ($position === false) {
                 $position = self::_replaceMonth($number, Cldr::getList($options['locale'], 'month', array('gregorian', 'format', 'abbreviated')));
             }
         }
     }
     if ($year !== false) {
         $parse[$year] = 'y';
     }
     if ($hour !== false) {
         $parse[$hour] = 'H';
     }
     if ($min !== false) {
         $parse[$min] = 'm';
     }
     if ($sec !== false) {
         $parse[$sec] = 's';
     }
     if (empty($parse)) {
         iconv_set_encoding('internal_encoding', $oenc);
         throw new Exception\InvalidArgumentException("Unknown date format, neither date nor time in '" . $format . "' found");
     }
     ksort($parse);
     // get daytime
     if (iconv_strpos($format, 'a') !== false) {
         if (iconv_strpos(strtoupper($number), strtoupper(Cldr::getContent($options['locale'], 'am'))) !== false) {
             $am = true;
         } else {
             if (iconv_strpos(strtoupper($number), strtoupper(Cldr::getContent($options['locale'], 'pm'))) !== false) {
                 $am = false;
             }
         }
     }
     // split number parts
     $split = false;
     preg_match_all('/\\d+/u', $number, $splitted);
     if (count($splitted[0]) == 0) {
         iconv_set_encoding('internal_encoding', $oenc);
         throw new Exception\InvalidArgumentException("No date part in '{$date}' found.");
     }
     if (count($splitted[0]) == 1) {
         $split = 0;
     }
     $cnt = 0;
     foreach ($parse as $key => $value) {
         switch ($value) {
             case 'd':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['day'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['day'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
             case 'M':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['month'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['month'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
             case 'y':
                 $length = 2;
                 if (iconv_substr($format, $year, 4) == 'yyyy' || iconv_substr($format, $year, 4) == 'YYYY') {
                     $length = 4;
                 }
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['year'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['year'] = iconv_substr($splitted[0][0], $split, $length);
                     $split += $length;
                 }
                 ++$cnt;
                 break;
             case 'H':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['hour'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['hour'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
             case 'm':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['minute'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['minute'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
             case 's':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['second'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['second'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
         }
     }
     // AM/PM correction
     if ($hour !== false) {
         if ($am === true and $result['hour'] == 12) {
             $result['hour'] = 0;
         } else {
             if ($am === false and $result['hour'] != 12) {
                 $result['hour'] += 12;
             }
         }
     }
     if ($options['fix_date'] === true) {
         $result['fixed'] = 0;
         // nothing has been "fixed" by swapping date parts around (yet)
     }
     if ($day !== false) {
         // fix false month
         if (isset($result['day']) and isset($result['month'])) {
             if ($position !== false and (iconv_strpos($date, $result['day']) === false or isset($result['year']) and iconv_strpos($date, $result['year']) === false)) {
                 if ($options['fix_date'] !== true) {
                     iconv_set_encoding('internal_encoding', $oenc);
                     throw new Exception\InvalidArgumentException("Unable to parse date '{$date}' using '" . $format . "' (false month, {$position}, {$month})");
                 }
                 $temp = $result['day'];
                 $result['day'] = $result['month'];
                 $result['month'] = $temp;
                 $result['fixed'] = 1;
             }
         }
         // fix switched values d <> y
         if (isset($result['day']) and isset($result['year'])) {
             if ($result['day'] > 31) {
                 if ($options['fix_date'] !== true) {
                     iconv_set_encoding('internal_encoding', $oenc);
                     throw new Exception\InvalidArgumentException("Unable to parse date '{$date}' using '" . $format . "' (d <> y)");
                 }
                 $temp = $result['year'];
                 $result['year'] = $result['day'];
                 $result['day'] = $temp;
                 $result['fixed'] = 2;
             }
         }
         // fix switched values M <> y
         if (isset($result['month']) and isset($result['year'])) {
             if ($result['month'] > 31) {
                 if ($options['fix_date'] !== true) {
                     iconv_set_encoding('internal_encoding', $oenc);
                     throw new Exception\InvalidArgumentException("Unable to parse date '{$date}' using '" . $format . "' (M <> y)");
                 }
                 $temp = $result['year'];
                 $result['year'] = $result['month'];
                 $result['month'] = $temp;
                 $result['fixed'] = 3;
             }
         }
         // fix switched values M <> d
         if (isset($result['month']) and isset($result['day'])) {
             if ($result['month'] > 12) {
                 if ($options['fix_date'] !== true || $result['month'] > 31) {
                     iconv_set_encoding('internal_encoding', $oenc);
                     throw new Exception\InvalidArgumentException("Unable to parse date '{$date}' using '" . $format . "' (M <> d)");
                 }
                 $temp = $result['day'];
                 $result['day'] = $result['month'];
                 $result['month'] = $temp;
                 $result['fixed'] = 4;
             }
         }
     }
     if (isset($result['year'])) {
         if (iconv_strlen($result['year']) == 2 && $result['year'] < 10 || (iconv_strpos($format, 'yy') !== false && iconv_strpos($format, 'yyyy') === false || iconv_strpos($format, 'YY') !== false && iconv_strpos($format, 'YYYY') === false)) {
             if ($result['year'] >= 0 && $result['year'] < 100) {
                 if ($result['year'] < 70) {
                     $result['year'] = (int) $result['year'] + 100;
                 }
                 $result['year'] = (int) $result['year'] + 1900;
             }
         }
     }
     iconv_set_encoding('internal_encoding', $oenc);
     return $result;
 }