/
DifferenceTrait.php
369 lines (328 loc) · 12.1 KB
/
DifferenceTrait.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\Chronos;
use Cake\Chronos\ChronosDate;
use Cake\Chronos\ChronosInterface;
use Cake\Chronos\DifferenceFormatter;
use Cake\Chronos\DifferenceFormatterInterface;
use DatePeriod;
use DateTimeInterface;
use DateTimeZone;
/**
* Provides methods for getting differences between datetime objects.
*
* Expects that the implementing class implements:
*
* - static::now()
* - static::instance()
* - copy()
*/
trait DifferenceTrait
{
/**
* Instance of the diff formatting object.
*
* @var \Cake\Chronos\DifferenceFormatterInterface|null
*/
protected static $diffFormatter;
/**
* Get the difference in years
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInYears(?ChronosInterface $dt = null, bool $abs = true): int
{
Chronos::checkTypes($this, $dt);
$diff = $this->diff($dt ?? static::now($this->tz), $abs);
return $diff->invert ? -$diff->y : $diff->y;
}
/**
* Get the difference in months
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInMonths(?ChronosInterface $dt = null, bool $abs = true): int
{
Chronos::checkTypes($this, $dt);
$diff = $this->diff($dt ?? static::now($this->tz), $abs);
$months = $diff->y * ChronosInterface::MONTHS_PER_YEAR + $diff->m;
return $diff->invert ? -$months : $months;
}
/**
* Get the difference in months ignoring the timezone. This means the months are calculated
* in the specified timezone without converting to UTC first. This prevents the day from changing
* which can change the month.
*
* For example, if comparing `2019-06-01 Asia/Tokyo` and `2019-10-01 Asia/Tokyo`,
* the result would be 4 months instead of 3 when using normal `DateTime::diff()`.
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInMonthsIgnoreTimezone(?ChronosInterface $dt = null, bool $abs = true): int
{
if (static::class === ChronosDate::class) {
trigger_error('2.5 diffInMonthsIgnoreTimezone() will be removed in 3.x.', E_USER_DEPRECATED);
}
$utcTz = new DateTimeZone('UTC');
$source = new static($this->format('Y-m-d H:i:s.u'), $utcTz);
$dt = $dt ?? static::now($this->tz);
$dt = new static($dt->format('Y-m-d H:i:s.u'), $utcTz);
return $source->diffInMonths($dt, $abs);
}
/**
* Get the difference in weeks
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeeks(?ChronosInterface $dt = null, bool $abs = true): int
{
return (int)($this->diffInDays($dt, $abs) / ChronosInterface::DAYS_PER_WEEK);
}
/**
* Get the difference in days
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInDays(?ChronosInterface $dt = null, bool $abs = true): int
{
$diff = $this->diff($dt ?? static::now($this->tz), $abs);
return $diff->invert ? -$diff->days : $diff->days;
}
/**
* Get the difference in days using a filter callable
*
* @param callable $callback The callback to use for filtering.
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInDaysFiltered(callable $callback, ?ChronosInterface $dt = null, bool $abs = true): int
{
$interval = Chronos::createInterval(0, 0, 0, 1);
return $this->diffFiltered($interval, $callback, $dt, $abs);
}
/**
* Get the difference in hours using a filter callable
*
* @param callable $callback The callback to use for filtering.
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInHoursFiltered(callable $callback, ?ChronosInterface $dt = null, bool $abs = true): int
{
if (static::class === ChronosDate::class) {
trigger_error('2.5 diffInHoursFiltered() will be removed in 3.x.', E_USER_DEPRECATED);
}
$interval = Chronos::createInterval(0, 0, 0, 0, 1);
return $this->diffFiltered($interval, $callback, $dt, $abs);
}
/**
* Get the difference by the given interval using a filter callable
*
* @param \Cake\Chronos\ChronosInterval|\DateInterval $ci An interval to traverse by
* @param callable $callback The callback to use for filtering.
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffFiltered(
$ci,
callable $callback,
?ChronosInterface $dt = null,
bool $abs = true
): int {
$start = $this;
$end = $dt ?? static::now($this->tz);
$inverse = false;
Chronos::checkTypes($start, $end);
if ($end < $start) {
$start = $end;
$end = $this;
$inverse = true;
}
$period = new DatePeriod($start, $ci, $end);
$vals = array_filter(iterator_to_array($period), function (DateTimeInterface $date) use ($callback) {
return $callback(static::parse($date));
});
$diff = count($vals);
return $inverse && !$abs ? -$diff : $diff;
}
/**
* Get the difference in weekdays
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeekdays(?ChronosInterface $dt = null, bool $abs = true): int
{
return $this->diffInDaysFiltered(function (ChronosInterface $date) {
return $date->isWeekday();
}, $dt, $abs);
}
/**
* Get the difference in weekend days using a filter
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeekendDays(?ChronosInterface $dt = null, bool $abs = true): int
{
return $this->diffInDaysFiltered(function (ChronosInterface $date) {
return $date->isWeekend();
}, $dt, $abs);
}
/**
* Get the difference in hours
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInHours(?ChronosInterface $dt = null, bool $abs = true): int
{
if (static::class === ChronosDate::class) {
trigger_error('2.5 diffInHours() will be removed in 3.x.', E_USER_DEPRECATED);
}
return (int)(
$this->diffInSeconds($dt, $abs)
/ ChronosInterface::SECONDS_PER_MINUTE
/ ChronosInterface::MINUTES_PER_HOUR
);
}
/**
* Get the difference in minutes
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInMinutes(?ChronosInterface $dt = null, bool $abs = true): int
{
if (static::class === ChronosDate::class) {
trigger_error('2.5 diffInMinutes() will be removed in 3.x.', E_USER_DEPRECATED);
}
return (int)($this->diffInSeconds($dt, $abs) / ChronosInterface::SECONDS_PER_MINUTE);
}
/**
* Get the difference in seconds
*
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInSeconds(?ChronosInterface $dt = null, bool $abs = true): int
{
if (static::class === ChronosDate::class) {
trigger_error('2.5 diffInSeconds() will be removed in 3.x.', E_USER_DEPRECATED);
}
$dt = $dt ?? static::now($this->tz);
$value = $dt->getTimestamp() - $this->getTimestamp();
return $abs ? abs($value) : $value;
}
/**
* The number of seconds since midnight.
*
* @return int
*/
public function secondsSinceMidnight(): int
{
if (static::class === ChronosDate::class) {
trigger_error('2.5 secondsSinceMidnight() will be removed in 3.x.', E_USER_DEPRECATED);
}
return $this->diffInSeconds($this->copy()->startOfDay());
}
/**
* The number of seconds until 23:59:59.
*
* @return int
*/
public function secondsUntilEndOfDay(): int
{
if (static::class === ChronosDate::class) {
trigger_error('2.5 secondsUntilEndOfDay() will be removed in 3.x.', E_USER_DEPRECATED);
}
return $this->diffInSeconds($this->copy()->endOfDay());
}
/**
* Convenience method for getting the remaining time from a given time.
*
* @param \DateTime|\DateTimeImmutable $datetime The date to get the remaining time from.
* @return \DateInterval|bool The DateInterval object representing the difference between the two dates or FALSE on failure.
*/
public static function fromNow($datetime)
{
if (static::class === ChronosDate::class) {
trigger_error('2.5 fromNow() will be removed in 3.x.', E_USER_DEPRECATED);
}
$timeNow = new static();
return $timeNow->diff($datetime);
}
/**
* Get the difference in a human readable format.
*
* When comparing a value in the past to default now:
* 1 hour ago
* 5 months ago
*
* When comparing a value in the future to default now:
* 1 hour from now
* 5 months from now
*
* When comparing a value in the past to another value:
* 1 hour before
* 5 months before
*
* When comparing a value in the future to another value:
* 1 hour after
* 5 months after
*
* @param \Cake\Chronos\ChronosInterface|null $other The datetime to compare with.
* @param bool $absolute removes time difference modifiers ago, after, etc
* @return string
*/
public function diffForHumans(?ChronosInterface $other = null, bool $absolute = false): string
{
return static::diffFormatter()->diffForHumans($this, $other, $absolute);
}
/**
* Get the difference formatter instance or overwrite the current one.
*
* @param \Cake\Chronos\DifferenceFormatterInterface|null $formatter The formatter instance when setting.
* @return \Cake\Chronos\DifferenceFormatterInterface The formatter instance.
*/
public static function diffFormatter(?DifferenceFormatterInterface $formatter = null): DifferenceFormatterInterface
{
if ($formatter === null) {
if (static::$diffFormatter === null) {
static::$diffFormatter = new DifferenceFormatter();
}
return static::$diffFormatter;
}
return static::$diffFormatter = $formatter;
}
}