4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Intl\DateFormatter\DateFormat
;
14 use Symfony\Component\Intl\Exception\NotImplementedException
;
17 * Parser and formatter for time zone format
19 * @author Igor Wiedler <igor@wiedler.ch>
21 class TimeZoneTransformer
extends Transformer
26 * @throws NotImplementedException When time zone is different than UTC or GMT (Etc/GMT)
28 public function format(\DateTime
$dateTime, $length)
30 $timeZone = substr($dateTime->getTimezone()->getName(), 0, 3);
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.');
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';
39 return $dateTime->format($format);
45 public function getReverseMatchingRegExp($length)
47 return 'GMT[+-]\d{2}:?\d{2}';
53 public function extractDateOptions($matched, $length)
56 'timezone' => self
::getEtcTimeZoneId($matched)
61 * Get an Etc/GMT timezone identifier for the specified timezone
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).
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.
70 * @param string $formattedTimeZone A GMT timezone string (GMT-03:00, e.g.)
72 * @return string A timezone identifier
74 * @see http://php.net/manual/en/timezones.others.php
75 * @see http://www.twinsun.com/tz/tz-link.htm
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
80 public static function getEtcTimeZoneId($formattedTimeZone)
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'] == '-' ? '+' : '-';
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.',
94 return 'Etc/GMT'.($hours !== 0 ? $signal.$hours : '');
97 throw new \
InvalidArgumentException('The GMT time zone \'%s\' does not match with the supported formats GMT[+-]HH:MM or GMT[+-]HHMM.');