blob: 54f5629641a20f35546aa3fc9f2996c2404af060 [file] [log] [blame]
Dave Baracha98c4032018-06-06 10:52:08 -04001/*
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
Damjan Mariondae1c7e2020-10-17 13:32:25 +020018__clib_export void
Dave Baracha98c4032018-06-06 10:52:08 -040019clib_timebase_init (clib_timebase_t * tb, i32 timezone_offset_in_hours,
Dave Barach19718002020-03-11 10:31:36 -040020 clib_timebase_daylight_time_t daylight_type,
21 clib_time_t * clib_time)
Dave Baracha98c4032018-06-06 10:52:08 -040022{
Dave Barachb7b92992018-10-17 10:38:51 -040023 clib_memset (tb, 0, sizeof (*tb));
Dave Baracha98c4032018-06-06 10:52:08 -040024
Dave Barach19718002020-03-11 10:31:36 -040025 if (clib_time == 0)
26 {
27 tb->clib_time = clib_mem_alloc_aligned
28 (sizeof (*clib_time), CLIB_CACHE_LINE_BYTES);
29 memset (tb->clib_time, 0, sizeof (*clib_time));
30 clib_time_init (tb->clib_time);
31 }
32 else
33 tb->clib_time = clib_time;
Dave Baracha98c4032018-06-06 10:52:08 -040034
35 tb->timezone_offset = ((f64) (timezone_offset_in_hours)) * 3600.0;
36 tb->daylight_time_type = daylight_type;
37 switch (tb->daylight_time_type)
38 {
39 case CLIB_TIMEBASE_DAYLIGHT_NONE:
40 tb->summer_offset = 0.0;
41 break;
42 case CLIB_TIMEBASE_DAYLIGHT_USA:
43 tb->summer_offset = 3600.0;
44 break;
45 default:
46 clib_warning ("unknown daylight type %d", tb->daylight_time_type);
47 tb->daylight_time_type = CLIB_TIMEBASE_DAYLIGHT_NONE;
48 tb->summer_offset = 0.0;
49 }
50}
51
52const 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
67const 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
82const static char *day_names_epoch_order[] = {
83 "Thu",
84 "Fri",
85 "Sat",
86 "Sun",
87 "Mon",
88 "Tue",
89 "Wed",
90};
91
92const static char *day_names_calendar_order[] = {
93 "Sun",
94 "Mon",
95 "Tue",
96 "Wed",
97 "Thu",
98 "Fri",
99 "Sat",
100};
101
102
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200103__clib_export void
Dave Baracha98c4032018-06-06 10:52:08 -0400104clib_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 year = 1970;
114 days_since_epoch = 0;
115
116 do
117 {
118 days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
119 days_since_epoch += days_in_year;
120 now = now - ((f64) days_in_year) * 86400.0;
121 year++;
122 }
123 while (now > 0.0);
124
125 days_since_epoch -= days_in_year;
126 now += ((f64) days_in_year) * 86400;
127 year--;
128
129 month = 0;
130
131 do
132 {
133 days_in_month = days_per_month[month];
134 if (month == 1 && clib_timebase_is_leap_year (year))
135 days_in_month++;
136
137 days_since_epoch += days_in_month;
138 now = now - ((f64) days_in_month) * 86400.0;
139 month++;
140 }
141 while (now > 0.0);
142
143 days_since_epoch -= days_in_month;
144 now += ((f64) days_in_month) * 86400;
145 month--;
146
147 day_of_month = 1;
148 do
149 {
150 now = now - 86400;
151 day_of_month++;
152 days_since_epoch++;
153 }
154 while (now > 0.0);
155
156 day_of_month--;
157 days_since_epoch--;
158 now += 86400.0;
159
160 day_name_index = days_since_epoch % 7;
161
162 hours = (u32) (now / (3600.0));
163 now -= (f64) (hours * 3600);
164
165 minutes = (u32) (now / 60.0);
166 now -= (f64) (minutes * 60);
167
168 seconds = (u32) (now);
169 now -= (f64) (seconds);
170
171 nanoseconds = (f64) (now * 1e9);
172
173 cp->year = year;
174 cp->month = month;
175 cp->day = day_of_month;
176 cp->day_name_index = day_name_index;
177 cp->hour = hours;
178 cp->minute = minutes;
179 cp->second = seconds;
180 cp->nanosecond = nanoseconds;
181 cp->fractional_seconds = now;
182}
183
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200184__clib_export f64
Dave Baracha98c4032018-06-06 10:52:08 -0400185clib_timebase_components_to_time (clib_timebase_component_t * cp)
186{
187 f64 now = 0;
188 u32 year, days_in_year, month, days_in_month;
189
190 year = 1970;
191
192 while (year < cp->year)
193 {
194 days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
195 now += ((f64) days_in_year) * 86400.0;
196 year++;
197 }
198
199 month = 0;
200
201 while (month < cp->month)
202 {
203 days_in_month = days_per_month[month];
204 if (month == 1 && clib_timebase_is_leap_year (year))
205 days_in_month++;
206
207 now += ((f64) days_in_month) * 86400.0;
208 month++;
209 }
210
211 now += ((f64) cp->day - 1) * 86400.0;
212 now += ((f64) cp->hour) * 3600.0;
213 now += ((f64) cp->minute) * 60.0;
214 now += ((f64) cp->second);
215 now += ((f64) cp->nanosecond) * 1e-9;
216
217 return (now);
218}
219
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200220__clib_export f64
Dave Baracha98c4032018-06-06 10:52:08 -0400221clib_timebase_find_sunday_midnight (f64 start_time)
222{
223 clib_timebase_component_t _c, *cp = &_c;
224
225 clib_timebase_time_to_components (start_time, cp);
226
227 /* back up to midnight */
228 cp->hour = cp->minute = cp->second = 0;
229
230 start_time = clib_timebase_components_to_time (cp);
231
232 while (cp->day_name_index != 3 /* sunday */ )
233 {
234 /* Back up one day */
235 start_time -= 86400.0;
236 clib_timebase_time_to_components (start_time, cp);
237 }
238 /* Clean up residual fraction */
239 start_time -= cp->fractional_seconds;
240 start_time += 1e-6; /* 1us inside Sunday */
241
242 return (start_time);
243}
244
245f64
246clib_timebase_offset_from_sunday (u8 * day)
247{
248 int i;
249
250 for (i = 0; i < ARRAY_LEN (day_names_calendar_order); i++)
251 {
252 if (!strncmp ((char *) day, day_names_calendar_order[i], 3))
253 return ((f64) i) * 86400.0;
254 }
255 return 0.0;
256}
257
258
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200259__clib_export u8 *
Dave Baracha98c4032018-06-06 10:52:08 -0400260format_clib_timebase_time (u8 * s, va_list * args)
261{
262 f64 now = va_arg (*args, f64);
263 clib_timebase_component_t _c, *cp = &_c;
264
265 clib_timebase_time_to_components (now, cp);
266
Adrian Villin1fb7ae32024-08-19 10:51:59 +0200267 s = format (s, "%s, %02u %s %u %02u:%02u:%02u",
268 day_names_epoch_order[cp->day_name_index], cp->day,
269 month_short_names[cp->month], cp->year, cp->hour, cp->minute,
270 cp->second);
Dave Baracha98c4032018-06-06 10:52:08 -0400271 return (s);
272}
273
274uword
275unformat_clib_timebase_range_hms (unformat_input_t * input, va_list * args)
276{
277 clib_timebase_range_t *rp = va_arg (*args, clib_timebase_range_t *);
278 clib_timebase_component_t _c, *cp = &_c;
279 u32 start_hour, start_minute, start_second;
280 u32 end_hour, end_minute, end_second;
281
282 start_hour = start_minute = start_second
283 = end_hour = end_minute = end_second = 0;
284
285 if (unformat (input, "%u:%u:%u - %u:%u:%u",
286 &start_hour, &start_minute, &start_second,
287 &end_hour, &end_minute, &end_second))
288 ;
289 else if (unformat (input, "%u:%u - %u:%u",
290 &start_hour, &start_minute, &end_hour, &end_minute))
291 ;
292 else if (unformat (input, "%u - %u", &start_hour, &end_hour))
293 ;
294 else
295 return 0;
296
297 clib_timebase_time_to_components (1e-6, cp);
298
299 cp->hour = start_hour;
300 cp->minute = start_minute;
301 cp->second = start_second;
302
303 rp->start = clib_timebase_components_to_time (cp);
304
305 cp->hour = end_hour;
306 cp->minute = end_minute;
307 cp->second = end_second;
308
309 rp->end = clib_timebase_components_to_time (cp);
310
311 return 1;
312}
313
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200314__clib_export uword
Dave Baracha98c4032018-06-06 10:52:08 -0400315unformat_clib_timebase_range_vector (unformat_input_t * input, va_list * args)
316{
317 clib_timebase_range_t **rpp = va_arg (*args, clib_timebase_range_t **);
318 clib_timebase_range_t _tmp, *tmp = &_tmp;
319 clib_timebase_range_t *rp, *new_rp;
320 int day_range_match = 0;
321 int time_range_match = 0;
322 f64 range_start_time_offset;
323 f64 range_end_time_offset;
324 f64 now;
325 u8 *start_day = 0, *end_day = 0;
326
327 rp = *rpp;
328
329 while (1)
330 {
331 if (!day_range_match
332 && unformat (input, "%s - %s", &start_day, &end_day))
333 {
334 range_start_time_offset
335 = clib_timebase_offset_from_sunday (start_day);
336 range_end_time_offset = clib_timebase_offset_from_sunday (end_day);
337 vec_free (start_day);
338 vec_free (end_day);
339 day_range_match = 1;
340 time_range_match = 0;
341 }
342 else if (!day_range_match && unformat (input, "%s", &start_day))
343 {
344 range_start_time_offset
345 = clib_timebase_offset_from_sunday (start_day);
346 range_end_time_offset = range_start_time_offset + 86399.0;
347 day_range_match = 1;
348 vec_free (start_day);
349 day_range_match = 1;
350 time_range_match = 0;
351 }
352 else if (day_range_match &&
353 unformat (input, "%U", unformat_clib_timebase_range_hms, tmp))
354 {
355 /* Across the week... */
356 for (now = range_start_time_offset; now <= range_end_time_offset;
357 now += 86400.0)
358 {
359 vec_add2 (rp, new_rp, 1);
360 new_rp->start = now + tmp->start;
361 new_rp->end = now + tmp->end;
362 }
363 day_range_match = 0;
364 time_range_match = 1;
365 }
366 else if (time_range_match)
367 break;
368 else
369 {
370 vec_free (rp);
371 *rpp = 0;
372 return 0;
373 }
374 }
375
376 if (time_range_match)
377 {
378 *rpp = rp;
379 return 1;
380 }
381 else
382 {
383 vec_free (rp);
384 *rpp = 0;
385 return 0;
386 }
387}
388
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200389__clib_export f64
Dave Baracha98c4032018-06-06 10:52:08 -0400390clib_timebase_summer_offset (clib_timebase_t * tb, f64 now)
391{
392 clib_timebase_component_t _c, *cp = &_c;
393 f64 second_sunday_march_2am;
394 f64 first_sunday_november_2am;
395
396 if (PREDICT_TRUE
397 (now >= tb->cached_year_start && now <= tb->cached_year_end))
398 {
399 if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
400 return tb->summer_offset;
401 else
402 return (0.0);
403 }
404
405 clib_timebase_time_to_components (now, cp);
406
407 cp->month = 0;
408 cp->day = 1;
409 cp->hour = 0;
410 cp->minute = 0;
411 cp->second = 1;
412
413 tb->cached_year_start = clib_timebase_components_to_time (cp);
414
415 cp->year += 1;
416
417 tb->cached_year_end = clib_timebase_components_to_time (cp);
418
419 cp->year -= 1;
420
421 /* Search for the second sunday in march, 2am */
422 cp->month = 2;
423 cp->day = 1;
424 cp->hour = 2;
425 cp->second = 0;
426 cp->nanosecond = 1;
427
428 /* March 1st will never be the second sunday... */
429 second_sunday_march_2am = clib_timebase_components_to_time (cp);
430 cp->day_name_index = 0;
431
432 /* Find the first sunday */
433 do
434 {
435 clib_timebase_time_to_components (second_sunday_march_2am, cp);
436 second_sunday_march_2am += 86400.0;
437 }
438 while (cp->day_name_index != 3 /* sunday */ );
439
440 /* Find the second sunday */
441 do
442 {
443 clib_timebase_time_to_components (second_sunday_march_2am, cp);
444 second_sunday_march_2am += 86400.0;
445 }
446 while (cp->day_name_index != 3 /* sunday */ );
447
448 second_sunday_march_2am -= 86400.0;
449
450 tb->cached_summer_start = second_sunday_march_2am;
451
452 /* Find the first sunday in November, which can easily be 11/1 */
453 cp->month = 10;
454 cp->day = 1;
455
456 first_sunday_november_2am = clib_timebase_components_to_time (cp);
457 clib_timebase_time_to_components (first_sunday_november_2am, cp);
458
459 while (cp->day_name_index != 3 /* sunday */ )
460 {
461 first_sunday_november_2am += 86400.0;
462 clib_timebase_time_to_components (first_sunday_november_2am, cp);
463 }
464
465 tb->cached_summer_end = first_sunday_november_2am;
466
467 if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
468 return tb->summer_offset;
469 else
470 return (0.0);
471}
472
473/*
474 * fd.io coding-style-patch-verification: ON
475 *
476 * Local Variables:
477 * eval: (c-set-style "gnu")
478 * End:
479 */