PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / s3 / Aws / Api / DateTimeResult.php

DateTimeResult.php in Media Cloud Sync 1.4.2, at includes/sdk/s3/Aws/Api/DateTimeResult.php

116 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\Api;
4
5 use Dudlewebs\WPMCS\s3\Aws\Api\Parser\Exception\ParserException;
6 use DateTime;
7 use DateTimeZone;
8 use Exception;
9 /**
10 * DateTime overrides that make DateTime work more seamlessly as a string,
11 * with JSON documents, and with JMESPath.
12 */
13 class DateTimeResult extends \DateTime implements \JsonSerializable
14 {
15 private const ISO8601_NANOSECOND_REGEX = '/^(.*\\.\\d{6})(\\d{1,3})(Z|[+-]\\d{2}:\\d{2})?$/';
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 // PHP 5.5 does not support sub-second precision
31 if (\PHP_VERSION_ID < 56000) {
32 return new self(\gmdate('c', $unixTimestamp));
33 }
34 $decimalSeparator = isset(\localeconv()['decimal_point']) ? \localeconv()['decimal_point'] : ".";
35 $formatString = "U" . $decimalSeparator . "u";
36 $dateTime = DateTime::createFromFormat($formatString, \sprintf('%0.6f', $unixTimestamp), new DateTimeZone('UTC'));
37 if (\false === $dateTime) {
38 throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromEpoch');
39 }
40 return new self($dateTime->format('Y-m-d H:i:s.u'), new DateTimeZone('UTC'));
41 }
42 /**
43 * @return DateTimeResult
44 */
45 public static function fromISO8601($iso8601Timestamp)
46 {
47 if (\is_numeric($iso8601Timestamp) || !\is_string($iso8601Timestamp)) {
48 throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromISO8601');
49 }
50 // Prior to 8.0.10, nanosecond precision is not supported
51 // Reduces to microsecond precision if nanosecond precision is detected
52 if (\PHP_VERSION_ID < 80010 && \preg_match(self::ISO8601_NANOSECOND_REGEX, $iso8601Timestamp, $matches)) {
53 $iso8601Timestamp = $matches[1] . ($matches[3] ?? '');
54 }
55 return new DateTimeResult($iso8601Timestamp);
56 }
57 /**
58 * Create a new DateTimeResult from an unknown timestamp.
59 *
60 * @return DateTimeResult
61 * @throws Exception
62 */
63 public static function fromTimestamp($timestamp, $expectedFormat = null)
64 {
65 if (empty($timestamp)) {
66 return self::fromEpoch(0);
67 }
68 if (!(\is_string($timestamp) || \is_numeric($timestamp))) {
69 throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp');
70 }
71 try {
72 if ($expectedFormat == 'iso8601') {
73 try {
74 return self::fromISO8601($timestamp);
75 } catch (Exception $exception) {
76 return self::fromEpoch($timestamp);
77 }
78 } else {
79 if ($expectedFormat == 'unixTimestamp') {
80 try {
81 return self::fromEpoch($timestamp);
82 } catch (Exception $exception) {
83 return self::fromISO8601($timestamp);
84 }
85 } else {
86 if (\Dudlewebs\WPMCS\s3\Aws\is_valid_epoch($timestamp)) {
87 return self::fromEpoch($timestamp);
88 }
89 }
90 }
91 return self::fromISO8601($timestamp);
92 } catch (Exception $exception) {
93 throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp');
94 }
95 }
96 /**
97 * Serialize the DateTimeResult as an ISO 8601 date string.
98 *
99 * @return string
100 */
101 public function __toString()
102 {
103 return $this->format('c');
104 }
105 /**
106 * Serialize the date as an ISO 8601 date when serializing as JSON.
107 *
108 * @return string
109 */
110 #[\ReturnTypeWillChange]
111 public function jsonSerialize()
112 {
113 return (string) $this;
114 }
115 }
116