|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace marsapp\helper\timeperiod; |
|
4
|
|
|
|
|
5
|
|
|
use marsapp\helper\timeperiod\classes\Base; |
|
6
|
|
|
use marsapp\helper\timeperiod\classes\LogicalProcess; |
|
7
|
|
|
use marsapp\helper\timeperiod\classes\DataProcess; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Time Period Helper |
|
11
|
|
|
* |
|
12
|
|
|
* Note: |
|
13
|
|
|
* 1. Format: $timePeriods = [[$startDatetime1, $endDatetime1], [$startDatetime2, $endDatetime2], ...]; |
|
14
|
|
|
* - $Datetime = Y-m-d H:i:s ; Y-m-d H:i:00 ; Y-m-d H:00:00 ; |
|
15
|
|
|
* 2. If it is hour/minute/second, the end point is usually not included, for example, 8 o'clock to 9 o'clock is 1 hour. |
|
16
|
|
|
* - ●=====○ |
|
17
|
|
|
* 3. If it is a day/month/year, it usually includes an end point, for example, January to March is 3 months. |
|
18
|
|
|
* - ●=====● |
|
19
|
|
|
* 4. When processing, assume that the $timePeriods format is correct. If necessary, you need to call the verification function to verify the data. |
|
20
|
|
|
* 5. Ensure performance by keeping the $timePeriods format correct: |
|
21
|
|
|
* - a. When getting the raw $timePeriods, sort out it by format(), filter(), union(). |
|
22
|
|
|
* - b. Handle $timePeriods using only the functions provided by TimePeriodHelper (Will not break the format, sort) |
|
23
|
|
|
* - c. When you achieve the two operations described above, you can turn off auto sort out (TimePeriodHelper::setSortOut(false)) to improve performance. |
|
24
|
|
|
* |
|
25
|
|
|
* @version 0.5.3 |
|
26
|
|
|
* @author Mars Hung <[email protected]> |
|
27
|
|
|
* @see https://github.com/marshung24/TimePeriodHelper |
|
28
|
|
|
*/ |
|
29
|
|
|
class TimePeriodHelper extends Base |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* ************************************************ |
|
34
|
|
|
* ************** Operation Function ************** |
|
35
|
|
|
* ************************************************ |
|
36
|
|
|
*/ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Sort time periods (Order by ASC) |
|
40
|
|
|
* |
|
41
|
|
|
* 1. When sorting, sort the start time first, if the start time is the same, then sort the end time |
|
42
|
|
|
* 2. Sort Priority: Start Time => End Time |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $timePeriods |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function sort(array $timePeriods) |
|
48
|
|
|
{ |
|
49
|
|
|
return DataProcess::sort($timePeriods); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Union one or more time periods |
|
54
|
|
|
* |
|
55
|
|
|
* 1. Sort and merge one or more time periods with contacts |
|
56
|
|
|
* 2. TimePeriodHelper::union($timePeriods1, $timePeriods2, $timePeriods3, ......); |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $timePeriods |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function union() |
|
62
|
|
|
{ |
|
63
|
|
|
return \call_user_func_array(['\marsapp\helper\timeperiod\classes\LogicalProcess', 'union'], func_get_args()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Computes the difference of time periods |
|
68
|
|
|
* |
|
69
|
|
|
* 1. Compares $timePeriods1 against $timePeriods2 and returns the values in $timePeriods1 that are not present in $timePeriods2. |
|
70
|
|
|
* 2. e.g. TimePeriodHelper::diff($timePeriods1, $timePeriods2); |
|
71
|
|
|
* 3. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $timePeriods1 |
|
74
|
|
|
* @param array $timePeriods2 |
|
75
|
|
|
* @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function diff(array $timePeriods1, array $timePeriods2, $sortOut = 'default') |
|
79
|
|
|
{ |
|
80
|
|
|
return LogicalProcess::diff($timePeriods1, $timePeriods2, $sortOut); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Computes the intersection of time periods |
|
85
|
|
|
* |
|
86
|
|
|
* 1. e.g. TimePeriodHelper::intersect($timePeriods1, $timePeriods2); |
|
87
|
|
|
* 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $timePeriods1 |
|
90
|
|
|
* @param array $timePeriods2 |
|
91
|
|
|
* @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function intersect(array $timePeriods1, array $timePeriods2, $sortOut = 'default') |
|
95
|
|
|
{ |
|
96
|
|
|
return LogicalProcess::intersect($timePeriods1, $timePeriods2, $sortOut); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Time period is overlap |
|
101
|
|
|
* |
|
102
|
|
|
* 1. Determine if there is overlap between the two time periods |
|
103
|
|
|
* 2. Only when there is no intersection, no data is needed. |
|
104
|
|
|
* 3. Logic is similar to intersect. |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $timePeriods1 |
|
107
|
|
|
* @param array $timePeriods2 |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function isOverlap(array $timePeriods1, array $timePeriods2) |
|
111
|
|
|
{ |
|
112
|
|
|
return LogicalProcess::isOverlap($timePeriods1, $timePeriods2); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* The time period is in contact with the specified time (time period) |
|
117
|
|
|
* |
|
118
|
|
|
* @param array $timePeriods |
|
119
|
|
|
* @param string $sDateTime |
|
120
|
|
|
* @param string $eDateTime |
|
121
|
|
|
* @param bool|string $sortOut |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function contact(array $timePeriods, $sDateTime, $eDateTime = null, $sortOut = 'default') |
|
125
|
|
|
{ |
|
126
|
|
|
return LogicalProcess::contact($timePeriods, $sDateTime, $eDateTime, $sortOut); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Time period greater than the specified time |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $timePeriods |
|
133
|
|
|
* @param string $refDatetime Specified time to compare against |
|
134
|
|
|
* @param bool $fullTimePeriod Get only the full time period |
|
135
|
|
|
* @param bool|string $sortOut |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function greaterThan(array $timePeriods, $refDatetime, $fullTimePeriod = true, $sortOut = 'default') |
|
139
|
|
|
{ |
|
140
|
|
|
return LogicalProcess::greaterThan($timePeriods, $refDatetime, $fullTimePeriod, $sortOut); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Time period less than the specified time |
|
145
|
|
|
* |
|
146
|
|
|
* @param array $timePeriods |
|
147
|
|
|
* @param string $refDatetime Specified time to compare against |
|
148
|
|
|
* @param bool $fullTimePeriod Get only the full time period |
|
149
|
|
|
* @param bool|string $sortOut |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
|
|
public static function lessThan(array $timePeriods, $refDatetime, $fullTimePeriod = true, $sortOut = 'default') |
|
153
|
|
|
{ |
|
154
|
|
|
return LogicalProcess::lessThan($timePeriods, $refDatetime, $fullTimePeriod, $sortOut); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Fill time periods |
|
159
|
|
|
* |
|
160
|
|
|
* Leaving only the first start time and the last end time |
|
161
|
|
|
* |
|
162
|
|
|
* @param array $timePeriods |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public static function fill(array $timePeriods) |
|
166
|
|
|
{ |
|
167
|
|
|
return DataProcess::fill($timePeriods); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Get gap time periods of multiple sets of time periods |
|
172
|
|
|
* |
|
173
|
|
|
* 1. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct. |
|
174
|
|
|
* |
|
175
|
|
|
* @param array $timePeriods |
|
176
|
|
|
* @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
public static function gap(array $timePeriods, $sortOut = 'default') |
|
180
|
|
|
{ |
|
181
|
|
|
return DataProcess::gap($timePeriods, $sortOut); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Calculation period total time |
|
186
|
|
|
* |
|
187
|
|
|
* 1. You can specify the smallest unit (from setUnit()) |
|
188
|
|
|
* 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct. |
|
189
|
|
|
* 3. approximation: chop off |
|
190
|
|
|
* |
|
191
|
|
|
* @param array $timePeriods |
|
192
|
|
|
* @param int $precision |
|
193
|
|
|
* Optional decimal places for the decimal point |
|
194
|
|
|
* @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
|
195
|
|
|
* @return number |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function time(array $timePeriods, $precision = 0, $sortOut = 'default') |
|
198
|
|
|
{ |
|
199
|
|
|
return DataProcess::time($timePeriods, $precision, $sortOut); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Cut the time period of the specified length of time |
|
204
|
|
|
* |
|
205
|
|
|
* 1. You can specify the smallest unit (from setUnit()) |
|
206
|
|
|
* 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct. |
|
207
|
|
|
* |
|
208
|
|
|
* @param array $timePeriods |
|
209
|
|
|
* @param number $time |
|
210
|
|
|
* Specified length of time |
|
211
|
|
|
* @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
|
|
public static function cut(array $timePeriods, $time, $sortOut = 'default') |
|
215
|
|
|
{ |
|
216
|
|
|
return DataProcess::cut($timePeriods, $time, $sortOut); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Increase the time period of the specified length of time after the last time period |
|
221
|
|
|
* |
|
222
|
|
|
* 1. You can specify the smallest unit (from setUnit()) |
|
223
|
|
|
* 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct. |
|
224
|
|
|
* |
|
225
|
|
|
* @param array $timePeriods |
|
226
|
|
|
* @param number $time |
|
227
|
|
|
* Specified length of time (default uint:second) |
|
228
|
|
|
* @param number $interval |
|
229
|
|
|
* Interval with existing time period |
|
230
|
|
|
* @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
|
231
|
|
|
* @return array |
|
232
|
|
|
*/ |
|
233
|
|
|
public static function extend(array $timePeriods, $time, $interval = 0, $sortOut = 'default') |
|
234
|
|
|
{ |
|
235
|
|
|
return DataProcess::extend($timePeriods, $time, $interval, $sortOut); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Shorten the specified length of time from behind |
|
240
|
|
|
* |
|
241
|
|
|
* 1. You can specify the smallest unit (from setUnit()) |
|
242
|
|
|
* 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct. |
|
243
|
|
|
* |
|
244
|
|
|
* @param array $timePeriods |
|
245
|
|
|
* @param number $time |
|
246
|
|
|
* Specified length of time (default uint:second) |
|
247
|
|
|
* @param bool $crossperiod |
|
248
|
|
|
* Whether to shorten across time |
|
249
|
|
|
* @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
|
250
|
|
|
* @return array |
|
251
|
|
|
*/ |
|
252
|
|
|
public static function shorten(array $timePeriods, $time, $crossperiod = true, $sortOut = 'default') |
|
253
|
|
|
{ |
|
254
|
|
|
return DataProcess::shorten($timePeriods, $time, $crossperiod, $sortOut); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Transform format |
|
259
|
|
|
* |
|
260
|
|
|
* @param array $timePeriods |
|
261
|
|
|
* @param string $unit Time unit, if default,use class options setting |
|
262
|
|
|
* @return array |
|
263
|
|
|
*/ |
|
264
|
|
|
public static function format(array $timePeriods, $unit = 'default') |
|
265
|
|
|
{ |
|
266
|
|
|
return DataProcess::format($timePeriods, $unit); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Validate time period |
|
271
|
|
|
* |
|
272
|
|
|
* Verify format, size, start/end time |
|
273
|
|
|
* |
|
274
|
|
|
* @param mixed|array $timePeriods |
|
275
|
|
|
* @throws \Exception |
|
276
|
|
|
* @return bool |
|
277
|
|
|
*/ |
|
278
|
|
|
public static function validate($timePeriods) |
|
279
|
|
|
{ |
|
280
|
|
|
return DataProcess::validate($timePeriods); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Remove invalid time period |
|
285
|
|
|
* |
|
286
|
|
|
* 1. Verify format, size, start/end time, and remove invalid. |
|
287
|
|
|
* 2. time carry problem processing, e.g. 2019-01-01 24:00:00 => 2019-01-02 00:00:00 |
|
288
|
|
|
* |
|
289
|
|
|
* @param mixed|array $timePeriods |
|
290
|
|
|
* @throws \Exception |
|
291
|
|
|
* @return array |
|
292
|
|
|
*/ |
|
293
|
|
|
public static function filter($timePeriods) |
|
294
|
|
|
{ |
|
295
|
|
|
return DataProcess::filter($timePeriods); |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|