/
Time.php
476 lines (429 loc) · 16.8 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\I18n;
use Cake\Chronos\Chronos;
use DateTime;
use DateTimeZone;
use IntlDateFormatter;
use JsonSerializable;
/**
* Extends the built-in DateTime class to provide handy methods and locale-aware
* formatting helpers
*
*/
class Time extends Chronos implements JsonSerializable
{
use DateFormatTrait;
/**
* The format to use when formatting a time using `Cake\I18n\Time::i18nFormat()`
* and `__toString`
*
* The format should be either the formatting constants from IntlDateFormatter as
* described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
* as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
*
* It is possible to provide an array of 2 constants. In this case, the first position
* will be used for formatting the date part of the object and the second position
* will be used to format the time part.
*
* @var string|array|int
* @see \Cake\I18n\Time::i18nFormat()
*/
protected static $_toStringFormat = [IntlDateFormatter::SHORT, IntlDateFormatter::SHORT];
/**
* The format to use when formatting a time using `Cake\I18n\Time::nice()`
*
* The format should be either the formatting constants from IntlDateFormatter as
* described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
* as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
*
* It is possible to provide an array of 2 constants. In this case, the first position
* will be used for formatting the date part of the object and the second position
* will be used to format the time part.
*
* @var string|array|int
* @see \Cake\I18n\Time::nice()
*/
public static $niceFormat = [IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT];
/**
* The format to use when formatting a time using `Cake\I18n\Time::timeAgoInWords()`
* and the difference is more than `Cake\I18n\Time::$wordEnd`
*
* @var string
* @see \Cake\I18n\Time::timeAgoInWords()
*/
public static $wordFormat = [IntlDateFormatter::SHORT, -1];
/**
* The format to use when formatting a time using `Time::timeAgoInWords()`
* and the difference is less than `Time::$wordEnd`
*
* @var array
* @see \Cake\I18n\Time::timeAgoInWords()
*/
public static $wordAccuracy = [
'year' => "day",
'month' => "day",
'week' => "day",
'day' => "hour",
'hour' => "minute",
'minute' => "minute",
'second' => "second",
];
/**
* The end of relative time telling
*
* @var string
* @see \Cake\I18n\Time::timeAgoInWords()
*/
public static $wordEnd = '+1 month';
/**
* {@inheritDoc}
*/
public function __construct($time = null, $tz = null)
{
if ($time instanceof DateTime) {
$tz = $time->getTimeZone();
$time = $time->format('Y-m-d H:i:s');
}
if (is_numeric($time)) {
$time = '@' . $time;
}
parent::__construct($time, $tz);
}
/**
* Returns either a relative or a formatted absolute date depending
* on the difference between the current time and this object.
*
* ### Options:
*
* - `from` => another Time object representing the "now" time
* - `format` => a fall back format if the relative time is longer than the duration specified by end
* - `accuracy` => Specifies how accurate the date should be described (array)
* - year => The format if years > 0 (default "day")
* - month => The format if months > 0 (default "day")
* - week => The format if weeks > 0 (default "day")
* - day => The format if weeks > 0 (default "hour")
* - hour => The format if hours > 0 (default "minute")
* - minute => The format if minutes > 0 (default "minute")
* - second => The format if seconds > 0 (default "second")
* - `end` => The end of relative time telling
* - `relativeString` => The printf compatible string when outputting relative time
* - `absoluteString` => The printf compatible string when outputting absolute time
* - `timezone` => The user timezone the timestamp should be formatted in.
*
* Relative dates look something like this:
*
* - 3 weeks, 4 days ago
* - 15 seconds ago
*
* Default date formatting is d/M/YY e.g: on 18/2/09. Formatting is done internally using
* `i18nFormat`, see the method for the valid formatting strings
*
* The returned string includes 'ago' or 'on' and assumes you'll properly add a word
* like 'Posted ' before the function output.
*
* NOTE: If the difference is one week or more, the lowest level of accuracy is day
*
* @param array $options Array of options.
* @return string Relative time string.
*/
public function timeAgoInWords(array $options = [])
{
$time = $this;
$timezone = null;
$format = static::$wordFormat;
$end = static::$wordEnd;
$relativeString = __d('cake', '%s ago');
$absoluteString = __d('cake', 'on %s');
$accuracy = static::$wordAccuracy;
$from = static::now();
$opts = ['timezone', 'format', 'end', 'relativeString', 'absoluteString', 'from'];
foreach ($opts as $option) {
if (isset($options[$option])) {
${$option} = $options[$option];
unset($options[$option]);
}
}
if (isset($options['accuracy'])) {
if (is_array($options['accuracy'])) {
$accuracy = $options['accuracy'] + $accuracy;
} else {
foreach ($accuracy as $key => $level) {
$accuracy[$key] = $options['accuracy'];
}
}
}
if ($timezone) {
$time = $time->timezone($timezone);
}
$now = $from->format('U');
$inSeconds = $time->format('U');
$backwards = ($inSeconds > $now);
$futureTime = $now;
$pastTime = $inSeconds;
if ($backwards) {
$futureTime = $inSeconds;
$pastTime = $now;
}
$diff = $futureTime - $pastTime;
if (!$diff) {
return __d('cake', 'just now', 'just now');
}
if ($diff > abs($now - (new static($end))->format('U'))) {
return sprintf($absoluteString, $time->i18nFormat($format));
}
// If more than a week, then take into account the length of months
if ($diff >= 604800) {
list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $futureTime));
list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $pastTime));
$weeks = $days = $hours = $minutes = $seconds = 0;
$years = $future['Y'] - $past['Y'];
$months = $future['m'] + ((12 * $years) - $past['m']);
if ($months >= 12) {
$years = floor($months / 12);
$months = $months - ($years * 12);
}
if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] === 1) {
$years--;
}
if ($future['d'] >= $past['d']) {
$days = $future['d'] - $past['d'];
} else {
$daysInPastMonth = date('t', $pastTime);
$daysInFutureMonth = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y']));
if (!$backwards) {
$days = ($daysInPastMonth - $past['d']) + $future['d'];
} else {
$days = ($daysInFutureMonth - $past['d']) + $future['d'];
}
if ($future['m'] != $past['m']) {
$months--;
}
}
if (!$months && $years >= 1 && $diff < ($years * 31536000)) {
$months = 11;
$years--;
}
if ($months >= 12) {
$years = $years + 1;
$months = $months - 12;
}
if ($days >= 7) {
$weeks = floor($days / 7);
$days = $days - ($weeks * 7);
}
} else {
$years = $months = $weeks = 0;
$days = floor($diff / 86400);
$diff = $diff - ($days * 86400);
$hours = floor($diff / 3600);
$diff = $diff - ($hours * 3600);
$minutes = floor($diff / 60);
$diff = $diff - ($minutes * 60);
$seconds = $diff;
}
$fWord = $accuracy['second'];
if ($years > 0) {
$fWord = $accuracy['year'];
} elseif (abs($months) > 0) {
$fWord = $accuracy['month'];
} elseif (abs($weeks) > 0) {
$fWord = $accuracy['week'];
} elseif (abs($days) > 0) {
$fWord = $accuracy['day'];
} elseif (abs($hours) > 0) {
$fWord = $accuracy['hour'];
} elseif (abs($minutes) > 0) {
$fWord = $accuracy['minute'];
}
$fNum = str_replace(['year', 'month', 'week', 'day', 'hour', 'minute', 'second'], [1, 2, 3, 4, 5, 6, 7], $fWord);
$relativeDate = '';
if ($fNum >= 1 && $years > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '{0} year', '{0} years', $years, $years);
}
if ($fNum >= 2 && $months > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '{0} month', '{0} months', $months, $months);
}
if ($fNum >= 3 && $weeks > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '{0} week', '{0} weeks', $weeks, $weeks);
}
if ($fNum >= 4 && $days > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '{0} day', '{0} days', $days, $days);
}
if ($fNum >= 5 && $hours > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '{0} hour', '{0} hours', $hours, $hours);
}
if ($fNum >= 6 && $minutes > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '{0} minute', '{0} minutes', $minutes, $minutes);
}
if ($fNum >= 7 && $seconds > 0) {
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '{0} second', '{0} seconds', $seconds, $seconds);
}
// When time has passed
if (!$backwards && $relativeDate) {
return sprintf($relativeString, $relativeDate);
}
if (!$backwards) {
$aboutAgo = [
'second' => __d('cake', 'about a second ago'),
'minute' => __d('cake', 'about a minute ago'),
'hour' => __d('cake', 'about an hour ago'),
'day' => __d('cake', 'about a day ago'),
'week' => __d('cake', 'about a week ago'),
'year' => __d('cake', 'about a year ago')
];
return $aboutAgo[$fWord];
}
// When time is to come
if (!$relativeDate) {
$aboutIn = [
'second' => __d('cake', 'in about a second'),
'minute' => __d('cake', 'in about a minute'),
'hour' => __d('cake', 'in about an hour'),
'day' => __d('cake', 'in about a day'),
'week' => __d('cake', 'in about a week'),
'year' => __d('cake', 'in about a year')
];
return $aboutIn[$fWord];
}
return $relativeDate;
}
/**
* Returns the difference between this date and the provided one in a human
* readable format.
*
* See `Time::timeAgoInWords()` for a full list of options that can be passed
* to this method.
*
* @param \Cake\Chronos\Chronos|null $other the date to diff with
* @param array $options options accepted by timeAgoInWords
* @return string
* @see Time::timeAgoInWords()
*/
public function diffForHumans(Chronos $other = null, array $options = [])
{
$options = ['from' => $other] + $options;
return $this->timeAgoInWords($options);
}
/**
* Get list of timezone identifiers
*
* @param int|string $filter A regex to filter identifier
* Or one of DateTimeZone class constants
* @param string $country A two-letter ISO 3166-1 compatible country code.
* This option is only used when $filter is set to DateTimeZone::PER_COUNTRY
* @param bool|array $options If true (default value) groups the identifiers list by primary region.
* Otherwise, an array containing `group`, `abbr`, `before`, and `after`
* keys. Setting `group` and `abbr` to true will group results and append
* timezone abbreviation in the display value. Set `before` and `after`
* to customize the abbreviation wrapper.
* @return array List of timezone identifiers
* @since 2.2
*/
public static function listTimezones($filter = null, $country = null, $options = [])
{
if (is_bool($options)) {
$options = [
'group' => $options,
];
}
$defaults = [
'group' => true,
'abbr' => false,
'before' => ' - ',
'after' => null,
];
$options += $defaults;
$group = $options['group'];
$regex = null;
if (is_string($filter)) {
$regex = $filter;
$filter = null;
}
if ($filter === null) {
$filter = DateTimeZone::ALL;
}
$identifiers = DateTimeZone::listIdentifiers($filter, $country);
if ($regex) {
foreach ($identifiers as $key => $tz) {
if (!preg_match($regex, $tz)) {
unset($identifiers[$key]);
}
}
}
if ($group) {
$groupedIdentifiers = [];
$now = time();
$before = $options['before'];
$after = $options['after'];
foreach ($identifiers as $key => $tz) {
$abbr = null;
if ($options['abbr']) {
$dateTimeZone = new DateTimeZone($tz);
$trans = $dateTimeZone->getTransitions($now, $now);
$abbr = isset($trans[0]['abbr']) ?
$before . $trans[0]['abbr'] . $after :
null;
}
$item = explode('/', $tz, 2);
if (isset($item[1])) {
$groupedIdentifiers[$item[0]][$tz] = $item[1] . $abbr;
} else {
$groupedIdentifiers[$item[0]] = [$tz => $item[0] . $abbr];
}
}
return $groupedIdentifiers;
}
return array_combine($identifiers, $identifiers);
}
/**
* Returns true this instance will happen within the specified interval
*
* This overridden method provides backwards compatible behavior for integers,
* or strings with trailing spaces. This behavior is *deprecated* and will be
* removed in future versions of CakePHP.
*
* @param string|int $timeInterval the numeric value with space then time type.
* Example of valid types: 6 hours, 2 days, 1 minute.
* @return bool
*/
public function wasWithinLast($timeInterval)
{
$tmp = trim($timeInterval);
if (is_numeric($tmp)) {
$timeInterval = $tmp . ' days';
}
return parent::wasWithinLast($timeInterval);
}
/**
* Returns true this instance happened within the specified interval
*
* This overridden method provides backwards compatible behavior for integers,
* or strings with trailing spaces. This behavior is *deprecated* and will be
* removed in future versions of CakePHP.
*
* @param string|int $timeInterval the numeric value with space then time type.
* Example of valid types: 6 hours, 2 days, 1 minute.
* @return bool
*/
public function isWithinNext($timeInterval)
{
$tmp = trim($timeInterval);
if (is_numeric($tmp)) {
$timeInterval = $tmp . ' days';
}
return parent::isWithinNext($timeInterval);
}
}