emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Timezone handling problem in icalendar.el/icalendar-import-*


From: Christian
Subject: Timezone handling problem in icalendar.el/icalendar-import-*
Date: Wed, 16 Dec 2009 14:39:20 +0100
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.1.50 (gnu/linux)

I have detected a problem with timezone handling in icalendar.el;
unfortunately I know far too little about timezones to be able to say
what the correct fix is.

The problem was discovered in trying to use icalendar to parse some
Microsoft Exchange 2007 appointments on a SuSE 10.1 Linux system. I am
using emacs version "23.1.50.1" (from CVS) and the icalendar distributed
with that.

The appointments are recorded in timezone GMT+2 whereas I am in timezone
GMT+1. But when extracting the appointment using `icalendar-import-buffer',
all times where an hour off. Basically, it was as if the appointment was
registered in GMT rather than GMT+2.

After a lot of huffing and puffing, I narrowed it down to the following
piece of code:

    (format-time-string  "%Y-%m-%dT%T%z" (encode-time 0 30 11 16 12 2009 
"(STD?)-02:00(DST?)-03:00,M3.-1.0/03:00:00,M10.-1.0/04:00:00"))
    ;=> "2009-12-16T09:30:00+0100"

When evaluated, this returns a time of 09:30 but it should have reported
10:30. Checking the man page for `tzset', however, I read the following:

       The second format is used when there is daylight saving time:

              std offset dst [offset],start[/time],end[/time]
       ...
              Mm.w.d This  specifies  day  d (0 <= d <= 6) of week w (1 <= w <= 
5) of
              month m (1 <= m <= 12).  Week 1 is the first week in which day d
              occurs and week 5 is the last week in which day d occurs.  Day 0
              is a Sunday.

and sure enough, if I change the week specification from "-1" to "5" as in:

    (format-time-string  "%Y-%m-%dT%T%z" (encode-time 0 30 11 16 12 2009 
"(STD?)-02:00(DST?)-03:00,M3.5.0/03:00:00,M10.5.0/04:00:00"))
    ;=> "2009-12-16T10:30:00+0100"

I now get the correct start time of 10:30. In other words, at least on
this particular Linux system, a week specification for DST start/end of
"-1" is not supported, making `encode-time' falling back to GMT. Instead
one should use "5" to indicate the last week of the month. I have no
idea whether this is a bug in icalendar, SuSE 10.1 or Linux as such.

One fix (but I doubt it is the correct one) is to change the function
`icalendar--convert-tz-offset' into the following (one `if' form has
been inserted following the comment of "FIX"):

    (defun icalendar--convert-tz-offset (alist dst-p)
      "Return a cons of two strings representing a timezone start.
    ALIST is an alist entry from a VTIMEZONE, like STANDARD.
    DST-P is non-nil if this is for daylight savings time.
    The strings are suitable for assembling into a TZ variable."
      (let ((offset (car (cddr (assq 'TZOFFSETTO alist))))
            (rrule-value (car (cddr (assq 'RRULE alist))))
            (dtstart (car (cddr (assq 'DTSTART alist)))))
        ;; FIXME: for now we only handle RRULE and not RDATE here.
        (when (and offset rrule-value dtstart)
          (let* ((rrule (icalendar--split-value rrule-value))
                 (freq (cadr (assq 'FREQ rrule)))
                 (bymonth (cadr (assq 'BYMONTH rrule)))
                 (byday (cadr (assq 'BYDAY rrule))))
            ;; FIXME: we don't correctly handle WKST here.
            (if (and (string= freq "YEARLY") bymonth)
                (cons
                 (concat
                  ;; Fake a name.
                  (if dst-p "(DST?)" "(STD?)")
                  ;; For TZ, OFFSET is added to the local time.  So,
                  ;; invert the values.
                  (if (eq (aref offset 0) ?-) "+" "-")
                  (substring offset 1 3)
                  ":"
                  (substring offset 3 5))
                 ;; The start time.
                 (let* ((day (icalendar--get-weekday-number (substring byday 
-2)))
                        (week (if (eq day -1)
                                  byday
                                (substring byday 0 -2))))
                   ;; FIX for bad week spec
                   (if (equal week "-1")
                       (setq week "5"))
                   (concat "M" bymonth "." week "." (if (eq day -1) "0"
                                                      (int-to-string day))
                           ;; Start time.
                           "/"
                           (substring dtstart -6 -4)
                           ":"
                           (substring dtstart -4 -2)
                           ":"
                           (substring dtstart -2)))))))))


------------------------+-----------------------------------------------------
Christian Lynbech       | christian #\@ defun #\. dk
------------------------+-----------------------------------------------------
Hit the philistines three times over the head with the Elisp reference manual.
                                        - address@hidden (Michael A. Petonic)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]