]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/TimeZoneTransformer.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / intl / Symfony / Component / Intl / DateFormatter / DateFormat / TimeZoneTransformer.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Intl\DateFormatter\DateFormat;
13
14 use Symfony\Component\Intl\Exception\NotImplementedException;
15
16 /**
17 * Parser and formatter for time zone format
18 *
19 * @author Igor Wiedler <igor@wiedler.ch>
20 */
21 class TimeZoneTransformer extends Transformer
22 {
23 /**
24 * {@inheritDoc}
25 *
26 * @throws NotImplementedException When time zone is different than UTC or GMT (Etc/GMT)
27 */
28 public function format(\DateTime $dateTime, $length)
29 {
30 $timeZone = substr($dateTime->getTimezone()->getName(), 0, 3);
31
32 if (!in_array($timeZone, array('Etc', 'UTC'))) {
33 throw new NotImplementedException('Time zone different than GMT or UTC is not supported as a formatting output.');
34 }
35
36 // From ICU >= 4.8, the zero offset is not more used, example: GMT instead of GMT+00:00
37 $format = (0 !== (int) $dateTime->format('O')) ? '\G\M\TP' : '\G\M\T';
38
39 return $dateTime->format($format);
40 }
41
42 /**
43 * {@inheritDoc}
44 */
45 public function getReverseMatchingRegExp($length)
46 {
47 return 'GMT[+-]\d{2}:?\d{2}';
48 }
49
50 /**
51 * {@inheritDoc}
52 */
53 public function extractDateOptions($matched, $length)
54 {
55 return array(
56 'timezone' => self::getEtcTimeZoneId($matched)
57 );
58 }
59
60 /**
61 * Get an Etc/GMT timezone identifier for the specified timezone
62 *
63 * The PHP documentation for timezones states to not use the 'Other' time zones because them exists
64 * "for backwards compatibility". However all Etc/GMT time zones are in the tz database 'etcetera' file,
65 * which indicates they are not deprecated (neither are old names).
66 *
67 * Only GMT, Etc/Universal, Etc/Zulu, Etc/Greenwich, Etc/GMT-0, Etc/GMT+0 and Etc/GMT0 are old names and
68 * are linked to Etc/GMT or Etc/UTC.
69 *
70 * @param string $formattedTimeZone A GMT timezone string (GMT-03:00, e.g.)
71 *
72 * @return string A timezone identifier
73 *
74 * @see http://php.net/manual/en/timezones.others.php
75 * @see http://www.twinsun.com/tz/tz-link.htm
76 *
77 * @throws NotImplementedException When the GMT time zone have minutes offset different than zero
78 * @throws \InvalidArgumentException When the value can not be matched with pattern
79 */
80 public static function getEtcTimeZoneId($formattedTimeZone)
81 {
82 if (preg_match('/GMT(?P<signal>[+-])(?P<hours>\d{2}):?(?P<minutes>\d{2})/', $formattedTimeZone, $matches)) {
83 $hours = (int) $matches['hours'];
84 $minutes = (int) $matches['minutes'];
85 $signal = $matches['signal'] == '-' ? '+' : '-';
86
87 if (0 < $minutes) {
88 throw new NotImplementedException(sprintf(
89 'It is not possible to use a GMT time zone with minutes offset different than zero (0). GMT time zone tried: %s.',
90 $formattedTimeZone
91 ));
92 }
93
94 return 'Etc/GMT'.($hours !== 0 ? $signal.$hours : '');
95 }
96
97 throw new \InvalidArgumentException('The GMT time zone \'%s\' does not match with the supported formats GMT[+-]HH:MM or GMT[+-]HHMM.');
98 }
99 }