This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
decode_time.c
375 lines (335 loc) · 8.84 KB
/
decode_time.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
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
// Copyright 2013-2018 René Ladan
// SPDX-License-Identifier: BSD-2-Clause
#include "decode_time.h"
#include "calendar.h"
#include <stdbool.h>
#include <string.h>
#include <time.h>
static int dst_count, leap_count, minute_count;
static struct DT_result dt_res;
static bool
getpar(const int buffer[], unsigned start, unsigned stop)
{
int par = 0;
for (unsigned i = start; i <= stop; i++) {
par += buffer[i];
}
return (par & 1) == 0;
}
static int
getbcd(const int buffer[], unsigned start, unsigned stop)
{
int mul = 1, val = 0;
for (unsigned i = start; i <= stop; i++) {
val += mul * buffer[i];
mul *= 2;
if (mul == 16) {
if (val > 9) {
return 100;
}
mul = 10;
}
}
return val;
}
static bool
check_time_sanity(int minlen, const int buffer[])
{
if (minlen == -1 || minlen > 60) {
dt_res.minute_length = emin_long;
} else if (minlen < 59) {
dt_res.minute_length = emin_short;
} else {
dt_res.minute_length = emin_ok;
}
dt_res.bit0_ok = buffer[0] == 0;
dt_res.bit20_ok = buffer[20] == 1;
if (buffer[17] == buffer[18]) {
dt_res.dst_status = eDST_error;
} else {
dt_res.dst_status = eDST_ok;
}
/* only decode if set */
return (dt_res.minute_length == emin_ok) && dt_res.bit0_ok &&
dt_res.bit20_ok && (dt_res.dst_status == eDST_ok);
}
static void
handle_special_bits(const int buffer[])
{
dt_res.transmit_call = buffer[15] == 1;
}
static int
increase_old_time(unsigned init_min, int minlen, unsigned acc_minlen,
struct tm * const time)
{
static unsigned acc_minlen_partial;
int increase;
/* See if there are any partial / split minutes to be combined: */
if (acc_minlen <= 59000) {
acc_minlen_partial += acc_minlen;
if (acc_minlen_partial >= 60000) {
acc_minlen = acc_minlen_partial;
acc_minlen_partial %= 60000;
}
}
/* Calculate number of minutes to increase time with: */
increase = acc_minlen / 60000;
if (acc_minlen >= 60000) {
acc_minlen_partial %= 60000;
}
/* Account for complete minutes with a short acc_minlen: */
if (acc_minlen % 60000 > 59000) {
increase++;
acc_minlen_partial %= 60000;
}
/* There is no previous time on the very first (partial) minute: */
if (init_min < 2) {
for (int i = increase; increase > 0 && i > 0; i--) {
*time = add_minute(*time, dt_res.dst_announce);
}
for (int i = increase; increase < 0 && i < 0; i++) {
*time = substract_minute(*time, dt_res.dst_announce);
}
}
return increase;
}
static unsigned
calculate_date_time(unsigned init_min, unsigned errflags, int increase,
const int buffer[], struct tm time, struct tm * const newtime)
{
int tmp0, tmp1, tmp2, tmp3;
bool p1, p2, p3;
p1 = getpar(buffer, 21, 28);
tmp0 = getbcd(buffer, 21, 27);
if (!p1) {
dt_res.minute_status = eval_parity;
} else if (tmp0 > 59) {
dt_res.minute_status = eval_bcd;
p1 = false;
} else {
dt_res.minute_status = eval_ok;
}
if ((init_min == 2 || increase != 0) && p1 && errflags == 0) {
newtime->tm_min = tmp0;
if (init_min == 0 && time.tm_min != newtime->tm_min) {
dt_res.minute_status = eval_jump;
}
}
p2 = getpar(buffer, 29, 35);
tmp0 = getbcd(buffer, 29, 34);
if (!p2) {
dt_res.hour_status = eval_parity;
} else if (tmp0 > 23) {
dt_res.hour_status = eval_bcd;
p2 = false;
} else {
dt_res.hour_status = eval_ok;
}
if ((init_min == 2 || increase != 0) && p2 && errflags == 0) {
newtime->tm_hour = tmp0;
if (init_min == 0 && time.tm_hour != newtime->tm_hour) {
dt_res.hour_status = eval_jump;
}
}
p3 = getpar(buffer, 36, 58);
tmp0 = getbcd(buffer, 36, 41);
tmp1 = getbcd(buffer, 42, 44);
tmp2 = getbcd(buffer, 45, 49);
tmp3 = getbcd(buffer, 50, 57);
if (!p3) {
dt_res.mday_status = eval_parity;
dt_res.wday_status = eval_parity;
dt_res.month_status = eval_parity;
dt_res.year_status = eval_parity;
} else {
if (tmp0 == 0 || tmp0 > 31) {
dt_res.mday_status = eval_bcd;
p3 = false;
} else {
dt_res.mday_status = eval_ok;
}
if (tmp1 == 0) {
dt_res.wday_status = eval_bcd;
p3 = false;
} else {
dt_res.wday_status = eval_ok;
}
if (tmp2 == 0 || tmp2 > 12) {
dt_res.month_status = eval_bcd;
p3 = false;
} else {
dt_res.month_status = eval_ok;
}
if (tmp3 > 99) {
dt_res.year_status = eval_bcd;
p3 = false;
} else {
dt_res.year_status = eval_ok;
}
}
if ((init_min == 2 || increase != 0) && p3 && errflags == 0) {
int centofs;
newtime->tm_mday = tmp0;
if (init_min == 0 && time.tm_mday != newtime->tm_mday) {
dt_res.mday_status = eval_jump;
}
newtime->tm_wday = tmp1;
if (init_min == 0 && time.tm_wday != newtime->tm_wday) {
dt_res.wday_status = eval_jump;
}
newtime->tm_mon = tmp2;
if (init_min == 0 && time.tm_mon != newtime->tm_mon) {
dt_res.month_status = eval_jump;
}
newtime->tm_year = tmp3;
centofs = century_offset(*newtime);
if (centofs == -1) {
dt_res.year_status = eval_bcd;
p3 = false;
} else {
if (init_min == 0 && time.tm_year != base_year +
100 * centofs + newtime->tm_year) {
dt_res.year_status = eval_jump;
}
newtime->tm_year += base_year + 100 * centofs;
if (newtime->tm_mday > lastday(*newtime)) {
dt_res.mday_status = eval_bcd;
p3 = false;
}
}
}
return (errflags << 3) | ((!p3) << 2) | ((!p2) << 1) | (!p1);
}
static void
stamp_date_time(unsigned errflags, struct tm newtime, struct tm * const time)
{
if ((dt_res.minute_length == emin_ok) && ((errflags & 0x0f) == 0)) {
time->tm_min = newtime.tm_min;
time->tm_hour = newtime.tm_hour;
time->tm_mday = newtime.tm_mday;
time->tm_mon = newtime.tm_mon;
time->tm_year = newtime.tm_year;
time->tm_wday = newtime.tm_wday;
if (dt_res.dst_status != eDST_jump) {
time->tm_isdst = newtime.tm_isdst;
}
}
}
static unsigned
handle_leap_second(unsigned errflags, int minlen, const int buffer[],
struct tm time)
{
/* determine if a leap second is announced */
if (buffer[19] == 1 && errflags == 0) {
leap_count++;
}
if (time.tm_min > 0) {
dt_res.leap_announce = 2 * leap_count > minute_count;
}
/* process possible leap second */
if (dt_res.leap_announce && time.tm_min == 0) {
dt_res.leapsecond_status = els_done;
if (minlen == 59) {
/* leap second processed, but missing */
dt_res.minute_length = emin_short;
errflags |= (1 << 4);
} else if (minlen == 60 && buffer[59] == 1) {
dt_res.leapsecond_status = els_one;
}
} else {
dt_res.leapsecond_status = els_none;
}
if (minlen == 60 && dt_res.leapsecond_status == els_none) {
/* leap second not processed, so bad minute */
dt_res.minute_length = emin_long;
errflags |= (1 << 4);
}
/* always reset announcement at hh:00 */
if (time.tm_min == 0) {
dt_res.leap_announce = false;
leap_count = 0;
}
return errflags;
}
static unsigned
handle_dst(unsigned errflags, bool olderr, const int buffer[], struct tm time,
struct tm * const newtime)
{
/* determine if a DST change is announced */
if (buffer[16] == 1 && errflags == 0) {
dst_count++;
}
if (time.tm_min > 0) {
dt_res.dst_announce = 2 * dst_count > minute_count;
}
if (buffer[17] != time.tm_isdst || buffer[18] == time.tm_isdst) {
/*
* Time offset change is OK if:
* - announced and on the hour
* - there was an error but not any more (needed if decoding
* at startup is problematic)
* - initial state (otherwise DST would never be valid)
*/
if ((dt_res.dst_announce && time.tm_min == 0) ||
(olderr && errflags == 0) ||
(dt_res.dst_status == eDST_ok && time.tm_isdst == -1)) {
newtime->tm_isdst = buffer[17]; /* expected change */
} else {
if (dt_res.dst_status != eDST_error) {
dt_res.dst_status = eDST_jump;
/* sudden change, ignore */
}
errflags |= (1 << 5);
}
}
/* done with DST */
if (dt_res.dst_announce && time.tm_min == 0) {
dt_res.dst_status = eDST_done;
/*
* like leap second, always clear the DST announcement at hh:00
*/
}
if (time.tm_min == 0) {
dt_res.dst_announce = false;
dst_count = 0;
}
return errflags;
}
struct DT_result
decode_time(unsigned init_min, int minlen, unsigned acc_minlen,
const int buffer[], struct tm * const time)
{
static bool olderr;
unsigned errflags;
int increase;
struct tm newtime;
memset(&newtime, 0, sizeof(newtime));
/* Initially, set time offset to unknown */
if (init_min == 2) {
time->tm_isdst = -1;
}
newtime.tm_isdst = time->tm_isdst; /* save DST value */
errflags = check_time_sanity(minlen, buffer) ? 0 : 1;
if (errflags == 0) {
handle_special_bits(buffer);
if (++minute_count == 60) {
minute_count = 0;
}
}
increase = increase_old_time(init_min, minlen, acc_minlen, time);
errflags = calculate_date_time(init_min, errflags, increase, buffer,
*time, &newtime);
if (init_min < 2) {
errflags = handle_leap_second(errflags, minlen, buffer, *time);
errflags = handle_dst(errflags, olderr, buffer, *time,
&newtime);
}
stamp_date_time(errflags, newtime, time);
if (olderr && (errflags == 0)) {
olderr = false;
}
if (errflags != 0) {
olderr = true;
}
return dt_res;
}