/
Time.php
247 lines (220 loc) · 5.39 KB
/
Time.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php declare(strict_types=1);
namespace DCarbone\Go\Time;
use DCarbone\Go\Time as TimeNS;
/**
* Class Time
* @package DCarbone\Go\Time
*
* TODO: Improve efficiency
*/
class Time extends \DateTime
{
public const DefaultFormat = 'Y-m-d H:i:s.u000 O e';
public const DefaultFormatNoSubSeconds = 'Y-m-d H:i:s O e';
private const FromDateTimeFormat = 'Y-m-d H:i:s.u O';
/**
* @param string $format
* @param string $time
* @param \DateTimeZone|null $timezone
* @return false|static
* @throws \Exception
*/
#[\ReturnTypeWillChange]
public static function createFromFormat($format, $time, \DateTimeZone $timezone = null)
{
if ($dt = parent::createFromFormat($format, $time, $timezone)) {
// todo: find more efficient implementation
return new static($dt->format(self::FromDateTimeFormat), $timezone);
}
return false;
}
/**
* @return string
*/
public static function getLastErrorsString(): string
{
$errs = \DateTime::getLastErrors();
if (!is_array($errs)) {
return '';
}
$errstr = '';
if (($errs['warning_count'] ?? 0) > 0) {
$errstr = sprintf('Warnings: ["%s"]', implode('", "', $errs['warnings'] ?? []));
}
if (($errs['error_count'] ?? 0) > 0) {
if ($errstr !== '') {
$errstr .= '; ';
}
$errstr = sprintf('%sErrors: ["%s"]', $errstr, implode('", "', $errs['errors'] ?? []));
}
return $errstr;
}
/**
* @return int
*/
public function Second(): int
{
return (int)$this->format('s');
}
/**
* @return int
*/
public function Minute(): int
{
return (int)$this->format('i');
}
/**
* @return int
*/
public function Hour(): int
{
return (int)$this->format('H');
}
/**
* @return int
*/
public function Day(): int
{
return (int)$this->format('d');
}
/**
* @return \DCarbone\Go\Time\Weekday
*/
public function Weekday(): Weekday
{
return new Weekday((int)$this->format('w'));
}
/**
* @return \DCarbone\Go\Time\Month
*/
public function Month(): Month
{
return new Month((int)$this->format('m'));
}
/**
* @return int
*/
public function Year(): int
{
return (int)$this->format('Y');
}
/**
* @return \DCarbone\Go\Time\Duration
*/
public function UnixNanoDuration(): Duration
{
return new Duration($this->UnixNano());
}
/**
* @return int
*/
public function UnixNano(): int
{
return intval($this->Unix() * TimeNS::Second) + $this->Nanosecond();
}
/**
* @return int
*/
public function Unix(): int
{
return (int)$this->format('U');
}
/**
* NOTE: PHP is only capable of microsecond accuracy at this point.
*
* @return int
*/
public function Nanosecond(): int
{
return (int)$this->format('u') * TimeNS::Microsecond;
}
/**
* @return bool
*/
public function IsZero(): bool
{
return 0 === $this->UnixNano();
}
/**
* @param \DCarbone\Go\Time\Time $t
* @return bool
*/
public function Before(Time $t): bool
{
return $this->UnixNano() < $t->UnixNano();
}
/**
* @param \DateTimeInterface $dt
* @return bool
*/
public function BeforeDateTime(\DateTimeInterface $dt): bool
{
return $this->UnixNano() <
((int)$dt->format('U') * TimeNS::Second +
(int)$dt->format('u') * TimeNS::Microsecond);
}
/**
* @param \DCarbone\Go\Time\Time $t
* @return bool
*/
public function After(Time $t): bool
{
return $this->UnixNano() > $t->UnixNano();
}
/**
* @param \DateTimeInterface $dt
* @return bool
*/
public function AfterDateTime(\DateTimeInterface $dt): bool
{
return $this->UnixNano() > intval(
((int)$dt->format('U') * TimeNS::Second + (int)$dt->format('u') * TimeNS::Microsecond)
);
}
/**
* @param \DCarbone\Go\Time\Time $t
* @return bool
*/
public function Equal(Time $t): bool
{
return $this->UnixNano() === $t->UnixNano();
}
/**
* @param \DateTimeInterface $dt
* @return bool
*/
public function EqualDateTime(\DateTimeInterface $dt): bool
{
return $this->UnixNano() === intval(
((int)$dt->format('U') * TimeNS::Second + (int)$dt->format('u') * TimeNS::Nanosecond)
);
}
/**
* @param \DCarbone\Go\Time\Duration $d
* @return \DCarbone\Go\Time\Time
* @throws \Exception
*/
public function AddDuration(Duration $d): Time
{
return $this->add($d->DateInterval());
}
/**
* @param \DCarbone\Go\Time\Duration $d
* @return \DCarbone\Go\Time\Time
* @throws \Exception
*/
public function SubDuration(Duration $d): Time
{
return $this->sub($d->DateInterval());
}
/**
* @return string
*/
public function __toString(): string
{
if (0 === $this->Nanosecond()) {
return $this->format(self::DefaultFormatNoSubSeconds);
}
return $this->format(self::DefaultFormat);
}
}