-
Notifications
You must be signed in to change notification settings - Fork 9
/
datetime.c
486 lines (426 loc) · 11.4 KB
/
datetime.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* Copyright (c) Kristaps Dzonsons <[email protected]>
*
* Permission to use, copy, modify, and 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 "config.h"
#include <sys/time.h>
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "libkcaldav.h"
/*
* THIS FILE IS NOT BUILT.
*/
struct icaltmcmp {
unsigned long year; /* x-1900, x>=1970 */
unsigned long mon; /* 1--12 */
unsigned long mday; /* 1--31 (depending) */
unsigned long hour; /* 0--23 */
unsigned long min; /* 0--59 */
unsigned long sec; /* 0--59 */
};
static void
ical_datetime2cmp(struct icaltmcmp *cmp, const struct icaltm *tm)
{
cmp->year = tm->year;
cmp->mon = tm->mon;
cmp->mday = tm->mday;
cmp->hour = tm->hour;
cmp->min = tm->min;
cmp->sec = tm->sec;
}
/*
* See ical_datetime() in ical.c for a description of the algorithm.
* This set the date from a set of broken-down data components.
* Note that "year" is -1900 (so 1970 is just 70).
* If the components are invalid (e.g., 30 February), this returns zero;
* else, it returns non-ezro.
*/
static int
ical_datetimecmp(struct icaltm *tm, const struct icaltmcmp *cmp)
{
size_t i;
unsigned long m, K, J;
unsigned mdaysa[12] = { 0, 31, 59, 90,
120, 151, 181, 212,
243, 273, 304, 334 };
unsigned mdayss[12] = { 31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31 };
memset(tm, 0, sizeof(struct icaltm));
if (cmp->year < 70)
return(0);
tm->year = cmp->year;
tm->ly = ((tm->year & 3) == 0 &&
((tm->year % 25) != 0 || (tm->year & 15) == 0));
if (tm->ly) {
mdayss[1] = 29;
for (i = 2; i < 12; i++)
mdaysa[i]++;
}
if (0 == cmp->mon || cmp->mon > 12)
return(0);
tm->mon = cmp->mon;
if (0 == cmp->mday || cmp->mday > mdayss[tm->mon - 1])
return(0);
tm->mday = cmp->mday;
if (cmp->hour > 23)
return(0);
tm->hour = cmp->hour;
if (cmp->min > 59)
return(0);
tm->min = cmp->min;
if (cmp->sec > 59)
return(0);
tm->sec = cmp->sec;
tm->day = mdaysa[tm->mon - 1] + (tm->mday - 1);
tm->tm = tm->sec + tm->min * 60 + tm->hour * 3600 +
tm->day * 86400 + (tm->year - 70) * 31536000 +
((tm->year - 69) / 4) * 86400 -
((tm->year - 1) / 100) * 86400 +
((tm->year + 299) / 400) * 86400;
m = tm->mon < 3 ? tm->mon + 12 : tm->mon;
K = (tm->year + 1900) % 100;
J = floor((tm->year + 1900) / 100.0);
tm->wday =
(tm->mday +
(unsigned long)floor((13 * (m + 1)) / 5.0) +
K +
(unsigned long)floor(K / 4.0) +
(unsigned long)floor(J / 4.0) +
5 * J) % 7;
return(1);
}
static size_t
ical_rrule_produce(const struct icaltm *cur,
const struct icalrrule *rrule)
{
const char *const weeks[7] = {
"Sat",
"Sun",
"Mon",
"Tues",
"Wed",
"Thurs",
"Fri",
};
/* Check the UNTIL clause. */
/*
* FIXME: UNTIL is in UTC, so we need to convert the "cur->tm"
* value into UTC in order for it to be consistent.
*/
if (0 != rrule->until.tm)
if (cur->tm > rrule->until.tm)
return(0);
fprintf(stderr, "%04lu%02lu%02luT%02lu%02lu%02lu (%s)\n",
cur->year + 1900,
cur->mon,
cur->mday,
cur->hour,
cur->min,
cur->sec,
cur->wday < 7 ? weeks[cur->wday] : "none");
return(1);
}
static size_t
ical_rrule_bysetpos(const struct icaltm *cur,
const struct icalrrule *rrule)
{
return(ical_rrule_produce(cur, rrule));
}
static size_t
ical_rrule_bysecond(const struct icaltm *cur,
const struct icalrrule *rrule)
{
struct icaltmcmp cmp;
struct icaltm tm;
size_t i, found;
int have;
ical_datetime2cmp(&cmp, cur);
for (found = i = 0, have = 0; i < rrule->bminsz; i++) {
cmp.min = rrule->bmin[i];
if ( ! ical_datetimecmp(&tm, &cmp))
continue;
found += ical_rrule_bysetpos(&tm, rrule);
have = 1;
}
/* If no entries, route directly through */
return(have ? found : ical_rrule_bysetpos(cur, rrule));
}
static size_t
ical_rrule_byminute(const struct icaltm *cur,
const struct icalrrule *rrule)
{
struct icaltmcmp cmp;
struct icaltm tm;
size_t i, found;
int have;
ical_datetime2cmp(&cmp, cur);
for (found = i = 0, have = 0; i < rrule->bminsz; i++) {
cmp.min = rrule->bmin[i];
if ( ! ical_datetimecmp(&tm, &cmp))
continue;
found += ical_rrule_bysecond(&tm, rrule);
have = 1;
}
/* If no entries, route directly through */
return(have ? found : ical_rrule_bysecond(cur, rrule));
}
static size_t
ical_rrule_byhour(const struct icaltm *cur,
const struct icalrrule *rrule)
{
struct icaltmcmp cmp;
struct icaltm tm;
size_t i, found;
int have;
ical_datetime2cmp(&cmp, cur);
for (found = i = 0, have = 0; i < rrule->bhrsz; i++) {
cmp.hour = rrule->bhr[i];
if ( ! ical_datetimecmp(&tm, &cmp))
continue;
found += ical_rrule_byminute(&tm, rrule);
have = 1;
}
/* If no entries, route directly through */
return(have ? found : ical_rrule_byminute(cur, rrule));
}
/*
* Recursively daily (day-in-week) component.
*/
static size_t
ical_rrule_byday(const struct icaltm *cur,
const struct icalrrule *rrule)
{
struct icaltmcmp cmp;
struct icaltm tm;
size_t i, found;
unsigned long j, wday;
unsigned long wdays[7][5];
size_t wdaysz[7];
int rc, have;
memset(wdays, 0, sizeof(wdays));
memset(wdaysz, 0, sizeof(wdaysz));
/* Direct recursion: nothing to do here. */
if (0 == rrule->bwkdsz)
return(ical_rrule_byhour(cur, rrule));
/*
* In the current month, compute the days that correspond to a
* given weekday.
* To do so, first compute the weekday given the date, then map
* an array of that weekday's days back into the day of month.
*/
ical_datetime2cmp(&cmp, cur);
for (i = 0; i < 32; i++) {
cmp.mday = i;
if ( ! ical_datetimecmp(&tm, &cmp))
continue;
assert(wdaysz[tm.wday] < sizeof(wdays[tm.wday]));
wdays[tm.wday][wdaysz[tm.wday]++] = cmp.mday;
}
for (found = i = 0, have = 0; i < rrule->bwkdsz; i++) {
assert(rrule->bwkd[i].wkday > ICALWKDAY_NONE);
assert(rrule->bwkd[i].wkday < ICALWKDAY__MAX);
wday = rrule->bwkd[i].wkday % 7;
/* All weekdays in month. */
if (0 == rrule->bwkd[i].wk) {
for (j = 0; j < wdaysz[wday]; j++) {
cmp.mday = wdays[wday][j];
rc = ical_datetimecmp(&tm, &cmp);
assert(rc);
found += ical_rrule_byhour(&tm, rrule);
have = 1;
}
continue;
}
/*
* Specific weekday in month.
* First, see if the weekday requested doesn't exist
* (e.g., the ninth Sunday).
* Second, see if we're counting from the end of the
* month or from the beginning.
*/
if (labs(rrule->bwkd[i].wk) - 1 >= (long)wdaysz[wday])
continue;
cmp.mday = rrule->bwkd[i].wk > 0 ?
wdays[wday][rrule->bwkd[i].wk] :
wdays[wday][wdaysz[wday] + rrule->bwkd[i].wk];
if ( ! ical_datetimecmp(&tm, &cmp))
continue;
found += ical_rrule_byhour(&tm, rrule);
have = 1;
}
return(have ? found : ical_rrule_byhour(cur, rrule));
}
/*
* Recursive day (day-in-month) component.
*/
static size_t
ical_rrule_bymonthday(const struct icaltm *cur,
const struct icalrrule *rrule)
{
struct icaltmcmp cmp;
struct icaltm tm;
size_t i, found;
int have;
unsigned mdayss[12] = { 31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31 };
/* Leap-year February. */
if (cur->ly)
mdayss[1] = 29;
ical_datetime2cmp(&cmp, cur);
for (found = i = 0, have = 0; i < rrule->bmndsz; i++) {
assert(0 != rrule->bmnd[i]);
assert(cmp.mon > 0 && cmp.mon < 13);
/*
* If we're going backwards, then make sure we don't
* specify a day before the beginning of the month,
* e.g., -31 for February.
*/
if (rrule->bmnd[i] < 0 && mdayss[cmp.mon - 1] <
(unsigned long)labs(rrule->bmnd[i]))
continue;
cmp.mday = rrule->bmnd[i] < 0 ?
(unsigned long)(mdayss[cmp.mon - 1] + (rrule->bmnd[i] + 1)) :
(unsigned long)rrule->bmnd[i];
if ( ! ical_datetimecmp(&tm, &cmp))
continue;
found += ical_rrule_byday(&tm, rrule);
have = 1;
}
/* If no entries, route directly through */
return(have ? found : ical_rrule_byday(cur, rrule));
}
/*
* Recrusive weekly (number in year) component.
*/
static size_t
ical_rrule_byweekno(const struct icaltm *cur,
const struct icalrrule *rrule)
{
return(ical_rrule_bymonthday(cur, rrule));
}
/*
* Recursive monthly component.
*/
static size_t
ical_rrule_bymonth(const struct icaltm *cur,
const struct icalrrule *rrule)
{
struct icaltmcmp cmp;
struct icaltm tm;
size_t i, found;
int have;
ical_datetime2cmp(&cmp, cur);
for (found = i = 0, have = 0; i < rrule->bmonsz; i++) {
cmp.mon = rrule->bmon[i];
if ( ! ical_datetimecmp(&tm, &cmp))
continue;
found += ical_rrule_byweekno(&tm, rrule);
have = 1;
}
/* If no entries, route directly through */
return(have ? found : ical_rrule_byweekno(cur, rrule));
}
/*
* Taking the first instance of a RRULE in "first", enumerate all
* possible instances following that.
*/
void
ical_rrule_generate(const struct icaltm *first,
const struct icalrrule *rrule)
{
unsigned long ivl, i, count;
struct icaltm tm;
struct icaltmcmp cmp;
ivl = 0 == rrule->interval ? 1 : rrule->interval;
count = 0 == rrule->count ? 10 : rrule->count;
tm = *first;
/* Create our initial instance. */
ical_datetime2cmp(&cmp, first);
switch (rrule->freq) {
case (ICALFREQ_YEARLY):
for (i = 0; i < count; i++) {
/* Increment over each year. */
cmp.year += ivl;
if ( ! ical_datetimecmp(&tm, &cmp))
continue;
/* Continue til no valid occurrences. */
if (0 == ical_rrule_bymonth(&tm, rrule))
break;
}
break;
default:
break;
}
}
#if 0
time_t
ical_time2utc(const struct icaltime *time)
{
size_t i;
time_t minres, res, diff, mindiff;
const struct icaltz *tz;
if (NULL == time->tz)
return(time->time.tm);
/*
* Initialise this to something because we're not guaranteed
* that the timezone entry will be sane.
* For example, each one of the RRULE components could have
* expired, leaving us nothing to translate into.
*/
mindiff = LONG_MAX;
minres = time->time.tm;
for (i = 0; i < time->tz->tzsz; i++) {
tz = &time->tz->tzs[i];
/* Check if we the rule applies to us. */
if (time->time.tm < tz->dtstart.tm)
continue;
/*
* If there's no RRULE, then we automatically choose
* this subcomponent as the correct one.
*/
res = time->time.tm + tz->tzto;
if (0 == tz->rrule.set)
break;
/* FIXME: check the RDATE. */
/*
* We compute the instance we'll be checking in the
* RRULE as described in the spec: using the tzto.
*/
res = time->time.tm + tz->tzto;
/* See if we fall after the RRULE end date. */
if (0 != tz->rrule.until.tm)
if (res >= tz->rrule.until.tm)
continue;
/*
* Compute the nearest date produced by the RRULE to the
* current date.
* Store the minimum difference, which will be the
* correct timezone component to use.
*/
diff = /*ical_rrule_after(&time->time, &tz->rrule) -
time->time.tm;*/ 0;
if (diff < mindiff) {
minres = res;
mindiff = diff;
}
}
return(minres);
}
#endif