X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/95b3a95e742da57976a1cd8f723cc0819cbba53c..4d5d51e903a42c23800540930244448c4aacae16:/sbr/datetime.c diff --git a/sbr/datetime.c b/sbr/datetime.c index a7906a52..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; @@ -153,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; @@ -240,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) { @@ -268,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") || @@ -281,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; } @@ -302,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); } @@ -318,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;