/** * 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. * @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); } $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 = IfwPsn_Vendor_Zend_Locale_Data::getList($options['locale'], 'day'); 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, IfwPsn_Vendor_Zend_Locale_Data::getList($options['locale'], 'month')); if ($position === false) { $position = self::_replaceMonth($number, IfwPsn_Vendor_Zend_Locale_Data::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); require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Exception.php'; throw new IfwPsn_Vendor_Zend_Locale_Exception("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(IfwPsn_Vendor_Zend_Locale_Data::getContent($options['locale'], 'am'))) !== false) { $am = true; } else { if (iconv_strpos(strtoupper($number), strtoupper(IfwPsn_Vendor_Zend_Locale_Data::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); require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Exception.php'; throw new IfwPsn_Vendor_Zend_Locale_Exception("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); require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Exception.php'; throw new IfwPsn_Vendor_Zend_Locale_Exception("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); require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Exception.php'; throw new IfwPsn_Vendor_Zend_Locale_Exception("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); require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Exception.php'; throw new IfwPsn_Vendor_Zend_Locale_Exception("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); require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Exception.php'; throw new IfwPsn_Vendor_Zend_Locale_Exception("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; }
/** * Returns the calculated month * * @param string $calc Calculation to make * @param string|integer|array|IfwPsn_Vendor_Zend_Date $month Month to calculate with, if null the actual month is taken * @param string|IfwPsn_Vendor_Zend_Locale $locale Locale for parsing input * @return integer|IfwPsn_Vendor_Zend_Date new time * @throws IfwPsn_Vendor_Zend_Date_Exception */ private function _month($calc, $month, $locale) { if ($month === null) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Date/Exception.php'; throw new IfwPsn_Vendor_Zend_Date_Exception('parameter $month must be set, null is not allowed'); } if ($locale === null) { $locale = $this->getLocale(); } if ($month instanceof IfwPsn_Vendor_Zend_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 { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Date/Exception.php'; throw new IfwPsn_Vendor_Zend_Date_Exception("no month given in array"); } } else { $monthlist = IfwPsn_Vendor_Zend_Locale_Data::getList($locale, 'month'); $monthlist2 = IfwPsn_Vendor_Zend_Locale_Data::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) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Date/Exception.php'; throw new IfwPsn_Vendor_Zend_Date_Exception("unknown month name ({$month})", 0, null, $month); } } } } $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale); if ($calc != 'cmp') { return $this; } return $return; }
/** * Returns an array with translated yes strings * * @param string|IfwPsn_Vendor_Zend_Locale $locale (Optional) Locale for language translation (defaults to $this locale) * @return array */ public static function getQuestion($locale = null) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Data.php'; $locale = self::findLocale($locale); $quest = IfwPsn_Vendor_Zend_Locale_Data::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; }