Line data Source code
1 : /*
2 : * Copyright (c) 2018 Cisco and/or its affiliates.
3 : * Licensed under the Apache License, Version 2.0 (the "License");
4 : * you may not use this file except in compliance with the License.
5 : * You may obtain a copy of the License at:
6 : *
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : *
9 : * Unless required by applicable law or agreed to in writing, software
10 : * distributed under the License is distributed on an "AS IS" BASIS,
11 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : * See the License for the specific language governing permissions and
13 : * limitations under the License.
14 : */
15 :
16 : #include <vppinfra/time_range.h>
17 :
18 : __clib_export void
19 90 : clib_timebase_init (clib_timebase_t * tb, i32 timezone_offset_in_hours,
20 : clib_timebase_daylight_time_t daylight_type,
21 : clib_time_t * clib_time)
22 : {
23 90 : clib_memset (tb, 0, sizeof (*tb));
24 :
25 90 : if (clib_time == 0)
26 : {
27 0 : tb->clib_time = clib_mem_alloc_aligned
28 : (sizeof (*clib_time), CLIB_CACHE_LINE_BYTES);
29 0 : memset (tb->clib_time, 0, sizeof (*clib_time));
30 0 : clib_time_init (tb->clib_time);
31 : }
32 : else
33 90 : tb->clib_time = clib_time;
34 :
35 90 : tb->timezone_offset = ((f64) (timezone_offset_in_hours)) * 3600.0;
36 90 : tb->daylight_time_type = daylight_type;
37 90 : switch (tb->daylight_time_type)
38 : {
39 88 : case CLIB_TIMEBASE_DAYLIGHT_NONE:
40 88 : tb->summer_offset = 0.0;
41 88 : break;
42 2 : case CLIB_TIMEBASE_DAYLIGHT_USA:
43 2 : tb->summer_offset = 3600.0;
44 2 : break;
45 0 : default:
46 0 : clib_warning ("unknown daylight type %d", tb->daylight_time_type);
47 0 : tb->daylight_time_type = CLIB_TIMEBASE_DAYLIGHT_NONE;
48 0 : tb->summer_offset = 0.0;
49 : }
50 90 : }
51 :
52 : const static u32 days_per_month[] = {
53 : 31, /* Jan */
54 : 28, /* Feb */
55 : 31, /* Mar */
56 : 30, /* Apr */
57 : 31, /* May */
58 : 30, /* Jun */
59 : 31, /* Jul */
60 : 31, /* Aug */
61 : 30, /* Sep */
62 : 31, /* Oct */
63 : 30, /* Nov */
64 : 31, /* Dec */
65 : };
66 :
67 : const static char *month_short_names[] = {
68 : "Jan",
69 : "Feb",
70 : "Mar",
71 : "Apr",
72 : "May",
73 : "Jun",
74 : "Jul",
75 : "Aug",
76 : "Sep",
77 : "Oct",
78 : "Nov",
79 : "Dec",
80 : };
81 :
82 : const static char *day_names_epoch_order[] = {
83 : "Thu",
84 : "Fri",
85 : "Sat",
86 : "Sun",
87 : "Mon",
88 : "Tue",
89 : "Wed",
90 : };
91 :
92 : const static char *day_names_calendar_order[] = {
93 : "Sun",
94 : "Mon",
95 : "Tue",
96 : "Wed",
97 : "Thu",
98 : "Fri",
99 : "Sat",
100 : };
101 :
102 :
103 : __clib_export void
104 863 : clib_timebase_time_to_components (f64 now, clib_timebase_component_t * cp)
105 : {
106 : u32 year, month, hours, minutes, seconds, nanoseconds;
107 : u32 days_in_year, days_in_month, day_of_month;
108 : u32 days_since_epoch;
109 : u32 day_name_index;
110 :
111 : /* Unix epoch is 1/1/1970 00:00:00.00, a Thursday */
112 :
113 863 : year = 1970;
114 863 : days_since_epoch = 0;
115 :
116 : do
117 : {
118 45943 : days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
119 45943 : days_since_epoch += days_in_year;
120 45943 : now = now - ((f64) days_in_year) * 86400.0;
121 45943 : year++;
122 : }
123 45943 : while (now > 0.0);
124 :
125 863 : days_since_epoch -= days_in_year;
126 863 : now += ((f64) days_in_year) * 86400;
127 863 : year--;
128 :
129 863 : month = 0;
130 :
131 : do
132 : {
133 5224 : days_in_month = days_per_month[month];
134 5224 : if (month == 1 && clib_timebase_is_leap_year (year))
135 0 : days_in_month++;
136 :
137 5224 : days_since_epoch += days_in_month;
138 5224 : now = now - ((f64) days_in_month) * 86400.0;
139 5224 : month++;
140 : }
141 5224 : while (now > 0.0);
142 :
143 863 : days_since_epoch -= days_in_month;
144 863 : now += ((f64) days_in_month) * 86400;
145 863 : month--;
146 :
147 863 : day_of_month = 1;
148 : do
149 : {
150 7382 : now = now - 86400;
151 7382 : day_of_month++;
152 7382 : days_since_epoch++;
153 : }
154 7382 : while (now > 0.0);
155 :
156 863 : day_of_month--;
157 863 : days_since_epoch--;
158 863 : now += 86400.0;
159 :
160 863 : day_name_index = days_since_epoch % 7;
161 :
162 863 : hours = (u32) (now / (3600.0));
163 863 : now -= (f64) (hours * 3600);
164 :
165 863 : minutes = (u32) (now / 60.0);
166 863 : now -= (f64) (minutes * 60);
167 :
168 863 : seconds = (u32) (now);
169 863 : now -= (f64) (seconds);
170 :
171 863 : nanoseconds = (f64) (now * 1e9);
172 :
173 863 : cp->year = year;
174 863 : cp->month = month;
175 863 : cp->day = day_of_month;
176 863 : cp->day_name_index = day_name_index;
177 863 : cp->hour = hours;
178 863 : cp->minute = minutes;
179 863 : cp->second = seconds;
180 863 : cp->nanosecond = nanoseconds;
181 863 : cp->fractional_seconds = now;
182 863 : }
183 :
184 : __clib_export f64
185 187 : clib_timebase_components_to_time (clib_timebase_component_t * cp)
186 : {
187 187 : f64 now = 0;
188 : u32 year, days_in_year, month, days_in_month;
189 :
190 187 : year = 1970;
191 :
192 9326 : while (year < cp->year)
193 : {
194 9139 : days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
195 9139 : now += ((f64) days_in_year) * 86400.0;
196 9139 : year++;
197 : }
198 :
199 187 : month = 0;
200 :
201 730 : while (month < cp->month)
202 : {
203 543 : days_in_month = days_per_month[month];
204 543 : if (month == 1 && clib_timebase_is_leap_year (year))
205 0 : days_in_month++;
206 :
207 543 : now += ((f64) days_in_month) * 86400.0;
208 543 : month++;
209 : }
210 :
211 187 : now += ((f64) cp->day - 1) * 86400.0;
212 187 : now += ((f64) cp->hour) * 3600.0;
213 187 : now += ((f64) cp->minute) * 60.0;
214 187 : now += ((f64) cp->second);
215 187 : now += ((f64) cp->nanosecond) * 1e-9;
216 :
217 187 : return (now);
218 : }
219 :
220 : __clib_export f64
221 2 : clib_timebase_find_sunday_midnight (f64 start_time)
222 : {
223 2 : clib_timebase_component_t _c, *cp = &_c;
224 :
225 2 : clib_timebase_time_to_components (start_time, cp);
226 :
227 : /* back up to midnight */
228 2 : cp->hour = cp->minute = cp->second = 0;
229 :
230 2 : start_time = clib_timebase_components_to_time (cp);
231 :
232 8 : while (cp->day_name_index != 3 /* sunday */ )
233 : {
234 : /* Back up one day */
235 6 : start_time -= 86400.0;
236 6 : clib_timebase_time_to_components (start_time, cp);
237 : }
238 : /* Clean up residual fraction */
239 2 : start_time -= cp->fractional_seconds;
240 2 : start_time += 1e-6; /* 1us inside Sunday */
241 :
242 2 : return (start_time);
243 : }
244 :
245 : f64
246 12 : clib_timebase_offset_from_sunday (u8 * day)
247 : {
248 : int i;
249 :
250 44 : for (i = 0; i < ARRAY_LEN (day_names_calendar_order); i++)
251 : {
252 44 : if (!strncmp ((char *) day, day_names_calendar_order[i], 3))
253 12 : return ((f64) i) * 86400.0;
254 : }
255 0 : return 0.0;
256 : }
257 :
258 :
259 : __clib_export u8 *
260 87 : format_clib_timebase_time (u8 * s, va_list * args)
261 : {
262 87 : f64 now = va_arg (*args, f64);
263 87 : clib_timebase_component_t _c, *cp = &_c;
264 :
265 87 : clib_timebase_time_to_components (now, cp);
266 :
267 87 : s = format (s, "%s, %u %s %u %u:%02u:%02u",
268 87 : day_names_epoch_order[cp->day_name_index],
269 : cp->day,
270 87 : month_short_names[cp->month],
271 : cp->year, cp->hour, cp->minute, cp->second);
272 87 : return (s);
273 : }
274 :
275 : uword
276 7 : unformat_clib_timebase_range_hms (unformat_input_t * input, va_list * args)
277 : {
278 7 : clib_timebase_range_t *rp = va_arg (*args, clib_timebase_range_t *);
279 7 : clib_timebase_component_t _c, *cp = &_c;
280 : u32 start_hour, start_minute, start_second;
281 : u32 end_hour, end_minute, end_second;
282 :
283 7 : start_hour = start_minute = start_second
284 7 : = end_hour = end_minute = end_second = 0;
285 :
286 7 : if (unformat (input, "%u:%u:%u - %u:%u:%u",
287 : &start_hour, &start_minute, &start_second,
288 : &end_hour, &end_minute, &end_second))
289 : ;
290 7 : else if (unformat (input, "%u:%u - %u:%u",
291 : &start_hour, &start_minute, &end_hour, &end_minute))
292 : ;
293 3 : else if (unformat (input, "%u - %u", &start_hour, &end_hour))
294 : ;
295 : else
296 0 : return 0;
297 :
298 7 : clib_timebase_time_to_components (1e-6, cp);
299 :
300 7 : cp->hour = start_hour;
301 7 : cp->minute = start_minute;
302 7 : cp->second = start_second;
303 :
304 7 : rp->start = clib_timebase_components_to_time (cp);
305 :
306 7 : cp->hour = end_hour;
307 7 : cp->minute = end_minute;
308 7 : cp->second = end_second;
309 :
310 7 : rp->end = clib_timebase_components_to_time (cp);
311 :
312 7 : return 1;
313 : }
314 :
315 : __clib_export uword
316 4 : unformat_clib_timebase_range_vector (unformat_input_t * input, va_list * args)
317 : {
318 4 : clib_timebase_range_t **rpp = va_arg (*args, clib_timebase_range_t **);
319 4 : clib_timebase_range_t _tmp, *tmp = &_tmp;
320 : clib_timebase_range_t *rp, *new_rp;
321 4 : int day_range_match = 0;
322 4 : int time_range_match = 0;
323 : f64 range_start_time_offset;
324 : f64 range_end_time_offset;
325 : f64 now;
326 4 : u8 *start_day = 0, *end_day = 0;
327 :
328 4 : rp = *rpp;
329 :
330 : while (1)
331 : {
332 18 : if (!day_range_match
333 11 : && unformat (input, "%s - %s", &start_day, &end_day))
334 : {
335 : range_start_time_offset
336 5 : = clib_timebase_offset_from_sunday (start_day);
337 5 : range_end_time_offset = clib_timebase_offset_from_sunday (end_day);
338 5 : vec_free (start_day);
339 5 : vec_free (end_day);
340 5 : day_range_match = 1;
341 5 : time_range_match = 0;
342 : }
343 13 : else if (!day_range_match && unformat (input, "%s", &start_day))
344 : {
345 : range_start_time_offset
346 2 : = clib_timebase_offset_from_sunday (start_day);
347 2 : range_end_time_offset = range_start_time_offset + 86399.0;
348 2 : day_range_match = 1;
349 2 : vec_free (start_day);
350 2 : day_range_match = 1;
351 2 : time_range_match = 0;
352 : }
353 18 : else if (day_range_match &&
354 7 : unformat (input, "%U", unformat_clib_timebase_range_hms, tmp))
355 : {
356 : /* Across the week... */
357 33 : for (now = range_start_time_offset; now <= range_end_time_offset;
358 26 : now += 86400.0)
359 : {
360 26 : vec_add2 (rp, new_rp, 1);
361 26 : new_rp->start = now + tmp->start;
362 26 : new_rp->end = now + tmp->end;
363 : }
364 7 : day_range_match = 0;
365 7 : time_range_match = 1;
366 : }
367 4 : else if (time_range_match)
368 4 : break;
369 : else
370 : {
371 0 : vec_free (rp);
372 0 : *rpp = 0;
373 0 : return 0;
374 : }
375 : }
376 :
377 4 : if (time_range_match)
378 : {
379 4 : *rpp = rp;
380 4 : return 1;
381 : }
382 : else
383 : {
384 0 : vec_free (rp);
385 0 : *rpp = 0;
386 0 : return 0;
387 : }
388 : }
389 :
390 : __clib_export f64
391 42 : clib_timebase_summer_offset (clib_timebase_t * tb, f64 now)
392 : {
393 42 : clib_timebase_component_t _c, *cp = &_c;
394 : f64 second_sunday_march_2am;
395 : f64 first_sunday_november_2am;
396 :
397 42 : if (PREDICT_TRUE
398 : (now >= tb->cached_year_start && now <= tb->cached_year_end))
399 : {
400 0 : if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
401 0 : return tb->summer_offset;
402 : else
403 0 : return (0.0);
404 : }
405 :
406 42 : clib_timebase_time_to_components (now, cp);
407 :
408 42 : cp->month = 0;
409 42 : cp->day = 1;
410 42 : cp->hour = 0;
411 42 : cp->minute = 0;
412 42 : cp->second = 1;
413 :
414 42 : tb->cached_year_start = clib_timebase_components_to_time (cp);
415 :
416 42 : cp->year += 1;
417 :
418 42 : tb->cached_year_end = clib_timebase_components_to_time (cp);
419 :
420 42 : cp->year -= 1;
421 :
422 : /* Search for the second sunday in march, 2am */
423 42 : cp->month = 2;
424 42 : cp->day = 1;
425 42 : cp->hour = 2;
426 42 : cp->second = 0;
427 42 : cp->nanosecond = 1;
428 :
429 : /* March 1st will never be the second sunday... */
430 42 : second_sunday_march_2am = clib_timebase_components_to_time (cp);
431 42 : cp->day_name_index = 0;
432 :
433 : /* Find the first sunday */
434 : do
435 : {
436 211 : clib_timebase_time_to_components (second_sunday_march_2am, cp);
437 211 : second_sunday_march_2am += 86400.0;
438 : }
439 211 : while (cp->day_name_index != 3 /* sunday */ );
440 :
441 : /* Find the second sunday */
442 : do
443 : {
444 294 : clib_timebase_time_to_components (second_sunday_march_2am, cp);
445 294 : second_sunday_march_2am += 86400.0;
446 : }
447 294 : while (cp->day_name_index != 3 /* sunday */ );
448 :
449 42 : second_sunday_march_2am -= 86400.0;
450 :
451 42 : tb->cached_summer_start = second_sunday_march_2am;
452 :
453 : /* Find the first sunday in November, which can easily be 11/1 */
454 42 : cp->month = 10;
455 42 : cp->day = 1;
456 :
457 42 : first_sunday_november_2am = clib_timebase_components_to_time (cp);
458 42 : clib_timebase_time_to_components (first_sunday_november_2am, cp);
459 :
460 211 : while (cp->day_name_index != 3 /* sunday */ )
461 : {
462 169 : first_sunday_november_2am += 86400.0;
463 169 : clib_timebase_time_to_components (first_sunday_november_2am, cp);
464 : }
465 :
466 42 : tb->cached_summer_end = first_sunday_november_2am;
467 :
468 42 : if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
469 41 : return tb->summer_offset;
470 : else
471 1 : return (0.0);
472 : }
473 :
474 : /*
475 : * fd.io coding-style-patch-verification: ON
476 : *
477 : * Local Variables:
478 : * eval: (c-set-style "gnu")
479 : * End:
480 : */
|