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,
21 clib_time_t * clib_time)
23 clib_memset (tb, 0, sizeof (*tb));
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);
33 tb->clib_time = clib_time;
35 tb->timezone_offset = ((f64) (timezone_offset_in_hours)) * 3600.0;
36 tb->daylight_time_type = daylight_type;
37 switch (tb->daylight_time_type)
39 case CLIB_TIMEBASE_DAYLIGHT_NONE:
40 tb->summer_offset = 0.0;
42 case CLIB_TIMEBASE_DAYLIGHT_USA:
43 tb->summer_offset = 3600.0;
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;
52 const static u32 days_per_month[] = {
67 const static char *month_short_names[] = {
82 const static char *day_names_epoch_order[] = {
92 const static char *day_names_calendar_order[] = {
104 clib_timebase_time_to_components (f64 now, clib_timebase_component_t * cp)
106 u32 year, month, hours, minutes, seconds, nanoseconds;
107 u32 days_in_year, days_in_month, day_of_month;
108 u32 days_since_epoch;
111 /* Unix epoch is 1/1/1970 00:00:00.00, a Thursday */
114 days_since_epoch = 0;
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;
125 days_since_epoch -= days_in_year;
126 now += ((f64) days_in_year) * 86400;
133 days_in_month = days_per_month[month];
134 if (month == 1 && clib_timebase_is_leap_year (year))
137 days_since_epoch += days_in_month;
138 now = now - ((f64) days_in_month) * 86400.0;
143 days_since_epoch -= days_in_month;
144 now += ((f64) days_in_month) * 86400;
160 day_name_index = days_since_epoch % 7;
162 hours = (u32) (now / (3600.0));
163 now -= (f64) (hours * 3600);
165 minutes = (u32) (now / 60.0);
166 now -= (f64) (minutes * 60);
168 seconds = (u32) (now);
169 now -= (f64) (seconds);
171 nanoseconds = (f64) (now * 1e9);
175 cp->day = day_of_month;
176 cp->day_name_index = day_name_index;
178 cp->minute = minutes;
179 cp->second = seconds;
180 cp->nanosecond = nanoseconds;
181 cp->fractional_seconds = now;
185 clib_timebase_components_to_time (clib_timebase_component_t * cp)
188 u32 year, days_in_year, month, days_in_month;
192 while (year < cp->year)
194 days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
195 now += ((f64) days_in_year) * 86400.0;
201 while (month < cp->month)
203 days_in_month = days_per_month[month];
204 if (month == 1 && clib_timebase_is_leap_year (year))
207 now += ((f64) days_in_month) * 86400.0;
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;
221 clib_timebase_find_sunday_midnight (f64 start_time)
223 clib_timebase_component_t _c, *cp = &_c;
225 clib_timebase_time_to_components (start_time, cp);
227 /* back up to midnight */
228 cp->hour = cp->minute = cp->second = 0;
230 start_time = clib_timebase_components_to_time (cp);
232 while (cp->day_name_index != 3 /* sunday */ )
234 /* Back up one day */
235 start_time -= 86400.0;
236 clib_timebase_time_to_components (start_time, cp);
238 /* Clean up residual fraction */
239 start_time -= cp->fractional_seconds;
240 start_time += 1e-6; /* 1us inside Sunday */
246 clib_timebase_offset_from_sunday (u8 * day)
250 for (i = 0; i < ARRAY_LEN (day_names_calendar_order); i++)
252 if (!strncmp ((char *) day, day_names_calendar_order[i], 3))
253 return ((f64) i) * 86400.0;
260 format_clib_timebase_time (u8 * s, va_list * args)
262 f64 now = va_arg (*args, f64);
263 clib_timebase_component_t _c, *cp = &_c;
265 clib_timebase_time_to_components (now, cp);
267 s = format (s, "%s, %u %s %u %u:%02u:%02u",
268 day_names_epoch_order[cp->day_name_index],
270 month_short_names[cp->month],
271 cp->year, cp->hour, cp->minute, cp->second);
276 unformat_clib_timebase_range_hms (unformat_input_t * input, va_list * args)
278 clib_timebase_range_t *rp = va_arg (*args, clib_timebase_range_t *);
279 clib_timebase_component_t _c, *cp = &_c;
280 u32 start_hour, start_minute, start_second;
281 u32 end_hour, end_minute, end_second;
283 start_hour = start_minute = start_second
284 = end_hour = end_minute = end_second = 0;
286 if (unformat (input, "%u:%u:%u - %u:%u:%u",
287 &start_hour, &start_minute, &start_second,
288 &end_hour, &end_minute, &end_second))
290 else if (unformat (input, "%u:%u - %u:%u",
291 &start_hour, &start_minute, &end_hour, &end_minute))
293 else if (unformat (input, "%u - %u", &start_hour, &end_hour))
298 clib_timebase_time_to_components (1e-6, cp);
300 cp->hour = start_hour;
301 cp->minute = start_minute;
302 cp->second = start_second;
304 rp->start = clib_timebase_components_to_time (cp);
307 cp->minute = end_minute;
308 cp->second = end_second;
310 rp->end = clib_timebase_components_to_time (cp);
316 unformat_clib_timebase_range_vector (unformat_input_t * input, va_list * args)
318 clib_timebase_range_t **rpp = va_arg (*args, clib_timebase_range_t **);
319 clib_timebase_range_t _tmp, *tmp = &_tmp;
320 clib_timebase_range_t *rp, *new_rp;
321 int day_range_match = 0;
322 int time_range_match = 0;
323 f64 range_start_time_offset;
324 f64 range_end_time_offset;
326 u8 *start_day = 0, *end_day = 0;
333 && unformat (input, "%s - %s", &start_day, &end_day))
335 range_start_time_offset
336 = clib_timebase_offset_from_sunday (start_day);
337 range_end_time_offset = clib_timebase_offset_from_sunday (end_day);
338 vec_free (start_day);
341 time_range_match = 0;
343 else if (!day_range_match && unformat (input, "%s", &start_day))
345 range_start_time_offset
346 = clib_timebase_offset_from_sunday (start_day);
347 range_end_time_offset = range_start_time_offset + 86399.0;
349 vec_free (start_day);
351 time_range_match = 0;
353 else if (day_range_match &&
354 unformat (input, "%U", unformat_clib_timebase_range_hms, tmp))
356 /* Across the week... */
357 for (now = range_start_time_offset; now <= range_end_time_offset;
360 vec_add2 (rp, new_rp, 1);
361 new_rp->start = now + tmp->start;
362 new_rp->end = now + tmp->end;
365 time_range_match = 1;
367 else if (time_range_match)
377 if (time_range_match)
391 clib_timebase_summer_offset (clib_timebase_t * tb, f64 now)
393 clib_timebase_component_t _c, *cp = &_c;
394 f64 second_sunday_march_2am;
395 f64 first_sunday_november_2am;
398 (now >= tb->cached_year_start && now <= tb->cached_year_end))
400 if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
401 return tb->summer_offset;
406 clib_timebase_time_to_components (now, cp);
414 tb->cached_year_start = clib_timebase_components_to_time (cp);
418 tb->cached_year_end = clib_timebase_components_to_time (cp);
422 /* Search for the second sunday in march, 2am */
429 /* March 1st will never be the second sunday... */
430 second_sunday_march_2am = clib_timebase_components_to_time (cp);
431 cp->day_name_index = 0;
433 /* Find the first 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 /* Find the second sunday */
444 clib_timebase_time_to_components (second_sunday_march_2am, cp);
445 second_sunday_march_2am += 86400.0;
447 while (cp->day_name_index != 3 /* sunday */ );
449 second_sunday_march_2am -= 86400.0;
451 tb->cached_summer_start = second_sunday_march_2am;
453 /* Find the first sunday in November, which can easily be 11/1 */
457 first_sunday_november_2am = clib_timebase_components_to_time (cp);
458 clib_timebase_time_to_components (first_sunday_november_2am, cp);
460 while (cp->day_name_index != 3 /* sunday */ )
462 first_sunday_november_2am += 86400.0;
463 clib_timebase_time_to_components (first_sunday_november_2am, cp);
466 tb->cached_summer_end = first_sunday_november_2am;
468 if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
469 return tb->summer_offset;
475 * fd.io coding-style-patch-verification: ON
478 * eval: (c-set-style "gnu")