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:
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.
16 #include <vppinfra/time_range.h>
19 test_time_range_main (unformat_input_t * input)
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;
29 f64 start_time, end_time;
33 clib_timebase_init (tb, -5 /* EST */ , CLIB_TIMEBASE_DAYLIGHT_USA,
34 0 /* allocate a clib_time_t */ );
36 /* Set up summer time cache */
37 now = clib_timebase_now (tb);
40 now = clib_timebase_now (tb);
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);
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);
53 * test a few other dates, to verify summer time operation
54 * 2011: started sunday 3/13, ended sunday 11/6
57 fformat (stdout, "Test daylight time rules:\n");
59 clib_memset (cp, 0, sizeof (*cp));
61 /* Just before DST starts */
68 then = clib_timebase_components_to_time (cp);
70 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
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");
80 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
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");
86 /* Just before DST ends */
93 then = clib_timebase_components_to_time (cp);
95 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
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");
101 /* add two seconds. */
105 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
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");
111 /* Back to the future... */
112 clib_timebase_time_to_components (now, cp);
114 fformat (stdout, "Test time range calculations:\n");
116 /* Find previous Sunday midnight */
117 sunday_midnight = now = clib_timebase_find_sunday_midnight (now);
119 clib_timebase_time_to_components (now, cp);
121 fformat (stdout, "Sunday midnight: %U\n", format_clib_timebase_time, now);
123 test_range_string = "Mon 11 - 17 Tue 7 - 11 Wed - Fri 8 - 18";
125 unformat_init_string (input2, test_range_string,
126 strlen (test_range_string));
128 if (unformat (input2, "%U", unformat_clib_timebase_range_vector, &rp))
130 vec_foreach (this_rp, rp)
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);
142 fformat (stdout, "Time convert fail!\n");
145 unformat_free (input2);
146 clib_mem_free (tb->clib_time);
152 * GDB callable function: vl - Return vector length of vector
162 main (int argc, char *argv[])
167 clib_mem_init (0, 64ULL << 20);
169 unformat_init_command_line (&i, argv);
170 ret = test_time_range_main (&i);
175 #endif /* CLIB_UNIX */
177 * fd.io coding-style-patch-verification: ON
180 * eval: (c-set-style "gnu")