1 | <?php |
||
22 | final class UniversalTimestamp |
||
23 | { |
||
24 | const ISO8601_WITH_MILLISECONDS = '_ISO8601_WITH_MILLIS_'; |
||
25 | const ISO8601_WITH_MILLISECONDS_WITHOUT_TZ = '_ISO8601_WITH_MILLIS_WITHOUT_TZ'; |
||
26 | const ISO8601_WITH_MICROSECONDS = 'Y-m-d\TH:i:s.uO'; |
||
27 | const ISO8601_WITH_MICROSECONDS_WITHOUT_TZ = 'Y-m-d\TH:i:s.u'; |
||
28 | |||
29 | use TsExtension; |
||
30 | |||
31 | /** @var int */ |
||
32 | private $millis; |
||
33 | |||
34 | /** @var int */ |
||
35 | private $micros; |
||
36 | |||
37 | /** |
||
38 | * Constructor. |
||
39 | * |
||
40 | * @param integer $millisSinceEpoch |
||
41 | * @param integer $micros |
||
42 | */ |
||
43 | 17 | private function __construct($millisSinceEpoch, $micros = 0) |
|
52 | |||
53 | /** |
||
54 | * @return UniversalTimestamp |
||
55 | */ |
||
56 | 10 | public static function now() |
|
65 | |||
66 | /** |
||
67 | * @param \DateTimeInterface $dateTime |
||
68 | * @return UniversalTimestamp |
||
69 | */ |
||
70 | 3 | public static function fromDateTimeInterface(\DateTimeInterface $dateTime) |
|
79 | |||
80 | /** |
||
81 | * @param string $strTimestamp |
||
82 | * @param string $tz |
||
83 | * @return UniversalTimestamp |
||
84 | */ |
||
85 | 1 | public static function fromStringTimestamp($strTimestamp, $tz = 'UTC') |
|
97 | |||
98 | /** |
||
99 | * @param int $secondsSinceEpoch |
||
100 | * @return UniversalTimestamp |
||
101 | */ |
||
102 | 1 | public static function fromSecondsTimestamp($secondsSinceEpoch) |
|
106 | |||
107 | /** |
||
108 | * @param int $millisSinceEpoch |
||
109 | * @param int $micros |
||
110 | * @return UniversalTimestamp |
||
111 | */ |
||
112 | 9 | public static function fromMillisecondsTimestamp($millisSinceEpoch, $micros = 0) |
|
116 | |||
117 | /** |
||
118 | * @param mixed $dateObject If it's an integer, then it's understood as milliseconds since epoch |
||
119 | * @return UniversalTimestamp |
||
120 | */ |
||
121 | 2 | public static function fromWhatever($dateObject) { |
|
140 | |||
141 | /** |
||
142 | * @param UniversalTimestamp $otherTimestamp |
||
143 | * @return boolean |
||
144 | */ |
||
145 | 3 | public function isGreaterThan(UniversalTimestamp $otherTimestamp) |
|
152 | |||
153 | /** |
||
154 | * @param int $seconds |
||
155 | * @return UniversalTimestamp |
||
156 | */ |
||
157 | 1 | public function addSeconds($seconds) |
|
164 | |||
165 | /** |
||
166 | * @param int $millis |
||
167 | * @return UniversalTimestamp |
||
168 | */ |
||
169 | 1 | public function addMilliseconds($millis) |
|
176 | |||
177 | /** |
||
178 | * @return int |
||
179 | */ |
||
180 | 10 | public function asSeconds() |
|
184 | |||
185 | /** |
||
186 | * @return int |
||
187 | */ |
||
188 | 9 | public function asMilliseconds() |
|
192 | |||
193 | /** |
||
194 | * @return int |
||
195 | */ |
||
196 | 1 | public function getRemainingMicroseconds() |
|
200 | |||
201 | /** |
||
202 | * @param string|\DateTimeZone $tz |
||
203 | * @return \DateTimeImmutable |
||
204 | */ |
||
205 | 2 | public function asDateTimeInterface($tz = 'UTC') |
|
216 | |||
217 | /** |
||
218 | * @param string $format |
||
219 | * @param string|\DateTimeZone $tz |
||
220 | * @return string |
||
221 | */ |
||
222 | 2 | public function asFormattedString($format = self::ISO8601_WITH_MICROSECONDS, $tz = 'UTC') |
|
234 | |||
235 | /** |
||
236 | * @return string |
||
237 | */ |
||
238 | 1 | public function __toString() |
|
242 | } |
||
243 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.