Hour1201Transformer.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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 (1-12)
  13. *
  14. * @author Igor Wiedler <igor@wiedler.ch>
  15. */
  16. class Hour1201Transformer extends HourTransformer
  17. {
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public function format(\DateTime $dateTime, $length)
  22. {
  23. return $this->padLeft($dateTime->format('g'), $length);
  24. }
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function getNormalizedHour($hour, $marker = null)
  29. {
  30. if ('PM' !== $marker && 12 === $hour) {
  31. $hour = 0;
  32. } elseif ('PM' === $marker && 12 !== $hour) {
  33. // If PM and hour is not 12 (1-12), sum 12 hour
  34. $hour = $hour + 12;
  35. }
  36. return $hour;
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function getReverseMatchingRegExp($length)
  42. {
  43. return '\d{1,2}';
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function extractDateOptions($matched, $length)
  49. {
  50. return array(
  51. 'hour' => (int) $matched,
  52. 'hourInstance' => $this
  53. );
  54. }
  55. }