-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTime.php
More file actions
170 lines (143 loc) · 4.13 KB
/
Time.php
File metadata and controls
170 lines (143 loc) · 4.13 KB
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
<?php declare(strict_types=1);
namespace DCarbone\Go\Time;
use DCarbone\Go\Time as TimeNS;
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';
/**
* @throws \DateMalformedStringException
*/
#[\ReturnTypeWillChange]
public static function createFromFormat(string $format, string $datetime, \DateTimeZone $timezone = null): bool|static
{
if ($dt = parent::createFromFormat($format, $datetime, $timezone)) {
// todo: find more efficient implementation
return new static($dt->format(self::FromDateTimeFormat), $timezone);
}
return false;
}
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;
}
public function Second(): int
{
return (int)$this->format('s');
}
public function Minute(): int
{
return (int)$this->format('i');
}
public function Hour(): int
{
return (int)$this->format('H');
}
public function Day(): int
{
return (int)$this->format('d');
}
public function Weekday(): Weekday
{
return new Weekday((int)$this->format('w'));
}
public function Month(): Month
{
return new Month((int)$this->format('m'));
}
public function Year(): int
{
return (int)$this->format('Y');
}
public function UnixNanoDuration(): Duration
{
return new Duration($this->UnixNano());
}
public function UnixNano(): int
{
return intval($this->Unix() * TimeNS::Second) + $this->Nanosecond();
}
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;
}
public function IsZero(): bool
{
return 0 === $this->UnixNano();
}
public function Before(Time $t): bool
{
return $this->UnixNano() < $t->UnixNano();
}
public function BeforeDateTime(\DateTimeInterface $dt): bool
{
return $this->UnixNano() <
((int)$dt->format('U') * TimeNS::Second +
(int)$dt->format('u') * TimeNS::Microsecond);
}
public function After(Time $t): bool
{
return $this->UnixNano() > $t->UnixNano();
}
public function AfterDateTime(\DateTimeInterface $dt): bool
{
return $this->UnixNano() > intval(
((int)$dt->format('U') * TimeNS::Second + (int)$dt->format('u') * TimeNS::Microsecond)
);
}
public function Equal(Time $t): bool
{
return $this->UnixNano() === $t->UnixNano();
}
public function EqualDateTime(\DateTimeInterface $dt): bool
{
return $this->UnixNano() === intval(
((int)$dt->format('U') * TimeNS::Second + (int)$dt->format('u') * TimeNS::Nanosecond)
);
}
/**
* @throws \Exception
*/
public function AddDuration(Duration $d): Time
{
return $this->add($d->DateInterval());
}
/**
* @throws \Exception
*/
public function SubDuration(Duration $d): Time
{
return $this->sub($d->DateInterval());
}
public function __toString(): string
{
if (0 === $this->Nanosecond()) {
return $this->format(self::DefaultFormatNoSubSeconds);
}
return $this->format(self::DefaultFormat);
}
}