Hour2401Transformer.php 1.4 KB

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