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:
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.
15 #include <vlib/vlib.h>
16 #include <vppinfra/time_range.h>
19 test_time_range_main (unformat_input_t * input)
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;
30 f64 start_time, end_time;
34 clib_timebase_init (tb, -5 /* EST */ , CLIB_TIMEBASE_DAYLIGHT_USA,
37 /* Set up summer time cache */
38 now = clib_timebase_now (tb);
41 now = clib_timebase_now (tb);
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);
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);
54 * test a few other dates, to verify summer time operation
55 * 2011: started sunday 3/13, ended sunday 11/6
58 fformat (stdout, "Test daylight time rules:\n");
60 clib_memset (cp, 0, sizeof (*cp));
62 /* Just before DST starts */
69 then = clib_timebase_components_to_time (cp);
71 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
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");
81 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
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");
87 /* Just before DST ends */
94 then = clib_timebase_components_to_time (cp);
96 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
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");
102 /* add two seconds. */
106 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
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");
112 /* Back to the future... */
113 clib_timebase_time_to_components (now, cp);
115 fformat (stdout, "Test time range calculations:\n");
117 /* Find previous Sunday midnight */
118 sunday_midnight = now = clib_timebase_find_sunday_midnight (now);
120 clib_timebase_time_to_components (now, cp);
122 fformat (stdout, "Sunday midnight: %U\n", format_clib_timebase_time, now);
124 test_range_string = "Mon 11 - 17 Tue 7 - 11 Wed - Fri 8 - 18";
126 unformat_init_string (input2, test_range_string,
127 strlen (test_range_string));
129 if (unformat (input2, "%U", unformat_clib_timebase_range_vector, &rp))
131 vec_foreach (this_rp, rp)
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);
143 fformat (stdout, "Time convert fail!\n");
147 unformat_free (input2);
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)
160 rv = test_time_range_main (input);
163 return clib_error_return (0, "test time range FAILED, error %d", rv);
168 VLIB_CLI_COMMAND (test_time_range_command, static) =
170 .path = "test time-range",
171 .short_help = "test time-range",
172 .function = test_time_range_command_fn,
176 * fd.io coding-style-patch-verification: ON
179 * eval: (c-set-style "gnu")