Hour2401Transformer.php 1.4 KB

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