summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/time_range.c
blob: e502ca358dbd4e67f978a493d55e74bc06ebaacf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #
/*
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vppinfra/time_range.h>

void
clib_timebase_init (clib_timebase_t * tb, i32 timezone_offset_in_hours,
		    clib_timebase_daylight_time_t daylight_type)
{
  memset (tb, 0, sizeof (*tb));

  clib_time_init (&tb->clib_time);
  tb->time_zero = unix_time_now ();

  tb->timezone_offset = ((f64) (timezone_offset_in_hours)) * 3600.0;
  tb->daylight_time_type = daylight_type;
  switch (tb->daylight_time_type)
    {
    case CLIB_TIMEBASE_DAYLIGHT_NONE:
      tb->summer_offset = 0.0;
      break;
    case CLIB_TIMEBASE_DAYLIGHT_USA:
      tb->summer_offset = 3600.0;
      break;
    default:
      clib_warning ("unknown daylight type %d", tb->daylight_time_type);
      tb->daylight_time_type = CLIB_TIMEBASE_DAYLIGHT_NONE;
      tb->summer_offset = 0.0;
    }
}

const static u32 days_per_month[] = {
  31,				/* Jan */
  28,				/* Feb */
  31,				/* Mar */
  30,				/* Apr */
  31,				/* May */
  30,				/* Jun */
  31,				/* Jul */
  31,				/* Aug */
  30,				/* Sep */
  31,				/* Oct */
  30,				/* Nov */
  31,				/* Dec */
};

const static char *month_short_names[] = {
  "Jan",
  "Feb",
  "Mar",
  "Apr",
  "May",
  "Jun",
  "Jul",
  "Aug",
  "Sep",
  "Oct",
  "Nov",
  "Dec",
};

const static char *day_names_epoch_order[] = {
  "Thu",
  "Fri",
  "Sat",
  "Sun",
  "Mon",
  "Tue",
  "Wed",
};

const static char *day_names_calendar_order[] = {
  "Sun",
  "Mon",
  "Tue",
  "Wed",
  "Thu",
  "Fri",
  "Sat",
};


void
clib_timebase_time_to_components (f64 now, clib_timebase_component_t * cp)
{
  u32 year, month, hours, minutes, seconds, nanoseconds;
  u32 days_in_year, days_in_month, day_of_month;
  u32 days_since_epoch;
  u32 day_name_index;

  /* Unix epoch is 1/1/1970 00:00:00.00, a Thursday */

  year = 1970;
  days_since_epoch = 0;

  do
    {
      days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
      days_since_epoch += days_in_year;
      now = now - ((f64) days_in_year) * 86400.0;
      year++;
    }
  while (now > 0.0);

  days_since_epoch -= days_in_year;
  now += ((f64) days_in_year) * 86400;
  year--;

  month = 0;

  do
    {
      days_in_month = days_per_month[month];
      if (month == 1 && clib_timebase_is_leap_year (year))
	days_in_month++;

      days_since_epoch += days_in_month;
      now = now - ((f64) days_in_month) * 86400.0;
      month++;
    }
  while (now > 0.0);

  days_since_epoch -= days_in_month;
  now += ((f64) days_in_month) * 86400;
  month--;

  day_of_month = 1;
  do
    {
      now = now - 86400;
      day_of_month++;
      days_since_epoch++;
    }
  while (now > 0.0);

  day_of_month--;
  days_since_epoch--;
  now += 86400.0;

  day_name_index = days_since_epoch % 7;

  hours = (u32) (now / (3600.0));
  now -= (f64) (hours * 3600);

  minutes = (u32) (now / 60.0);
  now -= (f64) (minutes * 60);

  seconds = (u32) (now);
  now -= (f64) (seconds);

  nanoseconds = (f64) (now * 1e9);

  cp->year = year;
  cp->month = month;
  cp->day = day_of_month;
  cp->day_name_index = day_name_index;
  cp->hour = hours;
  cp->minute = minutes;
  cp->second = seconds;
  cp->nanosecond = nanoseconds;
  cp->fractional_seconds = now;
}

