vppinfra: refactor clib_timebase_t
[vpp.git] / src / vppinfra / time_range.h
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 #ifndef included_time_range_h
17 #define included_time_range_h
18
19 #include <vppinfra/format.h>
20 #include <vppinfra/time.h>
21
22 typedef enum
23 {
24   CLIB_TIMEBASE_DAYLIGHT_NONE = 0,
25   CLIB_TIMEBASE_DAYLIGHT_USA,
26 } clib_timebase_daylight_time_t;
27
28 typedef struct
29 {
30   /* provides f64 seconds since clib_time_init was called */
31   clib_time_t *clib_time;
32   f64 timezone_offset;
33   f64 summer_offset;
34   clib_timebase_daylight_time_t daylight_time_type;
35   f64 cached_year_start;
36   f64 cached_year_end;
37   f64 cached_summer_start;
38   f64 cached_summer_end;
39 } clib_timebase_t;
40
41 typedef struct
42 {
43   u32 year, month, day, hour, minute, second, nanosecond;
44   /* 0 => Thursday */
45   u32 day_name_index;
46   f64 fractional_seconds;
47 } clib_timebase_component_t;
48
49 typedef struct
50 {
51   f64 start, end;
52 } clib_timebase_range_t;
53
54 void clib_timebase_init (clib_timebase_t * tb, i32 timezone_offset_in_hours,
55                          clib_timebase_daylight_time_t daylight_type,
56                          clib_time_t * clib_time);
57
58 void clib_timebase_time_to_components (f64 now,
59                                        clib_timebase_component_t * cp);
60
61 f64 clib_timebase_components_to_time (clib_timebase_component_t * cp);
62
63 f64 clib_timebase_find_sunday_midnight (f64 start_time);
64 f64 clib_timebase_offset_from_sunday (u8 * day);
65 f64 clib_timebase_summer_offset (clib_timebase_t * tb, f64 now);
66
67 unformat_function_t unformat_clib_timebase_range_hms;
68 unformat_function_t unformat_clib_timebase_range_vector;
69
70 format_function_t format_clib_timebase_time;
71
72 static inline f64 clib_timebase_summer_offset_fastpath
73   (clib_timebase_t * tb, f64 now)
74 {
75   if (PREDICT_TRUE
76       (now >= tb->cached_year_start && now <= tb->cached_year_end))
77     {
78       if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
79         return tb->summer_offset;
80       else
81         return 0.0;
82     }
83   else
84     return clib_timebase_summer_offset (tb, now);
85 }
86
87 static inline f64
88 clib_timebase_now (clib_timebase_t * tb)
89 {
90   f64 now;
91
92   now = tb->clib_time->init_reference_time + clib_time_now (tb->clib_time);
93   now += tb->timezone_offset;
94   now += clib_timebase_summer_offset_fastpath (tb, now);
95
96   return now;
97 }
98
99 static inline int
100 clib_timebase_is_leap_year (u32 year)
101 {
102   int rv;
103
104   if (PREDICT_TRUE ((year % 4) != 0))
105     return 0;
106
107   rv = 0;
108
109   if ((year % 4) == 0)
110     rv = 1;
111   if ((year % 100) == 0)
112     rv = 0;
113   if ((year % 400) == 0)
114     rv = 1;
115   return rv;
116 }
117
118 #endif /* included_time_range_h */
119
120
121 /*
122  * fd.io coding-style-patch-verification: ON
123  *
124  * Local Variables:
125  * eval: (c-set-style "gnu")
126  * End:
127  */