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);
35 /* Set up summer time cache */
36 now = clib_timebase_now (tb);
39 now = clib_timebase_now (tb);
41 /* show current time */
42 fformat (stdout, "Current time in UTC%f, US daylight time rules:\n",
43 tb->timezone_offset / 3600.0);
44 fformat (stdout, "%U", format_clib_timebase_time, now);
46 /* Test conversion to component structure */
47 clib_timebase_time_to_components (now, cp);
48 now = clib_timebase_components_to_time (cp);
49 fformat (stdout, " -> %U\n", format_clib_timebase_time, now);
52 * test a few other dates, to verify summer time operation
53 * 2011: started sunday 3/13, ended sunday 11/6
56 fformat (stdout, "Test daylight time rules:\n");
58 clib_memset (cp, 0, sizeof (*cp));
60 /* Just before DST starts */
67 then = clib_timebase_components_to_time (cp);
69 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
71 fformat (stdout, "%U should not be in DST, and it %s\n",
72 format_clib_timebase_time, then,
73 (timezone_offset != 0.0) ? "is" : "is not");
79 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
81 fformat (stdout, "%U should be in DST, and it %s\n",
82 format_clib_timebase_time, then,
83 (timezone_offset != 0.0) ? "is" : "is not");
85 /* Just before DST ends */
92 then = clib_timebase_components_to_time (cp);
94 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
96 fformat (stdout, "%U should be in DST, and it %s\n",
97 format_clib_timebase_time, then,
98 (timezone_offset != 0.0) ? "is" : "is not");
100 /* add two seconds. */
104 timezone_offset = clib_timebase_summer_offset_fastpath (tb, then);
106 fformat (stdout, "%U should not be in DST, and it %s\n",
107 format_clib_timebase_time, then,
108 (timezone_offset != 0.0) ? "is" : "is not");
110 /* Back to the future... */
111 clib_timebase_time_to_components (now, cp);
113 fformat (stdout, "Test time range calculations:\n");
115 /* Find previous Sunday midnight */
116 sunday_midnight = now = clib_timebase_find_sunday_midnight (now);
118 clib_timebase_time_to_components (now, cp);
120 fformat (stdout, "Sunday midnight: %U\n", format_clib_timebase_time, now);
122 test_range_string = "Mon 11 - 17 Tue 7 - 11 Wed - Fri 8 - 18";
124 unformat_init_string (input2, test_range_string,
125 strlen (test_range_string));
127 if (unformat (input2, "%U", unformat_clib_timebase_range_vector, &rp))
129 vec_foreach (this_rp, rp)
131 start_time = sunday_midnight + this_rp->start;
132 end_time = sunday_midnight + this_rp->end;
133 fformat (stdout, "range: %U - %U\n",
134 format_clib_timebase_time, start_time,
135 format_clib_timebase_time, end_time);
141 fformat (stdout, "Time convert fail!\n");
144 unformat_free (input2);
150 * GDB callable function: vl - Return vector length of vector
160 main (int argc, char *argv[])
165 clib_mem_init (0, 64ULL << 20);
167 unformat_init_command_line (&i, argv);
168 ret = test_time_range_main (&i);
173 #endif /* CLIB_UNIX */
175 * fd.io coding-style-patch-verification: ON
178 * eval: (c-set-style "gnu")