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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vppinfra/time_range.h>
19 clib_timebase_init (clib_timebase_t * tb, i32 timezone_offset_in_hours,
20 clib_timebase_daylight_time_t daylight_type)
22 memset (tb, 0, sizeof (*tb));
24 clib_time_init (&tb->clib_time);
25 tb->time_zero = unix_time_now ();
27 tb->timezone_offset = ((f64) (timezone_offset_in_hours)) * 3600.0;
28 tb->daylight_time_type = daylight_type;
29 switch (tb->daylight_time_type)
31 case CLIB_TIMEBASE_DAYLIGHT_NONE:
32 tb->summer_offset = 0.0;
34 case CLIB_TIMEBASE_DAYLIGHT_USA:
35 tb->summer_offset = 3600.0;
38 clib_warning ("unknown daylight type %d", tb->daylight_time_type);
39 tb->daylight_time_type = CLIB_TIMEBASE_DAYLIGHT_NONE;
40 tb->summer_offset = 0.0;
44 const static u32 days_per_month[] = {
59 const static char *month_short_names[] = {
74 const static char *day_names_epoch_order[] = {
84 const static char *day_names_calendar_order[] = {
96 clib_timebase_time_to_components (f64 now, clib_timebase_component_t * cp)
98 u32 year, month, hours, minutes, seconds, nanoseconds;
99 u32 days_in_year, days_in_month, day_of_month;
100 u32 days_since_epoch;
103 /* Unix epoch is 1/1/1970 00:00:00.00, a Thursday */
106 days_since_epoch = 0;
110 days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
111 days_since_epoch += days_in_year;
112 now = now - ((f64) days_in_year) * 86400.0;
117 days_since_epoch -= days_in_year;
118 now += ((f64) days_in_year) * 86400;
125 days_in_month = days_per_month[month];
126 if (month == 1 && clib_timebase_is_leap_year (year))
129 days_since_epoch += days_in_month;
130 now = now - ((f64) days_in_month) * 86400.0;
135 days_since_epoch -= days_in_month;
136 now += ((f64) days_in_month) * 86400;
152 day_name_index = days_since_epoch % 7;
154 hours = (u32) (now / (3600.0));
155 now -= (f64) (hours * 3600);
157 minutes = (u32) (now / 60.0);
158 now -= (f64) (minutes * 60);
160 seconds = (u32) (now);
161 now -= (f64) (seconds);
163 nanoseconds = (f64) (now * 1e9);
167 cp->day = day_of_month;
168 cp->day_name_index = day_name_index;
170 cp->minute = minutes;
171 cp->second = seconds;
172 cp->nanosecond = nanoseconds;
173 cp->fractional_seconds = now;
177 clib_timebase_components_to_time (clib_timebase_component_t * cp)
180 u32 year, days_in_year, month, days_in_month;
184 while (year < cp->year)
186 days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
187 now += ((f64) days_in_year) * 86400.0;
193 while (month < cp->month)
195 days_in_month = days_per_month[month];
196 if (month == 1 && clib_timebase_is_leap_year (year))
199 now += ((f64) days_in_month) * 86400.0;
203 now += ((f64) cp->day - 1) * 86400.0;
204 now += ((f64) cp->hour) * 3600.0;
205 now += ((f64) cp->minute) * 60.0;
206 now += ((f64) cp->second);
207 now += ((f64) cp->nanosecond) * 1e-9;
213 clib_timebase_find_sunday_midnight (f64 start_time)
215 clib_timebase_component_t _c, *cp = &_c;
217 clib_timebase_time_to_components (start_time, cp);
219 /* back up to midnight */
220 cp->hour = cp->minute = cp->second = 0;
222 start_time = clib_timebase_components_to_time (cp);
224 while (cp->day_name_index != 3 /* sunday */ )
226 /* Back up one day */
227 start_time -= 86400.0;
228 clib_timebase_time_to_components (start_time, cp);
230 /* Clean up residual fraction */
231 start_time -= cp->fractional_seconds;
232 start_time += 1e-6; /* 1us inside Sunday */
238 clib_timebase_offset_from_sunday (u8 * day)
242 for (i = 0; i < ARRAY_LEN (day_names_calendar_order); i++)
244 if (!strncmp ((char *) day, day_names_calendar_order[i], 3))
245 return ((f64) i) * 86400.0;
252 format_clib_timebase_time (u8 * s, va_list * args)
254 f64 now = va_arg (*args, f64);
255 clib_timebase_component_t _c, *cp = &_c;
257 clib_timebase_time_to_components (now, cp);
259 s = format (s, "%s, %u %s %u %u:%02u:%02u",
260 day_names_epoch_order[cp->day_name_index],
262 month_short_names[cp->month],
263 cp->year, cp->hour, cp->minute, cp->second);
268 unformat_clib_timebase_range_hms (unformat_input_t * input, va_list * args)
270 clib_timebase_range_t *rp = va_arg (*args, clib_timebase_range_t *);
271 clib_timebase_component_t _c, *cp = &_c;
272 u32 start_hour, start_minute, start_second;
273 u32 end_hour, end_minute, end_second;
275 start_hour = start_minute = start_second
276 = end_hour = end_minute = end_second = 0;
278 if (unformat (input, "%u:%u:%u - %u:%u:%u",
279 &start_hour, &start_minute, &start_second,
280 &end_hour, &end_minute, &end_second))
282 else if (unformat (input, "%u:%u - %u:%u",
283 &start_hour, &start_minute, &end_hour, &end_minute))
285 else if (unformat (input, "%u - %u", &start_hour, &end_hour))
290 clib_timebase_time_to_components (1e-6, cp);
292 cp->hour = start_hour;
293 cp->minute = start_minute;
294 cp->second = start_second;
296 rp->start = clib_timebase_components_to_time (cp);
299 cp->minute = end_minute;
300 cp->second = end_second;
302 rp->end = clib_timebase_components_to_time (cp);
308 unformat_clib_timebase_range_vector (unformat_input_t * input, va_list * args)
310 clib_timebase_range_t **rpp = va_arg (*args, clib_timebase_range_t **);
311 clib_timebase_range_t _tmp, *tmp = &_tmp;
312 clib_timebase_range_t *rp, *new_rp;
313 int day_range_match = 0;
314 int time_range_match = 0;
315 f64 range_start_time_offset;
316 f64 range_end_time_offset;
318 u8 *start_day = 0, *end_day = 0;
325 && unformat (input, "%s - %s", &start_day, &end_day))
327 range_start_time_offset
328 = clib_timebase_offset_from_sunday (start_day);
329 range_end_time_offset = clib_timebase_offset_from_sunday (end_day);
330 vec_free (start_day);
333 time_range_match = 0;
335 else if (!day_range_match && unformat (input, "%s", &start_day))
337 range_start_time_offset
338 = clib_timebase_offset_from_sunday (start_day);
339 range_end_time_offset = range_start_time_offset + 86399.0;
341 vec_free (start_day);
343 time_range_match = 0;
345 else if (day_range_match &&
346 unformat (input, "%U", unformat_clib_timebase_range_hms, tmp))
348 /* Across the week... */
349 for (now = range_start_time_offset; now <= range_end_time_offset;
352 vec_add2 (rp, new_rp, 1);
353 new_rp->start = now + tmp->start;
354 new_rp->end = now + tmp->end;
357 time_range_match = 1;
359 else if (time_range_match)
369 if (time_range_match)
383 clib_timebase_summer_offset (clib_timebase_t * tb, f64 now)
385 clib_timebase_component_t _c, *cp = &_c;
386 f64 second_sunday_march_2am;
387 f64 first_sunday_november_2am;
390 (now >= tb->cached_year_start && now <= tb->cached_year_end))
392 if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
393 return tb->summer_offset;
398 clib_timebase_time_to_components (now, cp);
406 tb->cached_year_start = clib_timebase_components_to_time (cp);
410 tb->cached_year_end = clib_timebase_components_to_time (cp);
414 /* Search for the second sunday in march, 2am */
421 /* March 1st will never be the second sunday... */
422 second_sunday_march_2am = clib_timebase_components_to_time (cp);
423 cp->day_name_index = 0;
425 /* Find the first sunday */
428 clib_timebase_time_to_components (second_sunday_march_2am, cp);
429 second_sunday_march_2am += 86400.0;
431 while (cp->day_name_index != 3 /* sunday */ );
433 /* Find the second sunday */
436 clib_timebase_time_to_components (second_sunday_march_2am, cp);
437 second_sunday_march_2am += 86400.0;
439 while (cp->day_name_index != 3 /* sunday */ );
441 second_sunday_march_2am -= 86400.0;
443 tb->cached_summer_start = second_sunday_march_2am;
445 /* Find the first sunday in November, which can easily be 11/1 */
449 first_sunday_november_2am = clib_timebase_components_to_time (cp);
450 clib_timebase_time_to_components (first_sunday_november_2am, cp);
452 while (cp->day_name_index != 3 /* sunday */ )
454 first_sunday_november_2am += 86400.0;
455 clib_timebase_time_to_components (first_sunday_november_2am, cp);
458 tb->cached_summer_end = first_sunday_november_2am;
460 if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
461 return tb->summer_offset;
467 * fd.io coding-style-patch-verification: ON
470 * eval: (c-set-style "gnu")