forked from flourishlib/flourish-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fTime.php
379 lines (328 loc) · 11.9 KB
/
fTime.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
<?php
/**
* Represents a time of day as a value object
*
* @copyright Copyright (c) 2008-2011 Will Bond
* @author Will Bond [wb] <[email protected]>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fTime
*
* @version 1.0.0b12
* @changes 1.0.0b12 Fixed a method signature [wb, 2011-08-24]
* @changes 1.0.0b11 Fixed a bug with the constructor not properly handling unix timestamps that are negative integers [wb, 2011-06-02]
* @changes 1.0.0b10 Changed the `$time` attribute to be protected [wb, 2011-03-20]
* @changes 1.0.0b9 Added the `$simple` parameter to ::getFuzzyDifference() [wb, 2010-03-15]
* @changes 1.0.0b8 Added a call to fTimestamp::callUnformatCallback() in ::__construct() for localization support [wb, 2009-06-01]
* @changes 1.0.0b7 Backwards compatibility break - Removed ::getSecondsDifference(), added ::eq(), ::gt(), ::gte(), ::lt(), ::lte() [wb, 2009-03-05]
* @changes 1.0.0b6 Fixed an outdated fCore method call [wb, 2009-02-23]
* @changes 1.0.0b5 Updated for new fCore API [wb, 2009-02-16]
* @changes 1.0.0b4 Fixed ::__construct() to properly handle the 5.0 to 5.1 change in strtotime() [wb, 2009-01-21]
* @changes 1.0.0b3 Added support for CURRENT_TIMESTAMP and CURRENT_TIME SQL keywords [wb, 2009-01-11]
* @changes 1.0.0b2 Removed the adjustment amount check from ::adjust() [wb, 2008-12-31]
* @changes 1.0.0b The initial implementation [wb, 2008-02-12]
*/
class fTime
{
/**
* Composes text using fText if loaded
*
* @param string $message The message to compose
* @param mixed $component A string or number to insert into the message
* @param mixed ...
* @return string The composed and possible translated message
*/
static protected function compose($message)
{
$args = array_slice(func_get_args(), 1);
if (class_exists('fText', FALSE)) {
return call_user_func_array(
array('fText', 'compose'),
array($message, $args)
);
} else {
return vsprintf($message, $args);
}
}
/**
* A timestamp of the time
*
* @var integer
*/
protected $time;
/**
* Creates the time to represent, no timezone is allowed since times don't have timezones
*
* @throws fValidationException When `$time` is not a valid time
*
* @param fTime|object|string|integer $time The time to represent, `NULL` is interpreted as now
* @return fTime
*/
public function __construct($time=NULL)
{
if ($time === NULL) {
$timestamp = time();
} elseif (is_numeric($time) && preg_match('#^-?\d+$#D', $time)) {
$timestamp = (int) $time;
} elseif (is_string($time) && in_array(strtoupper($time), array('CURRENT_TIMESTAMP', 'CURRENT_TIME'))) {
$timestamp = time();
} else {
if (is_object($time) && is_callable(array($time, '__toString'))) {
$time = $time->__toString();
} elseif (is_numeric($time) || is_object($time)) {
$time = (string) $time;
}
$time = fTimestamp::callUnformatCallback($time);
$timestamp = strtotime($time);
}
$is_51 = fCore::checkVersion('5.1');
$is_valid = ($is_51 && $timestamp !== FALSE) || (!$is_51 && $timestamp !== -1);
if (!$is_valid) {
throw new fValidationException(
'The time specified, %s, does not appear to be a valid time',
$time
);
}
$this->time = strtotime(date('1970-01-01 H:i:s', $timestamp));
}
/**
* All requests that hit this method should be requests for callbacks
*
* @internal
*
* @param string $method The method to create a callback for
* @return callback The callback for the method requested
*/
public function __get($method)
{
return array($this, $method);
}
/**
* Returns this time in `'H:i:s'` format
*
* @return string The `'H:i:s'` format of this time
*/
public function __toString()
{
return date('H:i:s', $this->time);
}
/**
* Changes the time by the adjustment specified, only adjustments of `'hours'`, `'minutes'`, and `'seconds'` are allowed
*
* @throws fValidationException When `$adjustment` is not a valid relative time measurement
*
* @param string $adjustment The adjustment to make
* @return fTime The adjusted time
*/
public function adjust($adjustment)
{
$timestamp = strtotime($adjustment, $this->time);
if ($timestamp === FALSE || $timestamp === -1) {
throw new fValidationException(
'The adjustment specified, %s, does not appear to be a valid relative time measurement',
$adjustment
);
}
return new fTime($timestamp);
}
/**
* If this time is equal to the time passed
*
* @param fTime|object|string|integer $other_time The time to compare with, `NULL` is interpreted as today
* @return boolean If this time is equal to the one passed
*/
public function eq($other_time=NULL)
{
$other_time = new fTime($other_time);
return $this->time == $other_time->time;
}
/**
* Formats the time
*
* @throws fValidationException When a non-time formatting character is included in `$format`
*
* @param string $format The [http://php.net/date date()] function compatible formatting string, or a format name from fTimestamp::defineFormat()
* @return string The formatted time
*/
public function format($format)
{
$format = fTimestamp::translateFormat($format);
$restricted_formats = 'cdDeFIjlLmMnNoOPrStTUwWyYzZ';
if (preg_match('#(?!\\\\).[' . $restricted_formats . ']#', $format)) {
throw new fProgrammerException(
'The formatting string, %1$s, contains one of the following non-time formatting characters: %2$s',
$format,
join(', ', str_split($restricted_formats))
);
}
return fTimestamp::callFormatCallback(date($format, $this->time));
}
/**
* Returns the approximate difference in time, discarding any unit of measure but the least specific.
*
* The output will read like:
*
* - "This time is `{return value}` the provided one" when a time it passed
* - "This time is `{return value}`" when no time is passed and comparing with the current time
*
* Examples of output for a time passed might be:
*
* - `'5 minutes after'`
* - `'2 hours before'`
* - `'at the same time'`
*
* Examples of output for no time passed might be:
*
* - `'5 minutes ago'`
* - `'2 hours ago'`
* - `'right now'`
*
* You would never get the following output since it includes more than one unit of time measurement:
*
* - `'5 minutes and 28 seconds'`
* - `'1 hour, 15 minutes'`
*
* Values that are close to the next largest unit of measure will be rounded up:
*
* - `'55 minutes'` would be represented as `'1 hour'`, however `'45 minutes'` would not
*
* @param fTime|object|string|integer $other_time The time to create the difference with, `NULL` is interpreted as now
* @param boolean $simple When `TRUE`, the returned value will only include the difference in the two times, but not `from now`, `ago`, `after` or `before`
* @param boolean |$simple
* @return string The fuzzy difference in time between the this time and the one provided
*/
public function getFuzzyDifference($other_time=NULL, $simple=FALSE)
{
if (is_bool($other_time)) {
$simple = $other_time;
$other_time = NULL;
}
$relative_to_now = FALSE;
if ($other_time === NULL) {
$relative_to_now = TRUE;
}
$other_time = new fTime($other_time);
$diff = $this->time - $other_time->time;
if (abs($diff) < 10) {
if ($relative_to_now) {
return self::compose('right now');
}
return self::compose('at the same time');
}
static $break_points = array();
if (!$break_points) {
$break_points = array(
/* 45 seconds */
45 => array(1, self::compose('second'), self::compose('seconds')),
/* 45 minutes */
2700 => array(60, self::compose('minute'), self::compose('minutes')),
/* 18 hours */
64800 => array(3600, self::compose('hour'), self::compose('hours')),
/* 5 days */
432000 => array(86400, self::compose('day'), self::compose('days'))
);
}
foreach ($break_points as $break_point => $unit_info) {
if (abs($diff) > $break_point) { continue; }
$unit_diff = round(abs($diff)/$unit_info[0]);
$units = fGrammar::inflectOnQuantity($unit_diff, $unit_info[1], $unit_info[2]);
break;
}
if ($simple) {
return self::compose('%1$s %2$s', $unit_diff, $units);
}
if ($relative_to_now) {
if ($diff > 0) {
return self::compose('%1$s %2$s from now', $unit_diff, $units);
}
return self::compose('%1$s %2$s ago', $unit_diff, $units);
}
if ($diff > 0) {
return self::compose('%1$s %2$s after', $unit_diff, $units);
}
return self::compose('%1$s %2$s before', $unit_diff, $units);
}
/**
* If this time is greater than the time passed
*
* @param fTime|object|string|integer $other_time The time to compare with, `NULL` is interpreted as now
* @return boolean If this time is greater than the one passed
*/
public function gt($other_time=NULL)
{
$other_time = new fTime($other_time);
return $this->time > $other_time->time;
}
/**
* If this time is greater than or equal to the time passed
*
* @param fTime|object|string|integer $other_time The time to compare with, `NULL` is interpreted as now
* @return boolean If this time is greater than or equal to the one passed
*/
public function gte($other_time=NULL)
{
$other_time = new fTime($other_time);
return $this->time >= $other_time->time;
}
/**
* If this time is less than the time passed
*
* @param fTime|object|string|integer $other_time The time to compare with, `NULL` is interpreted as today
* @return boolean If this time is less than the one passed
*/
public function lt($other_time=NULL)
{
$other_time = new fTime($other_time);
return $this->time < $other_time->time;
}
/**
* If this time is less than or equal to the time passed
*
* @param fTime|object|string|integer $other_time The time to compare with, `NULL` is interpreted as today
* @return boolean If this time is less than or equal to the one passed
*/
public function lte($other_time=NULL)
{
$other_time = new fTime($other_time);
return $this->time <= $other_time->time;
}
/**
* Modifies the current time, creating a new fTime object
*
* The purpose of this method is to allow for easy creation of a time
* based on this time. Below are some examples of formats to
* modify the current time:
*
* - `'17:i:s'` to set the hour of the time to 5 PM
* - 'H:00:00'` to set the time to the beginning of the current hour
*
* @param string $format The current time will be formatted with this string, and the output used to create a new object
* @return fTime The new time
*/
public function modify($format)
{
return new fTime($this->format($format));
}
}
/**
* Copyright (c) 2008-2011 Will Bond <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/