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; }
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); }
}
}
- if (day <= 7) {
+ if (day <= end_of_week) {
clock = tws->tw_clock;
}
}
2,147,483,648 / 60 = 35,791,394 */
#define DTZ_BUFFER_SIZE (sizeof "+3579139459")
+/*
+ * 1 if leap year, 0 otherwise.
+ */
+#define isleapyear01(y) \
+ (((y) % 4) ? 0 : (((y) % 100) ? 1 : (((y) % 400) ? 0 : 1)))
+
/*
* The number of days in the year, accounting for leap years
*/
#define dysize(y) \
- (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
+ (365 + isleapyear01(y))
char *tw_moty[] = {
"Jan", "Feb", "Mar", "Apr",
"Saturday", NULL
};
-static int dmsize[] = {
- 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+static int dmsize[][12] = {
+ { /* Not a leap year */
+ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+ },
+ { /* Leap year: February = 29 */
+ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+ }
};
}
+/*
+ * Last day of month (1-12) in given year.
+ */
+
+int
+dmlastday(int year, int mon)
+{
+ return dmsize[isleapyear01 (year)][mon - 1];
+}
+
/*
* Convert nmh time structure for local "broken-down"
* time to calendar time (clock value). This routine
time_t
dmktime (struct tws *tw)
{
- int i, sec, min, hour, mday, mon, year;
+ int i, sec, min, hour, mday, mon, year, *msize;
time_t result;
if (tw->tw_clock != 0)
for (i = 1970; i < year; i++)
result += dysize (i);
- if (dysize (year) == 366 && mon >= 3)
- result++;
+ msize = dmsize[isleapyear01 (year)];
while (--mon)
- result += dmsize[mon - 1];
+ result += msize[mon - 1];
result += mday - 1;
result *= 24; /* Days to hours. */
result += hour;
char *dtime(time_t *, int);
char *dasctime(struct tws *, int);
char *dtimezone(int, int);
+int dmlastday(int, int);
time_t dmktime(struct tws *);
void set_dotw(struct tws *);
int twsort(struct tws *, struct tws *);
actual_err="$MH_TEST_DIR/test-mhical$$.actual_err"
+# check timezone boundary at transition from daylight saving time, -2SU
+start_test "timezone boundary at transition from daylight saving time, -2SU"
+# Specifically looking at "second last Sunday of the month" type transitions.
+cat >"$expected" <<'EOF'
+Summary: BST to GMT
+At: Sat, 22 Oct 1994 23:33 +0000
+To: Sun, 23 Oct 1994 07:34
+EOF
+
+cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
+BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:test-mhical
+BEGIN:VTIMEZONE
+TZID:London
+BEGIN:STANDARD
+TZNAME:GMT
+DTSTART:19931018T020000
+TZOFFSETFROM:+0100
+TZOFFSETTO:+0000
+RRULE:FREQ=YEARLY;BYDAY=-2SU;BYMONTH=10
+END:STANDARD
+BEGIN:DAYLIGHT
+TZNAME:BST
+DTSTART:19810329T010000
+TZOFFSETFROM:+0000
+TZOFFSETTO:+0100
+RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
+END:DAYLIGHT
+END:VTIMEZONE
+BEGIN:VEVENT
+DTSTAMP:19941002T115852Z
+DTSTART;TZID=London:19941022T233300
+DTEND;TZID=London:19941023T073400
+Summary: BST to GMT
+END:VEVENT
+END:VCALENDAR
+EOF
+
+TZ=GMT mhical <"$MH_TEST_DIR/test1.ics" >"$MH_TEST_DIR/test1.txt"
+check "$expected" "$MH_TEST_DIR/test1.txt"
+
+
+# check timezone boundary at transition to daylight saving time, -1SU
+start_test "timezone boundary at transition to daylight saving time, -1SU"
+# Specifically looking at "last Sunday of the month" type transitions.
+cat >"$expected" <<'EOF'
+Summary: GMT to BST
+At: Sat, 27 Mar 1982 23:31 +0000
+To: Sun, 28 Mar 1982 07:32
+EOF
+
+cat >"$MH_TEST_DIR/test1.ics" <<'EOF'
+BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:test-mhical
+BEGIN:VTIMEZONE
+TZID:London
+BEGIN:STANDARD
+TZNAME:GMT
+DTSTART:19781025T030000
+TZOFFSETFROM:+0100
+TZOFFSETTO:+0000
+RRULE:FREQ=YEARLY;UNTIL=19811025T010000Z;BYDAY=-1SU;BYMONTH=10
+END:STANDARD
+BEGIN:DAYLIGHT
+TZNAME:BST
+DTSTART:19810329T010000
+TZOFFSETFROM:+0000
+TZOFFSETTO:+0100
+RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
+END:DAYLIGHT
+END:VTIMEZONE
+BEGIN:VEVENT
+DTSTAMP:19820302T115852Z
+DTSTART;TZID=London:19820327T233100
+DTEND;TZID=London:19820328T073200
+Summary: GMT to BST
+END:VEVENT
+END:VCALENDAR
+EOF
+
+TZ=GMT mhical <"$MH_TEST_DIR/test1.ics" >"$MH_TEST_DIR/test1.txt"
+check "$expected" "$MH_TEST_DIR/test1.txt"
+
+
# check -help
start_test "-help"
cat >"$expected" <<EOF