forked from jow-/nlbwmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.c
242 lines (195 loc) · 4.34 KB
/
timing.c
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
/*
ISC License
Copyright (c) 2016-2017, Jo-Philipp Wich <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <endian.h>
#include <errno.h>
#include "timing.h"
static int
is_leapyear(int year)
{
if (!(year % 400))
return 1;
if (!(year % 100))
return 0;
if (!(year % 4))
return 1;
return 0;
}
static int
days_in_month(int year, int month)
{
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
return 28 + is_leapyear(year);
case 4:
case 6:
case 9:
case 11:
return 30;
}
return -ERANGE;
}
static void
tm_inc_dec(struct tm *tm, bool inc)
{
if (!inc) {
if (tm->tm_mon > 0) {
tm->tm_mon--;
}
else {
tm->tm_mon = 11;
tm->tm_year--;
}
}
else {
if (tm->tm_mon < 11) {
tm->tm_mon++;
}
else {
tm->tm_mon = 0;
tm->tm_year++;
}
}
}
static int
interval_timestamp_monthly(const struct interval *intv, int offset)
{
time_t now = time(NULL);
struct tm *loc = localtime(&now);
int date = (int32_t)be32toh(intv->value);
int monthdays;
while (offset != 0) {
if (offset < 0) {
tm_inc_dec(loc, false);
offset++;
}
else {
tm_inc_dec(loc, true);
offset--;
}
}
monthdays = days_in_month(loc->tm_year + 1900, loc->tm_mon + 1);
if (date > 0) {
if (loc->tm_mday < date)
tm_inc_dec(loc, false);
}
else {
if (loc->tm_mday < (date + monthdays))
tm_inc_dec(loc, false);
monthdays = days_in_month(loc->tm_year + 1900, loc->tm_mon + 1);
date += monthdays;
if (date < 1)
date = 1;
if (date > monthdays)
date = monthdays;
}
return ((loc->tm_year + 1900) * 10000 +
(loc->tm_mon + 1) * 100 +
date);
}
static int
interval_timestamp_fixed(const struct interval *intv, int offset)
{
time_t now, base;
struct tm *loc;
int32_t value;
base = (time_t)be64toh(intv->base);
value = (int32_t)be32toh(intv->value);
now = time(NULL);
now -= now % 86400;
now += offset * (value * 86400);
base = now - ((now - base) % (value * 86400));
loc = localtime(&base);
return ((loc->tm_year + 1900) * 10000 +
(loc->tm_mon + 1) * 100 +
loc->tm_mday);
}
int
interval_pton(const char *spec, struct interval *intv)
{
unsigned int year, month, mday;
struct tm loc = { };
time_t base;
int value;
char *e;
if (sscanf(spec, "%4u-%2u-%2u/%d", &year, &month, &mday, &value) == 4) {
if (year < 2000 || year > 3000)
return -ERANGE;
if (month == 0 || month > 12)
return -ERANGE;
if (mday == 0 || mday > days_in_month(year, month))
return -ERANGE;
if (value <= 0)
return -ERANGE;
loc.tm_isdst = -1;
loc.tm_mday = mday;
loc.tm_mon = month - 1;
loc.tm_year = year - 1900;
base = mktime(&loc);
base -= base % 86400;
intv->type = FIXED;
intv->value = htobe32(value);
intv->base = htobe64(base);
return 0;
}
value = strtol(spec, &e, 10);
if (e == spec || *e != 0)
return -EINVAL;
intv->type = MONTHLY;
intv->base = 0;
intv->value = htobe32(value);
return 0;
}
void
interval_ntop(const struct interval *intv, char *spec, size_t len)
{
struct tm *loc;
time_t base;
switch (intv->type)
{
case FIXED:
base = (time_t)be64toh(intv->base);
loc = localtime(&base);
snprintf(spec, len, "%04d-%02d-%02d/%d",
loc->tm_year + 1900, loc->tm_mon + 1, loc->tm_mday,
intv->value);
break;
case MONTHLY:
snprintf(spec, len, "%d", (int32_t)be32toh(intv->value));
break;
}
}
int
interval_timestamp(const struct interval *intv, int offset)
{
switch (intv->type)
{
case FIXED:
return interval_timestamp_fixed(intv, offset);
case MONTHLY:
return interval_timestamp_monthly(intv, offset);
}
return -EINVAL;
}