-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbsoluteDate.php
More file actions
248 lines (215 loc) · 7.27 KB
/
AbsoluteDate.php
File metadata and controls
248 lines (215 loc) · 7.27 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
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
248
<?php
declare(strict_types=1);
namespace AssoConnect\PHPDate;
use Symfony\Component\Clock\DatePoint;
class AbsoluteDate implements \Stringable
{
public const DEFAULT_DATE_FORMAT = 'Y-m-d';
private DatePoint $datetime;
/**
* AbsoluteDate constructor from a date as string
* Use createInTimezone method if you have a DateTime instance or your format includes the hour part
*
* @param string $date Date as string
* @param string $format Format to parse the provided date
*/
public function __construct(string $date, string $format = self::DEFAULT_DATE_FORMAT)
{
$this->initDatetime($date, $format);
}
/**
* Returns date formatted according to given format
*
* Accepts any format supported by DateTime::format()
* @link https://www.php.net/manual/en/datetime.format.php
*/
public function format(string $format = self::DEFAULT_DATE_FORMAT): string
{
return $this->datetime->format($format);
}
/**
* Modify the internal datetime
*
* This method only supports year, month, and day modifiers
* @link https://www.php.net/manual/fr/datetime.formats.php
*
* @see TimeTraveler for coherent modifications month-over-month & year-over-year
*
* @return AbsoluteDate
*/
public function modify(string $modifier): self
{
$validPatterns = [
'day',
'days',
'month',
'months',
'year',
'years',
'week',
'weeks',
'last',
'first',
'ago',
'this',
'of',
'previous',
];
preg_match_all('/([a-z]+)/', $modifier, $matches);
$invalidPatterns = array_diff($matches[0], $validPatterns);
if ([] !== $invalidPatterns) {
throw new \DomainException(sprintf(
'Only modification on day, week, month, and year are allowed. Invalid patterns are: %s',
implode(', ', $invalidPatterns)
));
}
return new self($this->datetime->modify($modifier)->format(self::DEFAULT_DATE_FORMAT));
}
/**
* Return the DateTime for a given DateTimeZone
*/
public function startsAt(\DateTimeZone $timezone): \DateTimeImmutable
{
return $this->getDateTimeFromFormatAndTimezone(self::DEFAULT_DATE_FORMAT, $timezone);
}
/**
* Return the DateTime at the end of the day for a given DateTimeZone
*/
public function endsAt(\DateTimeZone $timezone): \DateTimeImmutable
{
return $this->getDateTimeFromFormatAndTimezone(self::DEFAULT_DATE_FORMAT . ' 23:59:59', $timezone);
}
private function getDateTimeFromFormatAndTimezone(string $format, \DateTimeZone $timezone): \DateTimeImmutable
{
return new DatePoint($this->format($format), $timezone);
}
/**
* Checks whether the date represented by this instance equals to the other.
*/
public function equalsTo(self $other): bool
{
return $this->__toString() === $other->__toString();
}
/**
* @deprecated use equalsTo
*/
public function equals(self $other): bool
{
return $this->equalsTo($other);
}
/**
* Compare this date with another date
* @return int [-1,0,1] If this date is before, on, or after the given date.
*/
public function compare(self $other): int
{
return $this->__toString() <=> $other->__toString();
}
/**
* Returns whether this date is before another.
*/
public function isBefore(self $other): bool
{
return $this->__toString() < $other->__toString();
}
/**
* Returns whether this date is before or equal to another.
*/
public function isBeforeOrEqualTo(self $other): bool
{
return $this->__toString() <= $other->__toString();
}
/**
* Returns whether this date is after another.
*/
public function isAfter(self $other): bool
{
return $this->__toString() > $other->__toString();
}
/**
* Returns whether this date is after or equal to another.
*/
public function isAfterOrEqualTo(self $other): bool
{
return $this->__toString() >= $other->__toString();
}
public function isBetween(self $other1, self $other2): bool
{
return $other1->__toString() < $this->__toString() && $this->__toString() < $other2->__toString();
}
public function isBetweenOrEqualTo(self $other1, self $other2): bool
{
return $other1->__toString() <= $this->__toString() && $this->__toString() <= $other2->__toString();
}
/**
* Returns date formatted according to the default date format (Y-m-d)
*/
public function __toString(): string
{
return $this->format(self::DEFAULT_DATE_FORMAT);
}
/**
* Returns an AbsoluteDate instance
*
* @param \DateTimeZone $timezone Timezone to use to get the right date
* @param \DateTimeInterface|null $datetime Point in time to find the date from
* @throws \Exception
*/
public static function createInTimezone(\DateTimeZone $timezone, \DateTimeInterface $datetime = null): self
{
$datetime = new DatePoint('@' . (null === $datetime ? time() : $datetime->getTimestamp()));
$datetime = $datetime->setTimezone($timezone);
return new self($datetime->format(self::DEFAULT_DATE_FORMAT));
}
/**
* Returns an AbsoluteDate instance from a relative format in a given timezone
*
* @param string $relative Relative format to use
* @param ?\DateTimeZone $timezone Timezone to use to get the right date
*/
public static function createRelative(string $relative = 'now', \DateTimeZone $timezone = null): self
{
if (null === $timezone) {
$timezone = new \DateTimeZone('UTC');
}
$datetime = new DatePoint($relative, $timezone);
return self::createInTimezone($timezone, $datetime);
}
private function initDatetime(string $date, string $format): void
{
$timezone = new \DateTimeZone('UTC');
$format .= 'H:i:s';
$date .= '00:00:00';
$datetime = DatePoint::createFromFormat($format, $date, $timezone);
$this->datetime = $datetime;
}
/**
* @return string[]
*/
public function __serialize(): array
{
return [
'date' => $this->format(),
];
}
/**
* @param string[] $data
*/
public function __unserialize(array $data): void
{
if (array_key_exists('date', $data)) {
if (1 === preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $data['date'])) {
$this->initDatetime($data['date'], self::DEFAULT_DATE_FORMAT);
return;
}
// Temporary for historic data
$this->initDatetime(unserialize($data['date']), self::DEFAULT_DATE_FORMAT);
return;
}
// Temporary for historic data
// The key looks like " AssoConnect\PHPDate\AbsoluteDate datetime" where both spaces are like \0
// so using array_values makes the value much easier to access
// @phpstan-ignore-next-line
$this->initDatetime(array_values($data)[0]->format(self::DEFAULT_DATE_FORMAT), self::DEFAULT_DATE_FORMAT);
}
}