| 1 |
efrain |
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 |
{
|
| 1441 |
ariadna |
16 |
private const ISO8601_NANOSECOND_REGEX = '/^(.*\.\d{6})(\d{1,3})(Z|[+-]\d{2}:\d{2})?$/';
|
|
|
17 |
|
| 1 |
efrain |
18 |
/**
|
|
|
19 |
* Create a new DateTimeResult from a unix timestamp.
|
|
|
20 |
* The Unix epoch (or Unix time or POSIX time or Unix
|
|
|
21 |
* timestamp) is the number of seconds that have elapsed since
|
|
|
22 |
* January 1, 1970 (midnight UTC/GMT).
|
|
|
23 |
*
|
|
|
24 |
* @return DateTimeResult
|
|
|
25 |
* @throws Exception
|
|
|
26 |
*/
|
|
|
27 |
public static function fromEpoch($unixTimestamp)
|
|
|
28 |
{
|
|
|
29 |
if (!is_numeric($unixTimestamp)) {
|
|
|
30 |
throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromEpoch');
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
// PHP 5.5 does not support sub-second precision
|
|
|
34 |
if (\PHP_VERSION_ID < 56000) {
|
|
|
35 |
return new self(gmdate('c', $unixTimestamp));
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
$decimalSeparator = isset(localeconv()['decimal_point']) ? localeconv()['decimal_point'] : ".";
|
|
|
39 |
$formatString = "U" . $decimalSeparator . "u";
|
|
|
40 |
$dateTime = DateTime::createFromFormat(
|
|
|
41 |
$formatString,
|
|
|
42 |
sprintf('%0.6f', $unixTimestamp),
|
|
|
43 |
new DateTimeZone('UTC')
|
|
|
44 |
);
|
|
|
45 |
|
|
|
46 |
if (false === $dateTime) {
|
|
|
47 |
throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromEpoch');
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
return new self(
|
|
|
51 |
$dateTime->format('Y-m-d H:i:s.u'),
|
|
|
52 |
new DateTimeZone('UTC')
|
|
|
53 |
);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* @return DateTimeResult
|
|
|
58 |
*/
|
|
|
59 |
public static function fromISO8601($iso8601Timestamp)
|
|
|
60 |
{
|
|
|
61 |
if (is_numeric($iso8601Timestamp) || !is_string($iso8601Timestamp)) {
|
|
|
62 |
throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromISO8601');
|
|
|
63 |
}
|
|
|
64 |
|
| 1441 |
ariadna |
65 |
// Prior to 8.0.10, nanosecond precision is not supported
|
|
|
66 |
// Reduces to microsecond precision if nanosecond precision is detected
|
|
|
67 |
if (PHP_VERSION_ID < 80010
|
|
|
68 |
&& preg_match(self::ISO8601_NANOSECOND_REGEX, $iso8601Timestamp, $matches)
|
|
|
69 |
) {
|
|
|
70 |
$iso8601Timestamp = $matches[1] . ($matches[3] ?? '');
|
|
|
71 |
}
|
|
|
72 |
|
| 1 |
efrain |
73 |
return new DateTimeResult($iso8601Timestamp);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Create a new DateTimeResult from an unknown timestamp.
|
|
|
78 |
*
|
|
|
79 |
* @return DateTimeResult
|
|
|
80 |
* @throws Exception
|
|
|
81 |
*/
|
|
|
82 |
public static function fromTimestamp($timestamp, $expectedFormat = null)
|
|
|
83 |
{
|
|
|
84 |
if (empty($timestamp)) {
|
|
|
85 |
return self::fromEpoch(0);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
if (!(is_string($timestamp) || is_numeric($timestamp))) {
|
|
|
89 |
throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp');
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
try {
|
|
|
93 |
if ($expectedFormat == 'iso8601') {
|
|
|
94 |
try {
|
|
|
95 |
return self::fromISO8601($timestamp);
|
|
|
96 |
} catch (Exception $exception) {
|
|
|
97 |
return self::fromEpoch($timestamp);
|
|
|
98 |
}
|
|
|
99 |
} else if ($expectedFormat == 'unixTimestamp') {
|
|
|
100 |
try {
|
|
|
101 |
return self::fromEpoch($timestamp);
|
|
|
102 |
} catch (Exception $exception) {
|
|
|
103 |
return self::fromISO8601($timestamp);
|
|
|
104 |
}
|
|
|
105 |
} else if (\Aws\is_valid_epoch($timestamp)) {
|
|
|
106 |
return self::fromEpoch($timestamp);
|
|
|
107 |
}
|
|
|
108 |
return self::fromISO8601($timestamp);
|
|
|
109 |
} catch (Exception $exception) {
|
|
|
110 |
throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp');
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Serialize the DateTimeResult as an ISO 8601 date string.
|
|
|
116 |
*
|
|
|
117 |
* @return string
|
|
|
118 |
*/
|
|
|
119 |
public function __toString()
|
|
|
120 |
{
|
|
|
121 |
return $this->format('c');
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Serialize the date as an ISO 8601 date when serializing as JSON.
|
|
|
126 |
*
|
|
|
127 |
* @return string
|
|
|
128 |
*/
|
|
|
129 |
#[\ReturnTypeWillChange]
|
|
|
130 |
public function jsonSerialize()
|
|
|
131 |
{
|
|
|
132 |
return (string) $this;
|
|
|
133 |
}
|
|
|
134 |
}
|