]>
diplodocus.org Git - nmh/blob - sbr/datetime.c
71a21f99f6d56eaec53df24c51e6def9c51af8b2
1 /* datetime.c -- functions for manipulating RFC 5545 date-time values
3 * This code is Copyright (c) 2014, by the authors of nmh.
4 * See the COPYRIGHT file in the root directory of the nmh
5 * distribution for complete copyright information.
11 #include "h/icalendar.h"
12 #include "h/fmt_scan.h"
18 * This doesn't try to support all of the myriad date-time formats
19 * allowed by RFC 5545. It is only used for viewing date-times,
20 * so that shouldn't be a problem: if a particular format can't
21 * be handled by this code, just present it to the user in its
24 * And, this assumes a valid iCalendar input file. E.g, it
25 * doesn't check that each BEGIN has a matching END and vice
26 * versa. That should be done in the parser, though it currently
30 typedef struct tzparams
{
31 /* Pointers to values in parse tree.
32 * TZOFFSETFROM is used to calculate the absolute time at which
33 * the transition to a given observance takes place.
34 * TZOFFSETTO is the timezone offset from UTC. Both are in HHmm
36 char *offsetfrom
, *offsetto
;
40 /* This is only used to make sure that timezone applies. And not
41 always, because if the timezone DTSTART is before the epoch, we
42 don't try to compare to it. */
43 time_t start_dt
; /* in seconds since epoch */
49 /* The following are translations of the pieces of RRULE and DTSTART
50 into seconds from beginning of year. */
51 tzparams standard_params
;
52 tzparams daylight_params
;
58 * Parse a datetime of the form YYYYMMDDThhmmss and a string
59 * representation of the timezone in units of [+-]hhmm and load the
63 parse_datetime (const char *datetime
, const char *zone
, bool dst
,
72 sscanf (datetime
, "%4d%2d%2dT%2d%2d%2d%c",
73 &tws
->tw_year
, &tws
->tw_mon
, &tws
->tw_mday
,
74 &tws
->tw_hour
, &tws
->tw_min
, &tws
->tw_sec
,
76 tws
->tw_flags
= TW_NULL
;
79 if (items_matched
== 7) {
80 /* The 'Z' must be capital according to RFC 5545 Sec. 3.3.5. */
81 if (utc_indicator
!= 'Z') {
82 inform("%s has invalid timezone-indicator byte: %#x",
83 datetime
, utc_indicator
);
86 } else if (zone
== NULL
) {
90 /* items_matched of 3 is for, e.g., 20151230. Assume that means
91 the entire day. The time fields of the tws struct were
92 initialized to 0 by the memset() above. */
93 if (items_matched
>= 6 || items_matched
== 3) {
94 /* struct tws defines tw_mon over [0, 11]. */
97 /* Fill out rest of tws, i.e., its tw_wday and tw_flags. */
99 /* set_dotw() sets TW_SIMP. Replace that with TW_SEXP so that
100 dasctime() outputs the dotw before the date instead of after. */
101 tws
->tw_flags
&= ~TW_SDAY
;
102 tws
->tw_flags
|= TW_SEXP
;
104 /* For the call to dmktime():
106 - tw_clock must be 0 on entry, and is set by dmktime()
107 - the only flag in tw_flags used is TW_DST
109 tws
->tw_yday
= tws
->tw_clock
= 0;
111 int offset
= atoi(zone
);
112 tws
->tw_zone
= 60 * (offset
/ 100) + offset
% 100;
116 tws
->tw_zone
-= 60; /* per dlocaltime() */
117 tws
->tw_flags
|= TW_DST
;
119 /* dmktime() just sets tws->tw_clock. */
120 (void) dmktime (tws
);
123 /* Set TW_SZEXP so that dasctime outputs timezone, except
124 with local time (Form #1). */
125 tws
->tw_flags
|= TW_SZEXP
;
127 /* Convert UTC time to time in local timezone. However,
128 don't try for years before 1970 because dlocatime()
129 doesn't handle them well. dlocaltime() will succeed if
130 tws->tw_clock is nonzero. */
131 if (tws
->tw_year
>= 1970 && tws
->tw_clock
> 0) {
132 const int was_dst
= tws
->tw_flags
& TW_DST
;
134 *tws
= *dlocaltime (&tws
->tw_clock
);
135 if (was_dst
&& ! (tws
->tw_flags
& TW_DST
)) {
136 /* dlocaltime() changed the DST flag from 1 to 0,
137 which means the time is in the hour (assumed to
138 be one hour) that is lost in the transition to
139 DST. So per RFC 5545 Sec. 3.3.5, "the
140 DATE-TIME value is interpreted using the UTC
141 offset before the gap in local times." In
142 other words, add an hour to it.
143 No adjustment is necessary for the transition
144 from DST to standard time, because dasctime()
145 shows the first occurrence of the time. */
146 tws
->tw_clock
+= 3600;
147 *tws
= *dlocaltime (&tws
->tw_clock
);
159 load_timezones (const contentline
*clines
)
161 tzdesc_t timezones
= NULL
, timezone
= NULL
;
162 bool in_vtimezone
, in_standard
, in_daylight
;
163 tzparams
*params
= NULL
;
164 const contentline
*node
;
166 /* Interpret each VTIMEZONE section. */
167 in_vtimezone
= in_standard
= in_daylight
= false;
168 for (node
= clines
; node
; node
= node
->next
) {
169 /* node->name will be NULL if the line was "deleted". */
170 if (! node
->name
) { continue; }
172 if (in_daylight
|| in_standard
) {
173 if (! strcasecmp ("END", node
->name
) &&
174 ((in_standard
&& ! strcasecmp ("STANDARD", node
->value
)) ||
175 (in_daylight
&& ! strcasecmp ("DAYLIGHT", node
->value
)))) {
178 if (in_standard
) { in_standard
= false; }
179 else if (in_daylight
) { in_daylight
= false; }
181 if (parse_datetime(params
->dtstart
, params
->offsetfrom
,
182 in_daylight
, &tws
) != OK
) {
183 inform("failed to parse start time %s for %s",
185 in_daylight
? "daylight" : "standard");
189 if (tws
.tw_year
>= 1970) {
190 /* dmktime() falls apart for, e.g., the year 1601. */
191 params
->start_dt
= tws
.tw_clock
;
194 } else if (! strcasecmp ("DTSTART", node
->name
)) {
195 /* Save DTSTART for use after getting TZOFFSETFROM. */
196 params
->dtstart
= node
->value
;
197 } else if (! strcasecmp ("TZOFFSETFROM", node
->name
)) {
198 params
->offsetfrom
= node
->value
;
199 } else if (! strcasecmp ("TZOFFSETTO", node
->name
)) {
200 params
->offsetto
= node
->value
;
201 } else if (! strcasecmp ("RRULE", node
->name
)) {
202 params
->rrule
= node
->value
;
204 } else if (in_vtimezone
) {
205 if (! strcasecmp ("END", node
->name
) &&
206 ! strcasecmp ("VTIMEZONE", node
->value
)) {
207 in_vtimezone
= false;
208 } else if (! strcasecmp ("BEGIN", node
->name
) &&
209 ! strcasecmp ("STANDARD", node
->value
)) {
211 params
= &timezone
->standard_params
;
212 } else if (! strcasecmp ("BEGIN", node
->name
) &&
213 ! strcasecmp ("DAYLIGHT", node
->value
)) {
215 params
= &timezone
->daylight_params
;
216 } else if (! strcasecmp ("TZID", node
->name
)) {
217 /* See comment below in format_datetime() about removing any enclosing quotes from a
218 timezone identifier. */
219 char *buf
= mh_xmalloc(strlen(node
->value
) + 1);
220 unquote_string(node
->value
, buf
);
221 timezone
->tzid
= buf
;
224 if (! strcasecmp ("BEGIN", node
->name
) &&
225 ! strcasecmp ("VTIMEZONE", node
->value
)) {
232 for (t
= timezones
; t
&& t
->next
; t
= t
->next
) { continue; }
233 /* The loop terminated at, not after, the last
237 timezones
= timezone
;
247 free_timezones (tzdesc_t timezone
)
251 for ( ; timezone
; timezone
= next
) {
252 free (timezone
->tzid
);
253 next
= timezone
->next
;
259 * Convert time to local timezone, accounting for daylight saving time:
260 * - Detect which type of datetime the node contains:
261 * Form #1: DATE WITH LOCAL TIME
262 * Form #2: DATE WITH UTC TIME
263 * Form #3: DATE WITH LOCAL TIME AND TIME ZONE REFERENCE
264 * - Convert value to local time in seconds since epoch.
265 * - If there's a DST in the timezone, convert its start and end
266 * date-times to local time in seconds, also. Then determine
267 * if the value is between them, and therefore DST. Otherwise, it's
269 * - Format the time value.
273 * Given a recurrence rule and year, calculate its time in seconds
274 * from 01 January UTC of the year.
277 rrule_clock (const char *rrule
, const char *starttime
, const char *zone
,
282 if (nmh_strcasestr (rrule
, "FREQ=YEARLY;INTERVAL=1") ||
283 (nmh_strcasestr (rrule
, "FREQ=YEARLY") && nmh_strcasestr(rrule
, "INTERVAL") == NULL
)) {
286 int wday
= -1, month
= -1;
287 int specific_day
= 1; /* BYDAY integer (prefix) */
291 if ((cp
= nmh_strcasestr (rrule
, "BYDAY="))) {
293 /* BYDAY integers must be ASCII. */
294 if (*cp
== '+') { ++cp
; } /* +n specific day; don't support '-' */
295 else if (*cp
== '-') { goto fail
; }
297 if (isdigit ((unsigned char) *cp
)) { specific_day
= *cp
++ - 0x30; }
299 if (! strncasecmp (cp
, "SU", 2)) { wday
= 0; }
300 else if (! strncasecmp (cp
, "MO", 2)) { wday
= 1; }
301 else if (! strncasecmp (cp
, "TU", 2)) { wday
= 2; }
302 else if (! strncasecmp (cp
, "WE", 2)) { wday
= 3; }
303 else if (! strncasecmp (cp
, "TH", 2)) { wday
= 4; }
304 else if (! strncasecmp (cp
, "FR", 2)) { wday
= 5; }
305 else if (! strncasecmp (cp
, "SA", 2)) { wday
= 6; }
307 if ((cp
= nmh_strcasestr (rrule
, "BYMONTH="))) {
308 month
= atoi (cp
+ 8);
311 for (day
= 1; day
<= 7; ++day
) {
312 /* E.g, 11-01-2014 02:00:00-0400 */
313 snprintf (buf
, sizeof buf
, "%02d-%02d-%04u %.2s:%.2s:%.2s%s",
314 month
, day
+ 7 * (specific_day
-1), year
,
315 starttime
, starttime
+ 2, starttime
+ 4,
316 zone
? zone
: "0000");
317 if ((tws
= dparsetime (buf
))) {
318 if (! (tws
->tw_flags
& (TW_SEXP
|TW_SIMP
))) { set_dotw (tws
); }
320 if (tws
->tw_wday
== wday
) {
321 /* Found the day specified in the RRULE. */
328 clock
= tws
->tw_clock
;
334 inform("Unsupported RRULE format: %s, assume local timezone, continuing...",
342 format_datetime (tzdesc_t timezones
, const contentline
*node
)
345 char *dt_timezone
= NULL
;
347 struct tws tws
[2]; /* [standard, daylight] */
349 char *tp_std
, *tp_dst
, *tp_dt
;
351 /* Extract the timezone, if specified (RFC 5545 Sec. 3.3.5 Form #3). */
352 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
353 if (! strcasecmp (p
->param_name
, "TZID") && p
->values
) {
354 /* Remove any enclosing quotes from the timezone identifier. I don't believe that it's
355 legal for it to be quoted, according to RFC 5545 ยง 3.2.19:
356 tzidparam = "TZID" "=" [tzidprefix] paramtext
358 where paramtext includes SAFE-CHAR, which specifically excludes DQUOTE. But we'll
359 be generous and strip quotes. */
360 char *buf
= mh_xmalloc(strlen(p
->values
->value
) + 1);
361 unquote_string(p
->values
->value
, buf
);
368 /* Form #1: DATE WITH LOCAL TIME, i.e., no time zone, or
369 Form #2: DATE WITH UTC TIME */
370 if (parse_datetime(node
->value
, NULL
, false, &tws
[0]) != OK
) {
371 inform("unable to parse datetime %s", node
->value
);
374 return strdup (dasctime (&tws
[0], 0));
379 * Form #3: DATE WITH LOCAL TIME AND TIME ZONE REFERENCE
382 /* Find the corresponding tzdesc. */
383 for (tz
= timezones
; dt_timezone
&& tz
; tz
= tz
->next
) {
384 /* Property parameter values are case insensitive (RFC 5545
385 Sec. 2) and time zone identifiers are property parameters
386 (RFC 5545 Sec. 3.8.2.4), though it would seem odd to use
387 different case in the same file for identifiers that are
388 supposed to be the same. */
389 if (tz
->tzid
&& ! strcasecmp (dt_timezone
, tz
->tzid
)) { break; }
393 inform("did not find VTIMEZONE section for %s", dt_timezone
);
399 /* Determine if it's Daylight Saving. */
400 tp_std
= strchr (tz
->standard_params
.dtstart
, 'T');
401 tp_dt
= strchr (node
->value
, 'T');
403 if (tz
->daylight_params
.dtstart
) {
404 tp_dst
= strchr (tz
->daylight_params
.dtstart
, 'T');
406 /* No DAYLIGHT section. */
411 if (tp_std
&& tp_dt
) {
412 time_t transition
[2] = { 0, 0 }; /* [standard, daylight] */
413 time_t dt
[2]; /* [standard, daylight] */
417 /* Datetime is form YYYYMMDDThhmmss. Extract year. */
418 memcpy (buf
, node
->value
, sizeof buf
- 1);
419 buf
[sizeof buf
- 1] = '\0';
422 if (tz
->standard_params
.rrule
) {
423 /* +1 to skip the T before the time */
425 rrule_clock (tz
->standard_params
.rrule
, tp_std
+ 1,
426 tz
->standard_params
.offsetfrom
, year
);
428 if (tp_dst
&& tz
->daylight_params
.rrule
) {
429 /* +1 to skip the T before the time */
431 rrule_clock (tz
->daylight_params
.rrule
, tp_dst
+ 1,
432 tz
->daylight_params
.offsetfrom
, year
);
435 if (transition
[0] < transition
[1]) {
436 inform("format_datetime() requires that daylight "
437 "saving time transition precede standard time "
442 if (parse_datetime(node
->value
, tz
->standard_params
.offsetto
,
443 false, &tws
[0]) != OK
) {
444 inform("unable to parse datetime %s", node
->value
);
447 dt
[0] = tws
[0].tw_clock
;
450 if (dt
[0] < transition
[1]) {
453 if (parse_datetime(node
->value
,
454 tz
->daylight_params
.offsetto
, true, &tws
[1]) != OK
) {
455 inform("unable to parse datetime %s", node
->value
);
458 dt
[1] = tws
[1].tw_clock
;
459 dst
= dt
[1] <= transition
[0];
464 if (tz
->daylight_params
.start_dt
> 0 &&
465 dt
[dst
] < tz
->daylight_params
.start_dt
) {
466 inform("date-time of %s is before VTIMEZONE start "
467 "of %s", node
->value
,
468 tz
->daylight_params
.dtstart
);
472 if (tz
->standard_params
.start_dt
> 0 &&
473 dt
[dst
] < tz
->standard_params
.start_dt
) {
474 inform("date-time of %s is before VTIMEZONE start "
475 "of %s", node
->value
,
476 tz
->standard_params
.dtstart
);
482 inform("unsupported date-time format: %s",
483 tz
->standard_params
.dtstart
);
487 inform("unsupported date-time format: %s", node
->value
);
492 return strdup (dasctime (&tws
[dst
], 0));