PluginProbe
Media Cloud Sync / 1.2.3
Media Cloud Sync v1.2.3
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 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / Api / DateTimeResult.php

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

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