DateTimeResult.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Aws\Api;
  3. use Aws\Api\Parser\Exception\ParserException;
  4. use Exception;
  5. /**
  6. * DateTime overrides that make DateTime work more seamlessly as a string,
  7. * with JSON documents, and with JMESPath.
  8. */
  9. class DateTimeResult extends \DateTime implements \JsonSerializable
  10. {
  11. /**
  12. * Create a new DateTimeResult from a unix timestamp.
  13. * The Unix epoch (or Unix time or POSIX time or Unix
  14. * timestamp) is the number of seconds that have elapsed since
  15. * January 1, 1970 (midnight UTC/GMT).
  16. * @param $unixTimestamp
  17. *
  18. * @return DateTimeResult
  19. * @throws Exception
  20. */
  21. public static function fromEpoch($unixTimestamp)
  22. {
  23. return new self(gmdate('c', $unixTimestamp));
  24. }
  25. /**
  26. * @param $iso8601Timestamp
  27. *
  28. * @return DateTimeResult
  29. */
  30. public static function fromISO8601($iso8601Timestamp)
  31. {
  32. if (is_numeric($iso8601Timestamp) || !is_string($iso8601Timestamp)) {
  33. throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromISO8601');
  34. }
  35. return new DateTimeResult($iso8601Timestamp);
  36. }
  37. /**
  38. * Create a new DateTimeResult from an unknown timestamp.
  39. *
  40. * @param $timestamp
  41. *
  42. * @return DateTimeResult
  43. * @throws ParserException|Exception
  44. */
  45. public static function fromTimestamp($timestamp, $expectedFormat = null)
  46. {
  47. if (empty($timestamp)) {
  48. return self::fromEpoch(0);
  49. }
  50. if (!(is_string($timestamp) || is_numeric($timestamp))) {
  51. throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp');
  52. }
  53. try {
  54. if ($expectedFormat == 'iso8601') {
  55. try {
  56. return self::fromISO8601($timestamp);
  57. } catch (Exception $exception) {
  58. return self::fromEpoch($timestamp);
  59. }
  60. } else if ($expectedFormat == 'unixTimestamp') {
  61. try {
  62. return self::fromEpoch($timestamp);
  63. } catch (Exception $exception) {
  64. return self::fromISO8601($timestamp);
  65. }
  66. } else if (\Aws\is_valid_epoch($timestamp)) {
  67. return self::fromEpoch($timestamp);
  68. }
  69. return self::fromISO8601($timestamp);
  70. } catch (Exception $exception) {
  71. throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp');
  72. }
  73. }
  74. /**
  75. * Serialize the DateTimeResult as an ISO 8601 date string.
  76. *
  77. * @return string
  78. */
  79. public function __toString()
  80. {
  81. return $this->format('c');
  82. }
  83. /**
  84. * Serialize the date as an ISO 8601 date when serializing as JSON.
  85. *
  86. * @return mixed|string
  87. */
  88. public function jsonSerialize()
  89. {
  90. return (string) $this;
  91. }
  92. }