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