Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/DateTimeResult.php

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