Dave Barach | a98c403 | 2018-06-06 10:52:08 -0400 | [diff] [blame] | 1 | /* |
| 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: |
| 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 | |
| 16 | #include <vppinfra/time_range.h> |
| 17 | |
| 18 | static int |
| 19 | test_time_range_main (unformat_input_t * input) |
| 20 | { |
| 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; |
| 27 | f64 sunday_midnight; |
| 28 | f64 now, then; |
| 29 | f64 start_time, end_time; |
| 30 | f64 timezone_offset; |
| 31 | |
| 32 | /* Init time base */ |
| 33 | clib_timebase_init (tb, -5 /* EST */ , CLIB_TIMEBASE_DAYLIGHT_USA); |
| 34 | |
| 35 | /* Set up summer time cache */ |
| 36 | now = clib_timebase_now (tb); |
| 37 | |
| 38 | /* Test it */ |
| 39 | now = clib_timebase_now (tb); |
| 40 | |
| 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); |
| 45 | |
| 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); |
| 50 | |
| 51 | /* |
| 52 | * test a few other dates, to verify summer time operation |
| 53 | * 2011: started sunday 3/13, ended sunday 11/6 |
| 54 | */ |
| 55 | |
| 56 | fformat (stdout, "Test daylight time rules:\n"); |
| 57 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 58 | clib_memset (cp, 0, sizeof (*cp)); |
Dave Barach | a98c403 | 2018-06-06 10:52:08 -0400 | [diff] [blame] | 59 | |
| 60 | /* Just before DST starts */ |
| 61 | cp->year = 2011; |
| 62 | cp->month = 2; |
| 63 | cp->day = 13; |
| 64 | cp->hour = 1; |
| 65 | cp->minute = 59; |
| 66 | cp->second = 59; |
| 67 | then = clib_timebase_components_to_time (cp); |
| 68 | |
| 69 | timezone_offset = clib_timebase_summer_offset_fastpath (tb, then); |
| 70 | |
| 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"); |
| 74 | |
| 75 | /* add two seconds */ |
| 76 | |
| 77 | then += 2.0; |
| 78 | |
| 79 | timezone_offset = clib_timebase_summer_offset_fastpath (tb, then); |
| 80 | |
| 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"); |
| 84 | |
| 85 | /* Just before DST ends */ |
| 86 | cp->year = 2011; |
| 87 | cp->month = 10; |
| 88 | cp->day = 6; |
| 89 | cp->hour = 1; |
| 90 | cp->minute = 59; |
| 91 | cp->second = 59; |
| 92 | then = clib_timebase_components_to_time (cp); |
| 93 | |
| 94 | timezone_offset = clib_timebase_summer_offset_fastpath (tb, then); |
| 95 | |
| 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"); |
| 99 | |
| 100 | /* add two seconds. */ |
| 101 | |
| 102 | then += 2.0; |
| 103 | |
| 104 | timezone_offset = clib_timebase_summer_offset_fastpath (tb, then); |
| 105 | |
| 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"); |
| 109 | |
| 110 | /* Back to the future... */ |
| 111 | clib_timebase_time_to_components (now, cp); |
| 112 | |
| 113 | fformat (stdout, "Test time range calculations:\n"); |
| 114 | |
| 115 | /* Find previous Sunday midnight */ |
| 116 | sunday_midnight = now = clib_timebase_find_sunday_midnight (now); |
| 117 | |
| 118 | clib_timebase_time_to_components (now, cp); |
| 119 | |
| 120 | fformat (stdout, "Sunday midnight: %U\n", format_clib_timebase_time, now); |
| 121 | |
| 122 | test_range_string = "Mon 11 - 17 Tue 7 - 11 Wed - Fri 8 - 18"; |
| 123 | |
| 124 | unformat_init_string (input2, test_range_string, |
| 125 | strlen (test_range_string)); |
| 126 | |
| 127 | if (unformat (input2, "%U", unformat_clib_timebase_range_vector, &rp)) |
| 128 | { |
| 129 | vec_foreach (this_rp, rp) |
| 130 | { |
| 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); |
| 136 | } |
| 137 | vec_free (rp); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | fformat (stdout, "Time convert fail!\n"); |
| 142 | } |
| 143 | |
| 144 | unformat_free (input2); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * GDB callable function: vl - Return vector length of vector |
| 151 | */ |
| 152 | u32 |
| 153 | vl (void *p) |
| 154 | { |
| 155 | return vec_len (p); |
| 156 | } |
| 157 | |
| 158 | #ifdef CLIB_UNIX |
| 159 | int |
| 160 | main (int argc, char *argv[]) |
| 161 | { |
| 162 | unformat_input_t i; |
| 163 | int ret; |
| 164 | |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 165 | clib_mem_init (0, 64ULL << 20); |
| 166 | |
Dave Barach | a98c403 | 2018-06-06 10:52:08 -0400 | [diff] [blame] | 167 | unformat_init_command_line (&i, argv); |
| 168 | ret = test_time_range_main (&i); |
| 169 | unformat_free (&i); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | #endif /* CLIB_UNIX */ |
| 174 | /* |
| 175 | * fd.io coding-style-patch-verification: ON |
| 176 | * |
| 177 | * Local Variables: |
| 178 | * eval: (c-set-style "gnu") |
| 179 | * End: |
| 180 | */ |