vppinfra: refactor clib_timebase_t
[vpp.git] / src / vppinfra / test_time_range.c
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 static int
19 test_time_range_main (unformat_input_t * input)
20 {
21   clib_timebase_t _tb, *tb = &_tb;
22   clib_timebase_component_t _c, *cp = &_c;
23   clib_timebase_range_t *rp = 0;
24   clib_timebase_range_t *this_rp;
25   unformat_input_t _input2, *input2 = &_input2;
26   char *test_range_string;
27   f64 sunday_midnight;
28   f64 now, then;
29   f64 start_time, end_time;
30   f64 timezone_offset;
31
32   /* Init time base */
33   clib_timebase_init (tb, -5 /* EST */ , CLIB_TIMEBASE_DAYLIGHT_USA,
34                       0 /* allocate a clib_time_t */ );
35
36   /* Set up summer time cache */
37   now = clib_timebase_now (tb);
38
39   /* Test it */
40   now = clib_timebase_now (tb);
41
42   /* show current time */
43   fformat (stdout, "Current time in UTC%f, US daylight time rules:\n",
44            tb->timezone_offset / 3600.0);
45   fformat (stdout, "%U", format_clib_timebase_time, now);
46
47   /* Test conversion to component structure */
48   clib_timebase_time_to_components (now, cp);
49   now = clib_timebase_components_to_time (cp);
50   fformat (stdout, " -> %U\n", format_clib_timebase_time, now);
51
52   /*
53    * test a few other dates, to verify summer time operation
54    * 2011: started sunday 3/13, ended sunday 11/6
55    */
56
57   fformat (stdout, "Test daylight time rules:\n");
58
59   clib_memset (cp, 0, sizeof (*cp));
60
61   /* Just before DST starts */
62   cp->year = 2011;
63   cp->month = 2;
64   cp->day = 13;
65   cp->hour = 1;
66   cp->minute = 59;
67   cp->second = 59;
68   then = clib_timebase_components_to_time (cp);
69
70   timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
71
72   fformat (stdout, "%U should not be in DST, and it %s\n",
73            format_clib_timebase_time, then,
74            (timezone_offset != 0.0) ? "is" : "is not");
75
76   /* add two seconds */
77
78   then += 2.0;
79
80   timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
81
82   fformat (stdout, "%U should be in DST, and it %s\n",
83            format_clib_timebase_time, then,
84            (timezone_offset != 0.0) ? "is" : "is not");
85
86   /* Just before DST ends */
87   cp->year = 2011;
88   cp->month = 10;
89   cp->day = 6;
90   cp->hour = 1;
91   cp->minute = 59;
92   cp->second = 59;
93   then = clib_timebase_components_to_time (cp);
94
95   timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
96
97   fformat (stdout, "%U should be in DST, and it %s\n",
98            format_clib_timebase_time, then,
99            (timezone_offset != 0.0) ? "is" : "is not");
100
101   /* add two seconds. */
102
103   then += 2.0;
104
105   timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
106
107   fformat (stdout, "%U should not be in DST, and it %s\n",
108            format_clib_timebase_time, then,
109            (timezone_offset != 0.0) ? "is" : "is not");
110
111   /* Back to the future... */
112   clib_timebase_time_to_components (now, cp);
113
114   fformat (stdout, "Test time range calculations:\n");
115
116   /* Find previous Sunday midnight */
117   sunday_midnight = now = clib_timebase_find_sunday_midnight (now);
118
119   clib_timebase_time_to_components (now, cp);
120
121   fformat (stdout, "Sunday midnight: %U\n", format_clib_timebase_time, now);
122
123   test_range_string = "Mon 11 - 17 Tue 7 - 11 Wed - Fri 8 - 18";
124
125   unformat_init_string (input2, test_range_string,
126                         strlen (test_range_string));
127
128   if (unformat (input2, "%U", unformat_clib_timebase_range_vector, &rp))
129     {
130       vec_foreach (this_rp, rp)
131       {
132         start_time = sunday_midnight + this_rp->start;
133         end_time = sunday_midnight + this_rp->end;
134         fformat (stdout, "range: %U - %U\n",
135                  format_clib_timebase_time, start_time,
136                  format_clib_timebase_time, end_time);
137       }
138       vec_free (rp);
139     }
140   else
141     {
142       fformat (stdout, "Time convert fail!\n");
143     }
144
145   unformat_free (input2);
146   clib_mem_free (tb->clib_time);
147
148   return 0;
149 }
150
151 /*
152  * GDB callable function: vl - Return vector length of vector
153  */
154 u32
155 vl (void *p)
156 {
157   return vec_len (p);
158 }
159
160 #ifdef CLIB_UNIX
161 int
162 main (int argc, char *argv[])
163 {
164   unformat_input_t i;
165   int ret;
166
167   clib_mem_init (0, 64ULL << 20);
168
169   unformat_init_command_line (&i, argv);
170   ret = test_time_range_main (&i);
171   unformat_free (&i);
172
173   return ret;
174 }
175 #endif /* CLIB_UNIX */
176 /*
177  * fd.io coding-style-patch-verification: ON
178  *
179  * Local Variables:
180  * eval: (c-set-style "gnu")
181  * End:
182  */