|
- <?php
-
-
- namespace yii\helpers;
-
- use IntlDateFormatter;
- use Yii;
-
-
- class BaseFormatConverter
- {
-
-
- public static $phpFallbackDatePatterns = [
- 'short' => [
- 'date' => 'n/j/y',
- 'time' => 'H:i',
- 'datetime' => 'n/j/y H:i',
- ],
- 'medium' => [
- 'date' => 'M j, Y',
- 'time' => 'g:i:s A',
- 'datetime' => 'M j, Y g:i:s A',
- ],
- 'long' => [
- 'date' => 'F j, Y',
- 'time' => 'g:i:sA',
- 'datetime' => 'F j, Y g:i:sA',
- ],
- 'full' => [
- 'date' => 'l, F j, Y',
- 'time' => 'g:i:sA T',
- 'datetime' => 'l, F j, Y g:i:sA T',
- ],
- ];
-
-
- public static $juiFallbackDatePatterns = [
- 'short' => [
- 'date' => 'd/m/y',
- 'time' => '',
- 'datetime' => 'd/m/y',
- ],
- 'medium' => [
- 'date' => 'M d, yy',
- 'time' => '',
- 'datetime' => 'M d, yy',
- ],
- 'long' => [
- 'date' => 'MM d, yy',
- 'time' => '',
- 'datetime' => 'MM d, yy',
- ],
- 'full' => [
- 'date' => 'DD, MM d, yy',
- 'time' => '',
- 'datetime' => 'DD, MM d, yy',
- ],
- ];
-
- private static $_icuShortFormats = [
- 'short' => 3,
- 'medium' => 2,
- 'long' => 1,
- 'full' => 0,
- ];
-
-
-
-
- public static function convertDateIcuToPhp($pattern, $type = 'date', $locale = null)
- {
- if (isset(self::$_icuShortFormats[$pattern])) {
- if (extension_loaded('intl')) {
- if ($locale === null) {
- $locale = Yii::$app->language;
- }
- if ($type === 'date') {
- $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE);
- } elseif ($type === 'time') {
- $formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]);
- } else {
- $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]);
- }
- $pattern = $formatter->getPattern();
- } else {
- return static::$phpFallbackDatePatterns[$pattern][$type];
- }
- }
-
-
- $escaped = [];
- if (preg_match_all('/(?<!\')\'(.*?[^\'])\'(?!\')/', $pattern, $matches, PREG_SET_ORDER)) {
- foreach ($matches as $match) {
- $match[1] = str_replace('\'\'', '\'', $match[1]);
- $escaped[$match[0]] = '\\'.implode('\\', preg_split('//u', $match[1], -1, PREG_SPLIT_NO_EMPTY));
- }
- }
- return strtr($pattern, array_merge($escaped, [
- '\'\'' => '\\\'',
- 'G' => '',
- 'Y' => 'o',
- 'y' => 'Y',
- 'yyyy' => 'Y',
- 'yy' => 'y',
- 'u' => '',
- 'U' => '',
- 'r' => '',
- 'Q' => '',
- 'QQ' => '',
- 'QQQ' => '',
- 'QQQQ' => '',
- 'QQQQQ' => '',
- 'q' => '',
- 'qq' => '',
- 'qqq' => '',
- 'qqqq' => '',
- 'qqqqq' => '',
- 'M' => 'n',
- 'MM' => 'm',
- 'MMM' => 'M',
- 'MMMM' => 'F',
- 'MMMMM' => '',
- 'L' => 'n',
- 'LL' => 'm',
- 'LLL' => 'M',
- 'LLLL' => 'F',
- 'LLLLL' => '',
- 'w' => 'W',
- 'ww' => 'W',
- 'W' => '',
- 'd' => 'j',
- 'dd' => 'd',
- 'D' => 'z',
- 'F' => '',
- 'g' => '',
- 'E' => 'D',
- 'EE' => 'D',
- 'EEE' => 'D',
- 'EEEE' => 'l',
- 'EEEEE' => '',
- 'EEEEEE' => '',
- 'e' => 'N',
- 'ee' => 'N',
- 'eee' => 'D',
- 'eeee' => 'l',
- 'eeeee' => '',
- 'eeeeee' => '',
- 'c' => 'N',
- 'cc' => 'N',
- 'ccc' => 'D',
- 'cccc' => 'l',
- 'ccccc' => '',
- 'cccccc' => '',
- 'a' => 'a',
- 'h' => 'g',
- 'hh' => 'h',
- 'H' => 'G',
- 'HH' => 'H',
- 'k' => '',
- 'kk' => '',
- 'K' => '',
- 'KK' => '',
- 'm' => 'i',
- 'mm' => 'i',
- 's' => 's',
- 'ss' => 's',
- 'S' => '',
- 'SS' => '',
- 'SSS' => '',
- 'SSSS' => '',
- 'A' => '',
- 'z' => 'T',
- 'zz' => 'T',
- 'zzz' => 'T',
- 'zzzz' => 'T',
- 'Z' => 'O',
- 'ZZ' => 'O',
- 'ZZZ' => 'O',
- 'ZZZZ' => '\G\M\TP',
- 'ZZZZZ' => '',
- 'O' => '',
- 'OOOO' => '\G\M\TP',
- 'v' => '\G\M\TP',
- 'vvvv' => '\G\M\TP',
- 'V' => '',
- 'VV' => 'e',
- 'VVV' => '',
- 'VVVV' => '\G\M\TP',
- 'X' => '',
- 'XX' => 'O, \Z',
- 'XXX' => 'P, \Z',
- 'XXXX' => '',
- 'XXXXX' => '',
- 'x' => '',
- 'xx' => 'O',
- 'xxx' => 'P',
- 'xxxx' => '',
- 'xxxxx' => '',
- ]));
- }
-
-
-
- public static function convertDatePhpToIcu($pattern)
- {
-
- return strtr($pattern, [
-
- 'd' => 'dd',
- 'D' => 'eee',
- 'j' => 'd',
- 'l' => 'eeee',
- 'N' => 'e',
- 'S' => '',
- 'w' => '',
- 'z' => 'D',
-
- 'W' => 'w',
-
- 'F' => 'MMMM',
- 'm' => 'MM',
- 'M' => 'MMM',
- 'n' => 'M',
- 't' => '',
-
- 'L' => '',
- 'o' => 'Y',
- 'Y' => 'yyyy',
- 'y' => 'yy',
-
- 'a' => 'a',
- 'A' => 'a',
- 'B' => '',
- 'g' => 'h',
- 'G' => 'H',
- 'h' => 'hh',
- 'H' => 'HH',
- 'i' => 'mm',
- 's' => 'ss',
- 'u' => '',
-
- 'e' => 'VV',
- 'I' => '',
- 'O' => 'xx',
- 'P' => 'xxx',
- 'T' => 'zzz',
- 'Z' => '',
-
- 'c' => 'yyyy-MM-dd\'T\'HH:mm:ssxxx',
- 'r' => 'eee, dd MMM yyyy HH:mm:ss xx',
- 'U' => '',
- ]);
- }
-
-
-
- public static function convertDateIcuToJui($pattern, $type = 'date', $locale = null)
- {
- if (isset(self::$_icuShortFormats[$pattern])) {
- if (extension_loaded('intl')) {
- if ($locale === null) {
- $locale = Yii::$app->language;
- }
- if ($type === 'date') {
- $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE);
- } elseif ($type === 'time') {
- $formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]);
- } else {
- $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]);
- }
- $pattern = $formatter->getPattern();
- } else {
- return static::$juiFallbackDatePatterns[$pattern][$type];
- }
- }
-
-
- $escaped = [];
- if (preg_match_all('/(?<!\')\'.*?[^\']\'(?!\')/', $pattern, $matches)) {
- foreach ($matches[0] as $match) {
- $escaped[$match] = $match;
- }
- }
- return strtr($pattern, array_merge($escaped, [
- 'G' => '',
- 'Y' => '',
- 'y' => 'yy',
- 'yyyy' => 'yy',
- 'yy' => 'y',
- 'u' => '',
- 'U' => '',
- 'r' => '',
- 'Q' => '',
- 'QQ' => '',
- 'QQQ' => '',
- 'QQQQ' => '',
- 'QQQQQ' => '',
- 'q' => '',
- 'qq' => '',
- 'qqq' => '',
- 'qqqq' => '',
- 'qqqqq' => '',
- 'M' => 'm',
- 'MM' => 'mm',
- 'MMM' => 'M',
- 'MMMM' => 'MM',
- 'MMMMM' => '',
- 'L' => 'm',
- 'LL' => 'mm',
- 'LLL' => 'M',
- 'LLLL' => 'MM',
- 'LLLLL' => '',
- 'w' => '',
- 'ww' => '',
- 'W' => '',
- 'd' => 'd',
- 'dd' => 'dd',
- 'D' => 'o',
- 'F' => '',
- 'g' => '',
- 'E' => 'D',
- 'EE' => 'D',
- 'EEE' => 'D',
- 'EEEE' => 'DD',
- 'EEEEE' => '',
- 'EEEEEE' => '',
- 'e' => '',
- 'ee' => '',
- 'eee' => 'D',
- 'eeee' => '',
- 'eeeee' => '',
- 'eeeeee' => '',
- 'c' => '',
- 'cc' => '',
- 'ccc' => 'D',
- 'cccc' => 'DD',
- 'ccccc' => '',
- 'cccccc' => '',
- 'a' => '',
- 'h' => '',
- 'hh' => '',
- 'H' => '',
- 'HH' => '',
- 'k' => '',
- 'kk' => '',
- 'K' => '',
- 'KK' => '',
- 'm' => '',
- 'mm' => '',
- 's' => '',
- 'ss' => '',
- 'S' => '',
- 'SS' => '',
- 'SSS' => '',
- 'SSSS' => '',
- 'A' => '',
- 'z' => '',
- 'zz' => '',
- 'zzz' => '',
- 'zzzz' => '',
- 'Z' => '',
- 'ZZ' => '',
- 'ZZZ' => '',
- 'ZZZZ' => '',
- 'ZZZZZ' => '',
- 'O' => '',
- 'OOOO' => '',
- 'v' => '',
- 'vvvv' => '',
- 'V' => '',
- 'VV' => '',
- 'VVV' => '',
- 'VVVV' => '',
- 'X' => '',
- 'XX' => '',
- 'XXX' => '',
- 'XXXX' => '',
- 'XXXXX' => '',
- 'x' => '',
- 'xx' => '',
- 'xxx' => '',
- 'xxxx' => '',
- 'xxxxx' => '',
- ]));
- }
-
-
-
- public static function convertDatePhpToJui($pattern)
- {
-
- return strtr($pattern, [
-
- 'd' => 'dd',
- 'D' => 'D',
- 'j' => 'd',
- 'l' => 'DD',
- 'N' => '',
- 'S' => '',
- 'w' => '',
- 'z' => 'o',
-
- 'W' => '',
-
- 'F' => 'MM',
- 'm' => 'mm',
- 'M' => 'M',
- 'n' => 'm',
- 't' => '',
-
- 'L' => '',
- 'o' => '',
- 'Y' => 'yy',
- 'y' => 'y',
-
- 'a' => '',
- 'A' => '',
- 'B' => '',
- 'g' => '',
- 'G' => '',
- 'h' => '',
- 'H' => '',
- 'i' => '',
- 's' => '',
- 'u' => '',
-
- 'e' => '',
- 'I' => '',
- 'O' => '',
- 'P' => '',
- 'T' => '',
- 'Z' => '',
-
- 'c' => 'yyyy-MM-dd',
- 'r' => 'D, d M yy',
- 'U' => '@',
- ]);
- }
- }
|