Hour1200Transformer.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /**
  12. * Parser and formatter for 12 hour format (0-11)
  13. *
  14. * @author Igor Wiedler <igor@wiedler.ch>
  15. */
  16. class Hour1200Transformer extends HourTransformer
  17. {
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public function format(\DateTime $dateTime, $length)
  22. {
  23. $hourOfDay = $dateTime->format('g');
  24. $hourOfDay = ('12' == $hourOfDay) ? '0' : $hourOfDay;
  25. return $this->padLeft($hourOfDay, $length);
  26. }
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function normalizeHour($hour, $marker = null)
  31. {
  32. if ('PM' === $marker) {
  33. $hour += 12;
  34. }
  35. return $hour;
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function getReverseMatchingRegExp($length)
  41. {
  42. return '\d{1,2}';
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function extractDateOptions($matched, $length)
  48. {
  49. return array(
  50. 'hour' => (int) $matched,
  51. 'hourInstance' => $this
  52. );
  53. }
  54. }