f64
clib_timebase_components_to_time (clib_timebase_component_t * cp)
{
  f64 now = 0;
  u32 year, days_in_year, month, days_in_month;

  year = 1970;

  while (year < cp->year)
    {
      days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
      now += ((f64) days_in_year) * 86400.0;
      year++;
    }

  month = 0;

  while (month < cp->month)
    {
      days_in_month = days_per_month[month];
      if (month == 1 && clib_timebase_is_leap_year (year))
	days_in_month++;

      now += ((f64) days_in_month) * 86400.0;
      month++;
    }

  now += ((f64) cp->day - 1) * 86400.0;
  now += ((f64) cp->hour) * 3600.0;
  now += ((f64) cp->minute) * 60.0;
  now += ((f64) cp->second);
  now += ((f64) cp->nanosecond) * 1e-9;

  return (now);
}

f64
clib_timebase_find_sunday_midnight (f64 start_time)
{
  clib_timebase_component_t _c, *cp = &_c;

  clib_timebase_time_to_components (start_time, cp);

  /* back up to midnight */
  cp->hour = cp->minute = cp->second = 0;

  start_time = clib_timebase_components_to_time (cp);

  while (cp->day_name_index != 3 /* sunday */ )
    {
      /* Back up one day */
      start_time -= 86400.0;
      clib_timebase_time_to_components (start_time, cp);
    }
  /* Clean up residual fraction */
  start_time -= cp->fractional_seconds;
  start_time += 1e-6;		/* 1us inside Sunday  */

  return (start_time);
}

f64
clib_timebase_offset_from_sunday (u8 * day)
{
  int i;

  for (i = 0; i < ARRAY_LEN (day_names_calendar_order); i++)
    {
      if (!strncmp ((char *) day, day_names_calendar_order[i], 3))
	return ((f64) i) * 86400.0;
    }
  return 0.0;
}


u8 *
format_clib_timebase_time (u8 * s, va_list * args)
{
  f64 now = va_arg (*args, f64);
  clib_timebase_component_t _c, *cp = &_c;

  clib_timebase_time_to_components (now, cp);

  s = format (s, "%s, %u %s %u %u:%02u:%02u",
	      day_names_epoch_order[cp->day_name_index],
	      cp->day,
	      month_short_names[cp->month],
	      cp->year, cp->hour, cp->minute, cp->second);
  return (s);
}

uword
unformat_clib_timebase_range_hms (unformat_input_t * input, va_list * args)
{
  clib_timebase_range_t *rp = va_arg (*args, clib_timebase_range_t *);
  clib_timebase_component_t _c, *cp = &_c;
  u32 start_hour, start_minute, start_second;
  u32 end_hour, end_minute, end_second;

  start_hour = start_minute = start_second
    = end_hour = end_minute = end_second = 0;

  if (unformat (input, "%u:%u:%u - %u:%u:%u",
		&start_hour, &start_minute, &start_second,
		&end_hour, &end_minute, &end_second))
    ;
  else if (unformat (input, "%u:%u - %u:%u",
		     &start_hour, &start_minute, &end_hour, &end_minute))
    ;
  else if (unformat (input, "%u - %u", &start_hour, &end_hour))
    ;
  else
    return 0;

  clib_timebase_time_to_components (1e-6, cp);

  cp->hour = start_hour;
  cp->minute = start_minute;
  cp->second = start_second;

  rp->start = clib_timebase_components_to_time (cp);

  cp->hour = end_hour;
  cp->minute = end_minute;
  cp->second = end_second;

  rp->end = clib_timebase_components_to_time (cp);

  return 1;
}

