123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- <?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)
- {
-
- $result = strtr($pattern, [
- "'" => "''''",
-
- '\d' => "'d'",
- 'd' => 'dd',
- '\D' => "'D'",
- 'D' => 'eee',
- '\j' => "'j'",
- 'j' => 'd',
- '\l' => "'l'",
- 'l' => 'eeee',
- '\N' => "'N'",
- 'N' => 'e',
- '\S' => "'S'",
- 'S' => '',
- '\w' => "'w'",
- 'w' => '',
- '\z' => "'z'",
- 'z' => 'D',
-
- '\W' => "'W'",
- 'W' => 'w',
-
- '\F' => "'F'",
- 'F' => 'MMMM',
- '\m' => "'m'",
- 'm' => 'MM',
- '\M' => "'M'",
- 'M' => 'MMM',
- '\n' => "'n'",
- 'n' => 'M',
- '\t' => "'t'",
- 't' => '',
-
- '\L' => "'L'",
- 'L' => '',
- '\o' => "'o'",
- 'o' => 'Y',
- '\Y' => "'Y'",
- 'Y' => 'yyyy',
- '\y' => "'y'",
- 'y' => 'yy',
-
- '\a' => "'a'",
- 'a' => 'a',
- '\A' => "'A'",
- 'A' => 'a',
- '\B' => "'B'",
- 'B' => '',
- '\g' => "'g'",
- 'g' => 'h',
- '\G' => "'G'",
- 'G' => 'H',
- '\h' => "'h'",
- 'h' => 'hh',
- '\H' => "'H'",
- 'H' => 'HH',
- '\i' => "'i'",
- 'i' => 'mm',
- '\s' => "'s'",
- 's' => 'ss',
- '\u' => "'u'",
- 'u' => '',
-
- '\e' => "'e'",
- 'e' => 'VV',
- '\I' => "'I'",
- 'I' => '',
- '\O' => "'O'",
- 'O' => 'xx',
- '\P' => "'P'",
- 'P' => 'xxx',
- '\T' => "'T'",
- 'T' => 'zzz',
- '\Z' => "'Z'",
- 'Z' => '',
-
- '\c' => "'c'",
- 'c' => "yyyy-MM-dd'T'HH:mm:ssxxx",
- '\r' => "'r'",
- 'r' => 'eee, dd MMM yyyy HH:mm:ss xx',
- '\U' => "'U'",
- 'U' => '',
- '\\\\' => '\\',
- ]);
-
-
- return strtr($result, [
- "''''" => "''",
- "''" => '',
- ]);
- }
-
- 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' => '@',
- ]);
- }
- }
|