Line data Source code
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 1 : test_time_range_main (unformat_input_t * input)
20 : {
21 1 : vlib_main_t *vm = vlib_get_main ();
22 1 : clib_timebase_t _tb, *tb = &_tb;
23 1 : clib_timebase_component_t _c, *cp = &_c;
24 1 : clib_timebase_range_t *rp = 0;
25 : clib_timebase_range_t *this_rp;
26 1 : 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 1 : clib_timebase_init (tb, -5 /* EST */ , CLIB_TIMEBASE_DAYLIGHT_USA,
35 : &vm->clib_time);
36 :
37 : /* Set up summer time cache */
38 1 : now = clib_timebase_now (tb);
39 :
40 : /* Test it */
41 1 : now = clib_timebase_now (tb);
42 :
43 : /* show current time */
44 1 : fformat (stdout, "Current time in UTC%f, US daylight time rules:\n",
45 1 : tb->timezone_offset / 3600.0);
46 1 : fformat (stdout, "%U", format_clib_timebase_time, now);
47 :
48 : /* Test conversion to component structure */
49 1 : clib_timebase_time_to_components (now, cp);
50 1 : now = clib_timebase_components_to_time (cp);
51 1 : 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 1 : fformat (stdout, "Test daylight time rules:\n");
59 :
60 1 : clib_memset (cp, 0, sizeof (*cp));
61 :
62 : /* Just before DST starts */
63 1 : cp->year = 2011;
64 1 : cp->month = 2;
65 1 : cp->day = 13;
66 1 : cp->hour = 1;
67 1 : cp->minute = 59;
68 1 : cp->second = 59;
69 1 : then = clib_timebase_components_to_time (cp);
70 :
71 1 : timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
72 :
73 1 : 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 1 : then += 2.0;
80 :
81 1 : timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
82 :
83 1 : 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 1 : cp->year = 2011;
89 1 : cp->month = 10;
90 1 : cp->day = 6;
91 1 : cp->hour = 1;
92 1 : cp->minute = 59;
93 1 : cp->second = 59;
94 1 : then = clib_timebase_components_to_time (cp);
95 :
96 1 : timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
97 :
98 1 : 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 1 : then += 2.0;
105 :
106 1 : timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
107 :
108 1 : 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 1 : clib_timebase_time_to_components (now, cp);
114 :
115 1 : fformat (stdout, "Test time range calculations:\n");
116 :
117 : /* Find previous Sunday midnight */
118 1 : sunday_midnight = now = clib_timebase_find_sunday_midnight (now);
119 :
120 1 : clib_timebase_time_to_components (now, cp);
121 :
122 1 : fformat (stdout, "Sunday midnight: %U\n", format_clib_timebase_time, now);
123 :
124 1 : test_range_string = "Mon 11 - 17 Tue 7 - 11 Wed - Fri 8 - 18";
125 :
126 1 : unformat_init_string (input2, test_range_string,
127 1 : strlen (test_range_string));
128 :
129 1 : if (unformat (input2, "%U", unformat_clib_timebase_range_vector, &rp))
130 : {
131 6 : vec_foreach (this_rp, rp)
132 : {
133 5 : start_time = sunday_midnight + this_rp->start;
134 5 : end_time = sunday_midnight + this_rp->end;
135 5 : fformat (stdout, "range: %U - %U\n",
136 : format_clib_timebase_time, start_time,
137 : format_clib_timebase_time, end_time);
138 : }
139 1 : vec_free (rp);
140 : }
141 : else
142 : {
143 0 : fformat (stdout, "Time convert fail!\n");
144 0 : return -1;
145 : }
146 :
147 1 : unformat_free (input2);
148 :
149 1 : return 0;
150 : }
151 :
152 :
153 : static clib_error_t *
154 1 : 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 1 : rv = test_time_range_main (input);
161 :
162 1 : if (rv)
163 0 : return clib_error_return (0, "test time range FAILED, error %d", rv);
164 :
165 1 : return 0;
166 : }
167 :
168 : /* *INDENT-OFF* */
169 16239 : 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 : */
|