uword
unformat_clib_timebase_range_vector (unformat_input_t * input, va_list * args)
{
  clib_timebase_range_t **rpp = va_arg (*args, clib_timebase_range_t **);
  clib_timebase_range_t _tmp, *tmp = &_tmp;
  clib_timebase_range_t *rp, *new_rp;
  int day_range_match = 0;
  int time_range_match = 0;
  f64 range_start_time_offset;
  f64 range_end_time_offset;
  f64 now;
  u8 *start_day = 0, *end_day = 0;

  rp = *rpp;

  while (1)
    {
      if (!day_range_match
	  && unformat (input, "%s - %s", &start_day, &end_day))
	{
	  range_start_time_offset
	    = clib_timebase_offset_from_sunday (start_day);
	  range_end_time_offset = clib_timebase_offset_from_sunday (end_day);
	  vec_free (start_day);
	  vec_free (end_day);
	  day_range_match = 1;
	  time_range_match = 0;
	}
      else if (!day_range_match && unformat (input, "%s", &start_day))
	{
	  range_start_time_offset
	    = clib_timebase_offset_from_sunday (start_day);
	  range_end_time_offset = range_start_time_offset + 86399.0;
	  day_range_match = 1;
	  vec_free (start_day);
	  day_range_match = 1;
	  time_range_match = 0;
	}
      else if (day_range_match &&
	       unformat (input, "%U", unformat_clib_timebase_range_hms, tmp))
	{
	  /* Across the week... */
	  for (now = range_start_time_offset; now <= range_end_time_offset;
	       now += 86400.0)
	    {
	      vec_add2 (rp, new_rp, 1);
	      new_rp->start = now + tmp->start;
	      new_rp->end = now + tmp->end;
	    }
	  day_range_match = 0;
	  time_range_match = 1;
	}
      else if (time_range_match)
	break;
      else
	{
	  vec_free (rp);
	  *rpp = 0;
	  return 0;
	}
    }

  if (time_range_match)
    {
      *rpp = rp;
      return 1;
    }
  else
    {
      vec_free (rp);
      *rpp = 0;
      return 0;
    }
}

f64
clib_timebase_summer_offset (clib_timebase_t * tb, f64 now)
{
  clib_timebase_component_t _c, *cp = &_c;
  f64 second_sunday_march_2am;
  f64 first_sunday_november_2am;

  if (PREDICT_TRUE
      (now >= tb->cached_year_start && now <= tb->cached_year_end))
    {
      if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
	return tb->summer_offset;
      else
	return (0.0);
    }

  clib_timebase_time_to_components (now, cp);

  cp->month = 0;
  cp->day = 1;
  cp->hour = 0;
  cp->minute = 0;
  cp->second = 1;

  tb->cached_year_start = clib_timebase_components_to_time (cp);

  cp->year += 1;

  tb->cached_year_end = clib_timebase_components_to_time (cp);

  cp->year -= 1;

  /* Search for the second sunday in march, 2am */
  cp->month = 2;
  cp->day = 1;
  cp->hour = 2;
  cp->second = 0;
  cp->nanosecond = 1;

  /* March 1st will never be the second sunday... */
  second_sunday_march_2am = clib_timebase_components_to_time (cp);
  cp->day_name_index = 0;

  /* Find the first sunday */
  do
    {
      clib_timebase_time_to_components (second_sunday_march_2am, cp);
      second_sunday_march_2am += 86400.0;
    }
  while (cp->day_name_index != 3 /* sunday */ );

  /* Find the second sunday */
  do
    {
      clib_timebase_time_to_components (second_sunday_march_2am, cp);
      second_sunday_march_2am += 86400.0;
    }
  while (cp->day_name_index != 3 /* sunday */ );

  second_sunday_march_2am -= 86400.0;

  tb->cached_summer_start = second_sunday_march_2am;

  /* Find the first sunday in November, which can easily be 11/1 */
  cp->month = 10;
  cp->day = 1;

  first_sunday_november_2am = clib_timebase_components_to_time (cp);
  clib_timebase_time_to_components (first_sunday_november_2am, cp);

  while (cp->day_name_index != 3 /* sunday */ )
    {
      first_sunday_november_2am += 86400.0;
      clib_timebase_time_to_components (first_sunday_november_2am, cp);
    }

  tb->cached_summer_end = first_sunday_november_2am;

  if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
    return tb->summer_offset;
  else
    return (0.0);
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */