X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/97ed1473582676df043c03adc11997341ce391dd..4d5d51e903a42c23800540930244448c4aacae16:/sbr/datetime.c diff --git a/sbr/datetime.c b/sbr/datetime.c index bcf1a9a6..cdcb8991 100644 --- a/sbr/datetime.c +++ b/sbr/datetime.c @@ -6,8 +6,11 @@ */ #include "h/mh.h" +#include "dtime.h" +#include "error.h" #include "h/icalendar.h" -#include +#include "datetime.h" +#include "h/fmt_scan.h" #include "h/tws.h" #include "h/utils.h" #include "unquote.h" @@ -59,7 +62,8 @@ struct tzdesc { */ static int parse_datetime (const char *datetime, const char *zone, bool dst, - struct tws *tws) { + struct tws *tws) +{ char utc_indicator; bool form_1; int items_matched; @@ -88,8 +92,6 @@ parse_datetime (const char *datetime, const char *zone, bool dst, the entire day. The time fields of the tws struct were initialized to 0 by the memset() above. */ if (items_matched >= 6 || items_matched == 3) { - int offset = atoi (zone ? zone : "0"); - /* struct tws defines tw_mon over [0, 11]. */ --tws->tw_mon; @@ -106,7 +108,11 @@ parse_datetime (const char *datetime, const char *zone, bool dst, - the only flag in tw_flags used is TW_DST */ tws->tw_yday = tws->tw_clock = 0; - tws->tw_zone = 60 * (offset / 100) + offset % 100; + if (zone) { + int offset = atoi(zone); + tws->tw_zone = 60 * (offset / 100) + offset % 100; + } else + tws->tw_zone = 0; if (dst) { tws->tw_zone -= 60; /* per dlocaltime() */ tws->tw_flags |= TW_DST; @@ -151,7 +157,8 @@ parse_datetime (const char *datetime, const char *zone, bool dst, } tzdesc_t -load_timezones (const contentline *clines) { +load_timezones (const contentline *clines) +{ tzdesc_t timezones = NULL, timezone = NULL; bool in_vtimezone, in_standard, in_daylight; tzparams *params = NULL; @@ -171,19 +178,19 @@ load_timezones (const contentline *clines) { if (in_standard) { in_standard = false; } else if (in_daylight) { in_daylight = false; } - if (parse_datetime (params->dtstart, params->offsetfrom, - in_daylight, - &tws) == OK) { - if (tws.tw_year >= 1970) { - /* dmktime() falls apart for, e.g., the year 1601. */ - params->start_dt = tws.tw_clock; - } - } else { + + if (parse_datetime(params->dtstart, params->offsetfrom, + in_daylight, &tws) != OK) { inform("failed to parse start time %s for %s", - params->dtstart, - in_daylight ? "daylight" : "standard"); + params->dtstart, + in_daylight ? "daylight" : "standard"); return NULL; } + + if (tws.tw_year >= 1970) { + /* dmktime() falls apart for, e.g., the year 1601. */ + params->start_dt = tws.tw_clock; + } params = NULL; } else if (! strcasecmp ("DTSTART", node->name)) { /* Save DTSTART for use after getting TZOFFSETFROM. */ @@ -238,7 +245,8 @@ load_timezones (const contentline *clines) { } void -free_timezones (tzdesc_t timezone) { +free_timezones (tzdesc_t timezone) +{ tzdesc_t next; for ( ; timezone; timezone = next) { @@ -266,9 +274,10 @@ free_timezones (tzdesc_t timezone) { * Given a recurrence rule and year, calculate its time in seconds * from 01 January UTC of the year. */ -time_t +static time_t rrule_clock (const char *rrule, const char *starttime, const char *zone, - unsigned int year) { + unsigned int year) +{ time_t clock = 0; if (nmh_strcasestr (rrule, "FREQ=YEARLY;INTERVAL=1") || @@ -279,14 +288,15 @@ rrule_clock (const char *rrule, const char *starttime, const char *zone, int specific_day = 1; /* BYDAY integer (prefix) */ char buf[32]; int day; + int end_of_week; if ((cp = nmh_strcasestr (rrule, "BYDAY="))) { cp += 6; /* BYDAY integers must be ASCII. */ - if (*cp == '+') { ++cp; } /* +n specific day; don't support '-' */ - else if (*cp == '-') { goto fail; } + if (*cp == '+') { ++cp; } /* +n specific day */ + else if (*cp == '-') { ++cp; specific_day = -1; } - if (isdigit ((unsigned char) *cp)) { specific_day = *cp++ - 0x30; } + if (isdigit ((unsigned char) *cp)) { specific_day *= *cp++ - 0x30; } if (! strncasecmp (cp, "SU", 2)) { wday = 0; } else if (! strncasecmp (cp, "MO", 2)) { wday = 1; } @@ -300,12 +310,13 @@ rrule_clock (const char *rrule, const char *starttime, const char *zone, month = atoi (cp + 8); } - for (day = 1; day <= 7; ++day) { + end_of_week = specific_day > 0 ? 7 * specific_day : + dmlastday(year, month) + 7 * (1 + specific_day); + for (day = end_of_week - 6; day <= end_of_week; ++day) { /* E.g, 11-01-2014 02:00:00-0400 */ snprintf (buf, sizeof buf, "%02d-%02d-%04u %.2s:%.2s:%.2s%s", - month, day + 7 * (specific_day-1), year, - starttime, starttime + 2, starttime + 4, - zone ? zone : "0000"); + month, day, year, starttime, starttime + 2, + starttime + 4, zone ? zone : "0000"); if ((tws = dparsetime (buf))) { if (! (tws->tw_flags & (TW_SEXP|TW_SIMP))) { set_dotw (tws); } @@ -316,22 +327,22 @@ rrule_clock (const char *rrule, const char *starttime, const char *zone, } } - if (day <= 7) { + if (day <= end_of_week) { clock = tws->tw_clock; } } -fail: if (clock == 0) { inform("Unsupported RRULE format: %s, assume local timezone, continuing...", - rrule); + rrule); } return clock; } char * -format_datetime (tzdesc_t timezones, const contentline *node) { +format_datetime (tzdesc_t timezones, const contentline *node) +{ param_list *p; char *dt_timezone = NULL; int dst = 0; @@ -358,11 +369,11 @@ format_datetime (tzdesc_t timezones, const contentline *node) { if (! dt_timezone) { /* Form #1: DATE WITH LOCAL TIME, i.e., no time zone, or Form #2: DATE WITH UTC TIME */ - if (parse_datetime (node->value, NULL, false, &tws[0]) == OK) { - return strdup (dasctime (&tws[0], 0)); + if (parse_datetime(node->value, NULL, false, &tws[0]) != OK) { + inform("unable to parse datetime %s", node->value); + return NULL; } - inform("unable to parse datetime %s", node->value); - return NULL; + return strdup (dasctime (&tws[0], 0)); } /* @@ -380,13 +391,12 @@ format_datetime (tzdesc_t timezones, const contentline *node) { if (tz->tzid && ! strcasecmp (dt_timezone, tz->tzid)) { break; } } - if (tz) { - free(dt_timezone); - } else { + if (!tz) { inform("did not find VTIMEZONE section for %s", dt_timezone); free(dt_timezone); return NULL; } + free(dt_timezone); /* Determine if it's Daylight Saving. */ tp_std = strchr (tz->standard_params.dtstart, 'T'); @@ -431,28 +441,23 @@ format_datetime (tzdesc_t timezones, const contentline *node) { return NULL; } - if (parse_datetime (node->value, tz->standard_params.offsetto, - false, &tws[0]) == OK) { - dt[0] = tws[0].tw_clock; - } else { + if (parse_datetime(node->value, tz->standard_params.offsetto, + false, &tws[0]) != OK) { inform("unable to parse datetime %s", node->value); return NULL; } + dt[0] = tws[0].tw_clock; if (tp_dst) { if (dt[0] < transition[1]) { dst = 0; } else { - if (parse_datetime (node->value, - tz->daylight_params.offsetto, true, - &tws[1]) == OK) { - dt[1] = tws[1].tw_clock; - } else { - inform("unable to parse datetime %s", - node->value); + if (parse_datetime(node->value, + tz->daylight_params.offsetto, true, &tws[1]) != OK) { + inform("unable to parse datetime %s", node->value); return NULL; } - + dt[1] = tws[1].tw_clock; dst = dt[1] <= transition[0]; } }