|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DataValues; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Logical and mathematical helper functions for calculations with and conversions from TimeValue |
|
9
|
|
|
* objects. |
|
10
|
|
|
* |
|
11
|
|
|
* @since 0.6 |
|
12
|
|
|
* |
|
13
|
|
|
* @license GPL-2.0+ |
|
14
|
|
|
* @author Thiemo Kreuz |
|
15
|
|
|
*/ |
|
16
|
|
|
class TimeValueCalculator { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Average length of a year in the Gregorian calendar in seconds, calculated via |
|
20
|
|
|
* 365 + 1 / 4 - 1 / 100 + 1 / 400 = 365.2425 days. |
|
21
|
|
|
*/ |
|
22
|
|
|
const SECONDS_PER_GREGORIAN_YEAR = 31556952; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Maximum length for a timestamp. |
|
26
|
|
|
*/ |
|
27
|
|
|
private $MAX_LENGTH_TIMESTAMP = 33; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Lowest positive timestamp. |
|
31
|
|
|
*/ |
|
32
|
|
|
private $TIMESTAMP_ZERO = '+0000000000000000-01-01T00:00:00Z'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Highest positive timestamp strictly earlier than the lowest positive timestamp with |
|
36
|
|
|
* a length of $MAX_LENGTH_TIMESTAMP + 1. |
|
37
|
|
|
*/ |
|
38
|
|
|
private $HIGHEST_TIMESTAMP = '+9999999999999999-12-31T23:59:59Z'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* This returns a Unix timestamp from a TimeValue similar to PHP's mk_time() (or strtotime()), |
|
42
|
|
|
* but with no range limitations. Data type is float because PHP's 32 bit integer would |
|
43
|
|
|
* clip in the year 2038. |
|
44
|
|
|
* |
|
45
|
|
|
* @param TimeValue $timeValue |
|
46
|
|
|
* |
|
47
|
|
|
* @return float seconds since 1970-01-01T00:00:00Z |
|
48
|
|
|
*/ |
|
49
|
36 |
|
public function getTimestamp( TimeValue $timeValue ) { |
|
50
|
36 |
|
return $this->getSecondsSinceUnixEpoch( $timeValue->getTime(), $timeValue->getTimezone() ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns the lowest possible Unix timestamp from a TimeValue considering its precision |
|
55
|
|
|
* and its before value. Data type is float because PHP's 32 bit integer would clip in the |
|
56
|
|
|
* year 2038. |
|
57
|
|
|
* |
|
58
|
|
|
* @param TimeValue $timeValue |
|
59
|
|
|
* |
|
60
|
|
|
* @return float seconds since 1970-01-01T00:00:00Z |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function getLowerTimestamp( TimeValue $timeValue ) { |
|
63
|
1 |
|
$precision = $timeValue->getPrecision(); |
|
64
|
1 |
|
$timestamp = $timeValue->getTime(); |
|
65
|
1 |
|
if ( $timestamp[0] === '+' || $precision >= TimeValue::PRECISION_YEAR ) { |
|
66
|
1 |
|
$timestamp = $this->timestampAbsFloor( $timestamp, $precision ); |
|
67
|
|
|
} else { |
|
68
|
|
|
// digits corresponding to [0, PRECISION_YEAR] must be maximized |
|
69
|
|
|
// digits corresponding to [PRECISION_MONTH, PRECISION_SECOND] must be minimized |
|
70
|
1 |
|
$subTimestampLeft = $this->timestampAbsCeiling( $timestamp, $precision ); |
|
71
|
1 |
|
$subTimestampLeft = substr( |
|
72
|
1 |
|
$subTimestampLeft, |
|
73
|
1 |
|
0, |
|
74
|
1 |
|
-$this->charsAffectedByPrecision( TimeValue::PRECISION_YEAR ) |
|
75
|
|
|
); |
|
76
|
1 |
|
$subTimestampRight = $this->timestampAbsFloor( $timestamp, $precision ); |
|
77
|
1 |
|
$subTimestampRight = substr( |
|
78
|
1 |
|
$subTimestampRight, |
|
79
|
1 |
|
-$this->charsAffectedByPrecision( TimeValue::PRECISION_YEAR ) |
|
80
|
|
|
); |
|
81
|
1 |
|
$timestamp = $subTimestampLeft . $subTimestampRight; |
|
82
|
|
|
} |
|
83
|
1 |
|
$unixTimestamp = $this->getSecondsSinceUnixEpoch( $timestamp, $timeValue->getTimezone() ); |
|
84
|
1 |
|
$unixTimestamp -= $timeValue->getBefore() * $this->getSecondsForPrecision( $precision ); |
|
85
|
1 |
|
return $unixTimestamp; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the highest possible Unix timestamp from a TimeValue considering its precision |
|
90
|
|
|
* and its after value. Data type is float because PHP's 32 bit integer would clip in the |
|
91
|
|
|
* year 2038. |
|
92
|
|
|
* |
|
93
|
|
|
* @param TimeValue $timeValue |
|
94
|
|
|
* |
|
95
|
|
|
* @return float seconds since 1970-01-01T00:00:00Z |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function getHigherTimestamp( TimeValue $timeValue ) { |
|
98
|
1 |
|
$precision = $timeValue->getPrecision(); |
|
99
|
1 |
|
$timestamp = $timeValue->getTime(); |
|
100
|
1 |
|
if ( $timestamp[0] === '+' || $precision >= TimeValue::PRECISION_YEAR ) { |
|
101
|
1 |
|
$timestamp = $this->timestampAbsCeiling( $timestamp, $precision ); |
|
102
|
|
|
} else { |
|
103
|
|
|
// digits corresponding to [0, PRECISION_YEAR] must be minimized |
|
104
|
|
|
// digits corresponding to [PRECISION_MONTH, PRECISION_SECOND] must be maximized |
|
105
|
1 |
|
$subTimestampLeft = $this->timestampAbsFloor( $timestamp, $precision ); |
|
106
|
1 |
|
$subTimestampLeft = substr( |
|
107
|
1 |
|
$subTimestampLeft, |
|
108
|
1 |
|
0, |
|
109
|
1 |
|
-$this->charsAffectedByPrecision( TimeValue::PRECISION_YEAR ) |
|
110
|
|
|
); |
|
111
|
1 |
|
$subTimestampRight = $this->timestampAbsCeiling( $timestamp, $precision ); |
|
112
|
1 |
|
$subTimestampRight = substr( |
|
113
|
1 |
|
$subTimestampRight, |
|
114
|
1 |
|
-$this->charsAffectedByPrecision( TimeValue::PRECISION_YEAR ) |
|
115
|
|
|
); |
|
116
|
1 |
|
$timestamp = $subTimestampLeft . $subTimestampRight; |
|
117
|
|
|
} |
|
118
|
1 |
|
$unixTimestamp = $this->getSecondsSinceUnixEpoch( $timestamp, $timeValue->getTimezone() ); |
|
119
|
1 |
|
$unixTimestamp += $timeValue->getAfter() * $this->getSecondsForPrecision( $precision ); |
|
120
|
1 |
|
return $unixTimestamp; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $time an ISO 8601 date and time |
|
125
|
|
|
* @param int $timezone offset from UTC in minutes |
|
126
|
|
|
* |
|
127
|
|
|
* @throws InvalidArgumentException |
|
128
|
|
|
* @return float seconds since 1970-01-01T00:00:00Z |
|
129
|
|
|
*/ |
|
130
|
36 |
|
private function getSecondsSinceUnixEpoch( $time, $timezone = 0 ) { |
|
131
|
|
|
// Validation is done in TimeValue. As long if we found enough numbers we are fine. |
|
132
|
36 |
|
if ( !preg_match( '/([-+]?\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)/', $time, $matches ) |
|
133
|
|
|
) { |
|
134
|
|
|
throw new InvalidArgumentException( "Failed to parse time value $time." ); |
|
135
|
|
|
} |
|
136
|
36 |
|
list( , $fullYear, $month, $day, $hour, $minute, $second ) = $matches; |
|
137
|
|
|
|
|
138
|
|
|
// We use mktime only for the month, day and time calculation. Set the year to the smallest |
|
139
|
|
|
// possible in the 1970-2038 range to be safe, even if it's 1901-2038 since PHP 5.1.0. |
|
140
|
36 |
|
$year = $this->isLeapYear( $fullYear ) ? 1972 : 1970; |
|
141
|
|
|
|
|
142
|
36 |
|
$defaultTimezone = date_default_timezone_get(); |
|
143
|
36 |
|
date_default_timezone_set( 'UTC' ); |
|
144
|
|
|
// With day/month set to 0 mktime would calculate the last day of the previous month/year. |
|
145
|
|
|
// In the context of this calculation we must assume 0 means "start of the month/year". |
|
146
|
36 |
|
$timestamp = mktime( $hour, $minute, $second, max( 1, $month ), max( 1, $day ), $year ); |
|
147
|
36 |
|
date_default_timezone_set( $defaultTimezone ); |
|
148
|
|
|
|
|
149
|
36 |
|
if ( $timestamp === false ) { |
|
150
|
|
|
throw new InvalidArgumentException( "Failed to get epoche from time value $time." ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
36 |
|
$missingYears = ( $fullYear < 0 ? $fullYear + 1 : $fullYear ) - $year; |
|
154
|
36 |
|
$missingLeapDays = $this->getNumberOfLeapYears( $fullYear ) |
|
155
|
36 |
|
- $this->getNumberOfLeapYears( $year ); |
|
156
|
|
|
|
|
157
|
36 |
|
return $timestamp + ( $missingYears * 365 + $missingLeapDays ) * 86400 - $timezone * 60; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $calendar only TimeValue::CALENDAR_GREGORIAN is supported |
|
162
|
|
|
* @param float $month |
|
163
|
|
|
* @param float $year |
|
164
|
|
|
* |
|
165
|
|
|
* @throws InvalidArgumentException if $calendar is not supported |
|
166
|
|
|
* @return int |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function getDaysInMonth( $calendar, $month, $year ) { |
|
169
|
1 |
|
if ( $calendar !== TimeValue::CALENDAR_GREGORIAN ) { |
|
170
|
|
|
throw new InvalidArgumentException( "Only Gregorian calendar is supported." ); |
|
171
|
|
|
} |
|
172
|
1 |
|
return $month == 2 ? ( $this->isLeapYear( $year ) ? 29 : 28 ) : ( ( $month - 1 ) % 7 % 2 ? 30 : 31 ); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param float $year |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool if the year is a leap year in the Gregorian calendar |
|
179
|
|
|
*/ |
|
180
|
70 |
|
public function isLeapYear( $year ) { |
|
181
|
70 |
|
$year = $year < 0 ? ceil( $year ) + 1 : floor( $year ); |
|
182
|
70 |
|
$isMultipleOf4 = fmod( $year, 4 ) === 0.0; |
|
183
|
70 |
|
$isMultipleOf100 = fmod( $year, 100 ) === 0.0; |
|
184
|
70 |
|
$isMultipleOf400 = fmod( $year, 400 ) === 0.0; |
|
185
|
70 |
|
return $isMultipleOf4 && !$isMultipleOf100 || $isMultipleOf400; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param float $year |
|
190
|
|
|
* |
|
191
|
|
|
* @return float The number of leap years since year 1. To be more precise: The number of |
|
192
|
|
|
* leap days in the range between 31 December of year 1 and 31 December of the given year. |
|
193
|
|
|
*/ |
|
194
|
70 |
|
public function getNumberOfLeapYears( $year ) { |
|
195
|
70 |
|
$year = $year < 0 ? ceil( $year ) + 1 : floor( $year ); |
|
196
|
70 |
|
return floor( $year / 4 ) - floor( $year / 100 ) + floor( $year / 400 ); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param int $precision One of the TimeValue::PRECISION_... constants |
|
201
|
|
|
* |
|
202
|
|
|
* @throws InvalidArgumentException |
|
203
|
|
|
* @return float number of seconds in one unit of the given precision |
|
204
|
|
|
*/ |
|
205
|
10 |
|
public function getSecondsForPrecision( $precision ) { |
|
206
|
10 |
|
if ( $precision <= TimeValue::PRECISION_YEAR ) { |
|
207
|
5 |
|
return self::SECONDS_PER_GREGORIAN_YEAR * pow( |
|
208
|
5 |
|
10, |
|
209
|
5 |
|
TimeValue::PRECISION_YEAR - $precision |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
switch ( $precision ) { |
|
214
|
7 |
|
case TimeValue::PRECISION_SECOND: |
|
215
|
3 |
|
return 1; |
|
216
|
6 |
|
case TimeValue::PRECISION_MINUTE: |
|
217
|
3 |
|
return 60; |
|
218
|
5 |
|
case TimeValue::PRECISION_HOUR: |
|
219
|
3 |
|
return 3600; |
|
220
|
4 |
|
case TimeValue::PRECISION_DAY: |
|
221
|
3 |
|
return 86400; |
|
222
|
3 |
|
case TimeValue::PRECISION_MONTH: |
|
223
|
3 |
|
return self::SECONDS_PER_GREGORIAN_YEAR / 12; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
throw new InvalidArgumentException( "Unable to get seconds for precision $precision." ); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param $timestamp |
|
231
|
|
|
* @param $precision |
|
232
|
|
|
* |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
2 |
|
private function timestampAbsFloor( $timestamp, $precision ) { |
|
236
|
|
|
// The year is padded with zeros to have 16 digits |
|
237
|
2 |
|
$timestamp = substr_replace( |
|
238
|
2 |
|
$timestamp, |
|
239
|
2 |
|
str_repeat( '0', $this->MAX_LENGTH_TIMESTAMP - strlen( $timestamp ) ), |
|
240
|
2 |
|
1, |
|
241
|
|
|
0 |
|
242
|
|
|
); |
|
243
|
2 |
|
$numCharsToModify = $this->charsAffectedByPrecision( $precision ); |
|
244
|
2 |
|
$timestamp = substr( $timestamp, 0, -$numCharsToModify ) . |
|
245
|
2 |
|
substr( $this->TIMESTAMP_ZERO, -$numCharsToModify ); |
|
246
|
2 |
|
return $timestamp; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param $timestamp |
|
251
|
|
|
* @param $precision |
|
252
|
|
|
* |
|
253
|
|
|
* @return string |
|
254
|
|
|
*/ |
|
255
|
2 |
|
private function timestampAbsCeiling( $timestamp, $precision ) { |
|
256
|
|
|
// The year is padded with zeros to have 16 digits |
|
257
|
2 |
|
$timestamp = substr_replace( |
|
258
|
2 |
|
$timestamp, |
|
259
|
2 |
|
str_repeat( '0', $this->MAX_LENGTH_TIMESTAMP - strlen( $timestamp ) ), |
|
260
|
2 |
|
1, |
|
261
|
|
|
0 |
|
262
|
|
|
); |
|
263
|
2 |
|
$numCharsToModify = $this->charsAffectedByPrecision( $precision ); |
|
264
|
2 |
|
$timestampCeiling = substr( $timestamp, 0, -$numCharsToModify ) . |
|
265
|
2 |
|
substr( $this->HIGHEST_TIMESTAMP, -$numCharsToModify ); |
|
266
|
2 |
|
if ( $precision === TimeValue::PRECISION_MONTH ) { |
|
267
|
|
|
// The highest day (28-31) depends on the month and the year |
|
268
|
1 |
|
$month = (float)substr( |
|
269
|
1 |
|
$timestamp, |
|
270
|
1 |
|
-$this->charsAffectedByPrecision( TimeValue::PRECISION_YEAR ), |
|
271
|
1 |
|
2 |
|
272
|
|
|
); |
|
273
|
1 |
|
$year = (float)substr( |
|
274
|
1 |
|
$timestamp, |
|
275
|
1 |
|
1, |
|
276
|
1 |
|
-$this->charsAffectedByPrecision( TimeValue::PRECISION_YEAR ) - 1 |
|
277
|
|
|
); |
|
278
|
1 |
|
$daysInMonth = $this->getDaysInMonth( TimeValue::CALENDAR_GREGORIAN, $month, $year ); |
|
279
|
1 |
|
$timestampCeiling = substr( $timestamp, 0, -$numCharsToModify ) . |
|
280
|
1 |
|
$daysInMonth . |
|
281
|
1 |
|
substr( $this->HIGHEST_TIMESTAMP, -$numCharsToModify + 2 ); |
|
282
|
|
|
} |
|
283
|
2 |
|
return $timestampCeiling; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param int $precision |
|
288
|
|
|
* |
|
289
|
|
|
* @return int lowest number of characters in an ISO 8601 timestamp string |
|
290
|
|
|
* that are irrelevant given $precision |
|
291
|
|
|
*/ |
|
292
|
2 |
|
private function charsAffectedByPrecision( $precision ) { |
|
293
|
2 |
|
$numCharsAffected = 1; |
|
294
|
|
|
switch ( $precision ) { |
|
295
|
2 |
|
case TimeValue::PRECISION_MINUTE: |
|
296
|
2 |
|
$numCharsAffected = 3; |
|
297
|
2 |
|
break; |
|
298
|
2 |
|
case TimeValue::PRECISION_HOUR: |
|
299
|
2 |
|
$numCharsAffected = 6; |
|
300
|
2 |
|
break; |
|
301
|
2 |
|
case TimeValue::PRECISION_DAY: |
|
302
|
2 |
|
$numCharsAffected = 9; |
|
303
|
2 |
|
break; |
|
304
|
2 |
|
case TimeValue::PRECISION_MONTH: |
|
305
|
2 |
|
$numCharsAffected = 12; |
|
306
|
2 |
|
break; |
|
307
|
2 |
|
case TimeValue::PRECISION_YEAR: |
|
308
|
2 |
|
$numCharsAffected = 15; |
|
309
|
2 |
|
break; |
|
310
|
2 |
|
case TimeValue::PRECISION_YEAR10: |
|
311
|
2 |
|
$numCharsAffected = 17; |
|
312
|
2 |
|
break; |
|
313
|
2 |
|
case TimeValue::PRECISION_YEAR100: |
|
314
|
2 |
|
$numCharsAffected = 18; |
|
315
|
2 |
|
break; |
|
316
|
2 |
|
case TimeValue::PRECISION_YEAR1K: |
|
317
|
2 |
|
$numCharsAffected = 19; |
|
318
|
2 |
|
break; |
|
319
|
2 |
|
case TimeValue::PRECISION_YEAR10K: |
|
320
|
2 |
|
$numCharsAffected = 20; |
|
321
|
2 |
|
break; |
|
322
|
2 |
|
case TimeValue::PRECISION_YEAR100K: |
|
323
|
2 |
|
$numCharsAffected = 21; |
|
324
|
2 |
|
break; |
|
325
|
2 |
|
case TimeValue::PRECISION_YEAR1M: |
|
326
|
2 |
|
$numCharsAffected = 22; |
|
327
|
2 |
|
break; |
|
328
|
2 |
|
case TimeValue::PRECISION_YEAR10M: |
|
329
|
2 |
|
$numCharsAffected = 23; |
|
330
|
2 |
|
break; |
|
331
|
2 |
|
case TimeValue::PRECISION_YEAR100M: |
|
332
|
2 |
|
$numCharsAffected = 24; |
|
333
|
2 |
|
break; |
|
334
|
2 |
|
case TimeValue::PRECISION_YEAR1G: |
|
335
|
2 |
|
$numCharsAffected = 25; |
|
336
|
2 |
|
break; |
|
337
|
|
|
} |
|
338
|
2 |
|
return $numCharsAffected; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
} |
|
342
|
|
|
|