unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Timezone handling problem in icalendar.el/icalendar-import-*
@ 2009-12-16 13:39 Christian
  2009-12-16 14:18 ` Andreas Schwab
  2009-12-16 20:40 ` Ulf Jasper
  0 siblings, 2 replies; 10+ messages in thread
From: Christian @ 2009-12-16 13:39 UTC (permalink / raw)
  To: Emacs development discussions

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.
                                        - petonic@hal.com (Michael A. Petonic)




^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2009-12-19 12:16 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-16 13:39 Timezone handling problem in icalendar.el/icalendar-import-* Christian
2009-12-16 14:18 ` Andreas Schwab
2009-12-16 18:38   ` Christian Lynbech
2009-12-16 20:40 ` Ulf Jasper
2009-12-17 10:09   ` Christian
2009-12-17 20:57     ` Ulf Jasper
2009-12-18  7:47       ` Christian
2009-12-18 19:38         ` Ulf Jasper
2009-12-18 22:40           ` Christian Lynbech
2009-12-19 12:16             ` Ulf Jasper

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).