ErrorParser
11 months ago
Parser
11 months ago
Serializer
11 months ago
AbstractModel.php
11 months ago
ApiProvider.php
11 months ago
DateTimeResult.php
11 months ago
DocModel.php
11 months ago
ListShape.php
11 months ago
MapShape.php
11 months ago
Operation.php
11 months ago
Service.php
11 months ago
Shape.php
11 months ago
ShapeMap.php
11 months ago
StructureShape.php
11 months ago
TimestampShape.php
11 months ago
Validator.php
11 months ago
DateTimeResult.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Aws\Api; |
| 4 | |
| 5 | use Aws\Api\Parser\Exception\ParserException; |
| 6 | use DateTime; |
| 7 | use DateTimeZone; |
| 8 | use Exception; |
| 9 | |
| 10 | /** |
| 11 | * DateTime overrides that make DateTime work more seamlessly as a string, |
| 12 | * with JSON documents, and with JMESPath. |
| 13 | */ |
| 14 | class DateTimeResult extends \DateTime implements \JsonSerializable |
| 15 | { |
| 16 | /** |
| 17 | * Create a new DateTimeResult from a unix timestamp. |
| 18 | * The Unix epoch (or Unix time or POSIX time or Unix |
| 19 | * timestamp) is the number of seconds that have elapsed since |
| 20 | * January 1, 1970 (midnight UTC/GMT). |
| 21 | * |
| 22 | * @return DateTimeResult |
| 23 | * @throws Exception |
| 24 | */ |
| 25 | public static function fromEpoch($unixTimestamp) |
| 26 | { |
| 27 | if (!is_numeric($unixTimestamp)) { |
| 28 | throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromEpoch'); |
| 29 | } |
| 30 | |
| 31 | // PHP 5.5 does not support sub-second precision |
| 32 | if (\PHP_VERSION_ID < 56000) { |
| 33 | return new self(gmdate('c', $unixTimestamp)); |
| 34 | } |
| 35 | |
| 36 | $decimalSeparator = isset(localeconv()['decimal_point']) ? localeconv()['decimal_point'] : "."; |
| 37 | $formatString = "U" . $decimalSeparator . "u"; |
| 38 | $dateTime = DateTime::createFromFormat( |
| 39 | $formatString, |
| 40 | sprintf('%0.6f', $unixTimestamp), |
| 41 | new DateTimeZone('UTC') |
| 42 | ); |
| 43 | |
| 44 | if (false === $dateTime) { |
| 45 | throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromEpoch'); |
| 46 | } |
| 47 | |
| 48 | return new self( |
| 49 | $dateTime->format('Y-m-d H:i:s.u'), |
| 50 | new DateTimeZone('UTC') |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return DateTimeResult |
| 56 | */ |
| 57 | public static function fromISO8601($iso8601Timestamp) |
| 58 | { |
| 59 | if (is_numeric($iso8601Timestamp) || !is_string($iso8601Timestamp)) { |
| 60 | throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromISO8601'); |
| 61 | } |
| 62 | |
| 63 | return new DateTimeResult($iso8601Timestamp); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Create a new DateTimeResult from an unknown timestamp. |
| 68 | * |
| 69 | * @return DateTimeResult |
| 70 | * @throws Exception |
| 71 | */ |
| 72 | public static function fromTimestamp($timestamp, $expectedFormat = null) |
| 73 | { |
| 74 | if (empty($timestamp)) { |
| 75 | return self::fromEpoch(0); |
| 76 | } |
| 77 | |
| 78 | if (!(is_string($timestamp) || is_numeric($timestamp))) { |
| 79 | throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp'); |
| 80 | } |
| 81 | |
| 82 | try { |
| 83 | if ($expectedFormat == 'iso8601') { |
| 84 | try { |
| 85 | return self::fromISO8601($timestamp); |
| 86 | } catch (Exception $exception) { |
| 87 | return self::fromEpoch($timestamp); |
| 88 | } |
| 89 | } else if ($expectedFormat == 'unixTimestamp') { |
| 90 | try { |
| 91 | return self::fromEpoch($timestamp); |
| 92 | } catch (Exception $exception) { |
| 93 | return self::fromISO8601($timestamp); |
| 94 | } |
| 95 | } else if (\Aws\is_valid_epoch($timestamp)) { |
| 96 | return self::fromEpoch($timestamp); |
| 97 | } |
| 98 | return self::fromISO8601($timestamp); |
| 99 | } catch (Exception $exception) { |
| 100 | throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp'); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Serialize the DateTimeResult as an ISO 8601 date string. |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
| 109 | public function __toString() |
| 110 | { |
| 111 | return $this->format('c'); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Serialize the date as an ISO 8601 date when serializing as JSON. |
| 116 | * |
| 117 | * @return string |
| 118 | */ |
| 119 | #[\ReturnTypeWillChange] |
| 120 | public function jsonSerialize() |
| 121 | { |
| 122 | return (string) $this; |
| 123 | } |
| 124 | } |
| 125 |