Hour2400Transformer.php 1.3 KB

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