TimeZoneTransformer.php 3.2KB

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