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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
static void tm_add(struct tm *tm, int multiplier, zbx_time_unit_t base);
static void tm_sub(struct tm *tm, int multiplier, zbx_time_unit_t base);
static int time_unit_seconds[ZBX_TIME_UNIT_COUNT] = {0, 1, SEC_PER_MIN, SEC_PER_HOUR, SEC_PER_DAY, SEC_PER_WEEK, 0,
0};
zbx_time_unit_t zbx_tm_str_to_unit(const char *text)
{
switch (*text)
{
case 's':
return ZBX_TIME_UNIT_SECOND;
case 'm':
return ZBX_TIME_UNIT_MINUTE;
case 'h':
return ZBX_TIME_UNIT_HOUR;
case 'd':
return ZBX_TIME_UNIT_DAY;
case 'w':
return ZBX_TIME_UNIT_WEEK;
case 'M':
return ZBX_TIME_UNIT_MONTH;
case 'y':
return ZBX_TIME_UNIT_YEAR;
default:
return ZBX_TIME_UNIT_UNKNOWN;
}
}
/******************************************************************************
* *
* Function: zbx_tm_parse_period *
* *
* Purpose: parse time period in format <multiplier><time unit> *
* *
* Parameters: period - [IN] the time period *
* len - [OUT] the length of parsed time period *
* multiplier - [OUT] the parsed multiplier *
* base - [OUT] the parsed time unit *
* error - [OUT] the error message if parsing failed *
* *
* Return value: SUCCEED - period was parsed successfully *
* FAIL - invalid time period was specified *
* *
******************************************************************************/
int zbx_tm_parse_period(const char *period, size_t *len, int *multiplier, zbx_time_unit_t *base, char **error)
{
const char *ptr;
for (ptr = period; 0 != isdigit(*ptr); ptr++)
;
if (FAIL == is_uint_n_range(period, ptr - period, multiplier, sizeof(*multiplier), 1, UINT32_MAX))
{
*error = zbx_strdup(*error, "invalid period multiplier");
return FAIL;
}
if (ZBX_TIME_UNIT_UNKNOWN == (*base = zbx_tm_str_to_unit(ptr)))
{
*error = zbx_strdup(*error, "invalid period time unit");
return FAIL;
}
*len = ptr - period + 1;
return SUCCEED;
}
/******************************************************************************
* *
* Function: tm_add_seconds *
* *
* Purpose: add seconds to the time and adjust result by dst *
* *
* Parameter: tm - [IN/OUT] the time structure *
* seconds - [IN] the seconds to add (can be negative) *
* *
******************************************************************************/
static void tm_add_seconds(struct tm *tm, int seconds)
{
time_t time_new;
struct tm tm_new = *tm;
if (-1 == (time_new = mktime(&tm_new)))
{
THIS_SHOULD_NEVER_HAPPEN;
return;
}
time_new += seconds;
localtime_r(&time_new, &tm_new);
if (tm->tm_isdst != tm_new.tm_isdst && -1 != tm->tm_isdst && -1 != tm_new.tm_isdst)
{
if (0 == tm_new.tm_isdst)
tm_add(&tm_new, 1, ZBX_TIME_UNIT_HOUR);
else
tm_sub(&tm_new, 1, ZBX_TIME_UNIT_HOUR);
}
*tm = tm_new;
}
/******************************************************************************
* *
* Function: tm_add *
* *
* Purpose: add time duration without adjusting DST clocks *
* *
* Parameter: tm - [IN/OUT] the time structure *
* multiplier - [IN] the unit multiplier *
* base - [IN] the time unit to add *
* *
******************************************************************************/
static void tm_add(struct tm *tm, int multiplier, zbx_time_unit_t base)
{
int shift;
switch (base)
{
case ZBX_TIME_UNIT_HOUR:
tm->tm_hour += multiplier;
if (24 <= tm->tm_hour)
{
shift = tm->tm_hour / 24;
tm->tm_hour %= 24;
tm_add(tm, shift, ZBX_TIME_UNIT_DAY);
}
break;
case ZBX_TIME_UNIT_DAY:
tm->tm_mday += multiplier;
while (tm->tm_mday > (shift = zbx_day_in_month(tm->tm_year + 1900, tm->tm_mon + 1)))
{
tm->tm_mday -= shift;
tm_add(tm, 1, ZBX_TIME_UNIT_MONTH);
}
tm->tm_wday += multiplier;
tm->tm_wday %= 7;
break;
case ZBX_TIME_UNIT_WEEK:
tm_add(tm, multiplier * 7, ZBX_TIME_UNIT_DAY);
break;
case ZBX_TIME_UNIT_MONTH:
tm->tm_mon += multiplier;
if (12 <= tm->tm_mon)
{
shift = tm->tm_mon / 12;
tm->tm_mon %= 12;
tm_add(tm, shift, ZBX_TIME_UNIT_YEAR);
}
break;
case ZBX_TIME_UNIT_YEAR:
tm->tm_year += multiplier;
break;
default:
break;
}
}
/******************************************************************************
* *
* Function: zbx_tm_add *
* *
* Purpose: add time duration *
* *
* Parameter: tm - [IN/OUT] the time structure *
* multiplier - [IN] the unit multiplier *
* base - [IN] the time unit to add *
* *
******************************************************************************/
void zbx_tm_add(struct tm *tm, int multiplier, zbx_time_unit_t base)
{
if (ZBX_TIME_UNIT_MONTH == base || ZBX_TIME_UNIT_YEAR == base)
tm_add(tm, multiplier, base);
tm_add_seconds(tm, multiplier * time_unit_seconds[base]);
return;
}
/******************************************************************************
* *
* Function: neg_to_pos_wrap *
* *
* Purpose: convert negative number to postive by wrapping around the base *
* *
* Parameter: value - [IN/OUT] the value to convert *
* base - [IN] the wrap base *
* *
******************************************************************************/
static void neg_to_pos_wrap(int *value, int base)
{
int reminder = *value % base;
*value = (0 == reminder ? 0 : base + reminder);
}
/******************************************************************************
* *
* Function: tm_sub *
* *
* Purpose: subtracts time duration without adjusting DST clocks *
* *
* Parameter: tm - [IN/OUT] the time structure *
* multiplier - [IN] the unit multiplier *
* base - [IN] the time unit to add *
* *
******************************************************************************/
static void tm_sub(struct tm *tm, int multiplier, zbx_time_unit_t base)
{
int shift;
switch (base)
{
case ZBX_TIME_UNIT_HOUR:
tm->tm_hour -= multiplier;
if (0 > tm->tm_hour)
{
shift = -tm->tm_hour / 24;
neg_to_pos_wrap(&tm->tm_hour, 24);
if (0 != tm->tm_hour)
shift++;
tm_sub(tm, shift, ZBX_TIME_UNIT_DAY);
}
return;
case ZBX_TIME_UNIT_DAY:
tm->tm_mday -= multiplier;
while (0 >= tm->tm_mday)
{
int prev_mon;
if (0 > (prev_mon = tm->tm_mon - 1))
prev_mon = 11;
prev_mon++;
tm->tm_mday += zbx_day_in_month(tm->tm_year + 1900, prev_mon);
tm_sub(tm, 1, ZBX_TIME_UNIT_MONTH);
}
tm->tm_wday -= multiplier;
if (0 > tm->tm_wday)
neg_to_pos_wrap(&tm->tm_wday, 7);
return;
case ZBX_TIME_UNIT_WEEK:
tm_sub(tm, multiplier * 7, ZBX_TIME_UNIT_DAY);
return;
case ZBX_TIME_UNIT_MONTH:
tm->tm_mon -= multiplier;
if (0 > tm->tm_mon)
{
shift = -tm->tm_mon / 12;
neg_to_pos_wrap(&tm->tm_mon, 12);
if (0 != tm->tm_mon)
shift++;
tm_sub(tm, shift, ZBX_TIME_UNIT_YEAR);
}
return;
case ZBX_TIME_UNIT_YEAR:
tm->tm_year -= multiplier;
return;
default:
return;
}
}
/******************************************************************************
* *
* Function: zbx_tm_sub *
* *
* Purpose: subtracts time duration *
* *
* Parameter: tm - [IN/OUT] the time structure *
* multiplier - [IN] the unit multiplier *
* base - [IN] the time unit to add *
* *
******************************************************************************/
void zbx_tm_sub(struct tm *tm, int multiplier, zbx_time_unit_t base)
{
if (ZBX_TIME_UNIT_MONTH == base || ZBX_TIME_UNIT_YEAR == base)
tm_sub(tm, multiplier, base);
tm_add_seconds(tm, -multiplier * time_unit_seconds[base]);
return;
}
/******************************************************************************
* *
* Function: zbx_tm_round_up *
* *
* Purpose: rounds time by the specified unit upwards *
* *
* Parameter: tm - [IN/OUT] the time structure *
* base - [IN] the time unit *
* *
******************************************************************************/
void zbx_tm_round_up(struct tm *tm, zbx_time_unit_t base)
{
if (0 != tm->tm_sec)
{
tm->tm_sec = 0;
zbx_tm_add(tm, 1, ZBX_TIME_UNIT_MINUTE);
}
if (ZBX_TIME_UNIT_MINUTE == base)
return;
if (0 != tm->tm_min)
{
tm->tm_min = 0;
zbx_tm_add(tm, 1, ZBX_TIME_UNIT_HOUR);
}
if (ZBX_TIME_UNIT_HOUR == base)
return;
if (0 != tm->tm_hour)
{
tm->tm_hour = 0;
zbx_tm_add(tm, 1, ZBX_TIME_UNIT_DAY);
}
if (ZBX_TIME_UNIT_DAY == base)
return;
if (ZBX_TIME_UNIT_WEEK == base)
{
if (1 != tm->tm_wday)
{
zbx_tm_add(tm, (0 == tm->tm_wday ? 1 : 8 - tm->tm_wday), ZBX_TIME_UNIT_DAY);
tm->tm_wday = 1;
}
return;
}
if (1 != tm->tm_mday)
{
tm->tm_mday = 1;
zbx_tm_add(tm, 1, ZBX_TIME_UNIT_MONTH);
}
if (ZBX_TIME_UNIT_MONTH == base)
return;
if (0 != tm->tm_mon)
{
tm->tm_mon = 0;
zbx_tm_add(tm, 1, ZBX_TIME_UNIT_YEAR);
}
return;
}
/******************************************************************************
* *
* Function: zbx_tm_round_down *
* *
* Purpose: rounds time by the specified unit downwards *
* *
* Parameter: tm - [IN/OUT] the time structure *
* base - [IN] the time unit *
* *
******************************************************************************/
void zbx_tm_round_down(struct tm *tm, zbx_time_unit_t base)
{
switch (base)
{
case ZBX_TIME_UNIT_WEEK:
if (1 != tm->tm_wday)
{
zbx_tm_sub(tm, (0 == tm->tm_wday ? 6 : tm->tm_wday - 1), ZBX_TIME_UNIT_DAY);
tm->tm_wday = 1;
}
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
break;
case ZBX_TIME_UNIT_YEAR:
tm->tm_mon = 0;
ZBX_FALLTHROUGH;
case ZBX_TIME_UNIT_MONTH:
tm->tm_mday = 1;
ZBX_FALLTHROUGH;
case ZBX_TIME_UNIT_DAY:
tm->tm_hour = 0;
ZBX_FALLTHROUGH;
case ZBX_TIME_UNIT_HOUR:
tm->tm_min = 0;
ZBX_FALLTHROUGH;
case ZBX_TIME_UNIT_MINUTE:
tm->tm_sec = 0;
break;
default:
break;
}
tm_add_seconds(tm, 0);
return;
}
const char *zbx_timespec_str(const zbx_timespec_t *ts)
{
static ZBX_THREAD_LOCAL char str[32];
time_t ts_time = ts->sec;
struct tm tm;
localtime_r(&ts_time, &tm);
zbx_snprintf(str, sizeof(str), "%04d.%02d.%02d %02d:%02d:%02d %09d", tm.tm_year + 1900, tm.tm_mon + 1,
tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, ts->ns);
return str;
}
|