(list-holidays): Make Y2 optional.
[emacs.git] / lisp / calendar / cal-dst.el
blobf4512fa12eac31e56fed93f8400de5496423ebc2
1 ;;; cal-dst.el --- calendar functions for daylight saving rules
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Paul Eggert <eggert@twinsun.com>
7 ;; Edward M. Reingold <reingold@cs.uiuc.edu>
8 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Keywords: calendar
10 ;; Human-Keywords: daylight saving time, calendar, diary, holidays
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; This collection of functions implements the features of calendar.el and
32 ;; holiday.el that deal with daylight saving time.
34 ;;; Code:
36 (require 'calendar)
39 (defgroup calendar-dst nil
40 "Options related to Daylight Saving Time."
41 :prefix "calendar-"
42 :group 'calendar)
45 (defcustom calendar-dst-check-each-year-flag t
46 "Non-nil means to check each year for DST transitions as needed.
47 Otherwise assume the next two transitions found after the
48 current date apply to all years. This is faster, but not always
49 correct, since the dates of daylight saving transitions sometimes
50 change."
51 :type 'boolean
52 :version "22.1"
53 :group 'calendar)
55 ;;;###autoload
56 (put 'calendar-daylight-savings-starts 'risky-local-variable t)
57 (defcustom calendar-daylight-savings-starts '(calendar-dst-starts year)
58 "Sexp giving the date on which daylight saving time starts.
59 This is an expression in the variable `year' whose value gives the Gregorian
60 date in the form (month day year) on which daylight saving time starts. It is
61 used to determine the starting date of daylight saving time for the holiday
62 list and for correcting times of day in the solar and lunar calculations.
64 For example, if daylight saving time is mandated to start on October 1,
65 you would set `calendar-daylight-savings-starts' to
67 '(10 1 year)
69 If it starts on the first Sunday in April, you would set it to
71 '(calendar-nth-named-day 1 0 4 year)
73 If the locale never uses daylight saving time, set this to nil."
74 :type 'sexp
75 :group 'calendar-dst)
77 ;;;###autoload
78 (put 'calendar-daylight-savings-ends 'risky-local-variable t)
79 (defcustom calendar-daylight-savings-ends '(calendar-dst-ends year)
80 "Sexp giving the date on which daylight saving time ends.
81 This is an expression in the variable `year' whose value gives the Gregorian
82 date in the form (month day year) on which daylight saving time ends. It is
83 used to determine the starting date of daylight saving time for the holiday
84 list and for correcting times of day in the solar and lunar calculations.
86 For example, if daylight saving time ends on the last Sunday in October:
88 '(calendar-nth-named-day -1 0 10 year)
90 If the locale never uses daylight saving time, set this to nil."
91 :type 'sexp
92 :group 'calendar-dst)
94 ;;; More defcustoms below.
97 (defvar calendar-current-time-zone-cache nil
98 "Cache for result of `calendar-current-time-zone'.")
99 (put 'calendar-current-time-zone-cache 'risky-local-variable t) ; why?
101 (defvar calendar-system-time-basis
102 (calendar-absolute-from-gregorian '(1 1 1970))
103 "Absolute date of starting date of system clock.")
105 (defun calendar-absolute-from-time (x utc-diff)
106 "Absolute local date of time X; local time is UTC-DIFF seconds from UTC.
108 X is (HIGH . LOW) or (HIGH LOW . IGNORED) where HIGH and LOW are the
109 high and low 16 bits, respectively, of the number of seconds since
110 1970-01-01 00:00:00 UTC, ignoring leap seconds.
112 Returns the pair (ABS-DATE . SECONDS) where SECONDS after local midnight on
113 absolute date ABS-DATE is the equivalent moment to X."
114 (let* ((h (car x))
115 (xtail (cdr x))
116 (l (+ utc-diff (if (numberp xtail) xtail (car xtail))))
117 (u (+ (* 512 (mod h 675)) (floor l 128))))
118 ;; Overflow is a terrible thing!
119 (cons (+ calendar-system-time-basis
120 ;; floor((2^16 h +l) / (60*60*24))
121 (* 512 (floor h 675)) (floor u 675))
122 ;; (2^16 h +l) mod (60*60*24)
123 (+ (* (mod u 675) 128) (mod l 128)))))
125 (defun calendar-time-from-absolute (abs-date s)
126 "Time of absolute date ABS-DATE, S seconds after midnight.
128 Returns the list (HIGH LOW) where HIGH and LOW are the high and low
129 16 bits, respectively, of the number of seconds 1970-01-01 00:00:00 UTC,
130 ignoring leap seconds, that is the equivalent moment to S seconds after
131 midnight UTC on absolute date ABS-DATE."
132 (let* ((a (- abs-date calendar-system-time-basis))
133 (u (+ (* 163 (mod a 512)) (floor s 128))))
134 ;; Overflow is a terrible thing!
135 (list
136 ;; floor((60*60*24*a + s) / 2^16)
137 (+ a (* 163 (floor a 512)) (floor u 512))
138 ;; (60*60*24*a + s) mod 2^16
139 (+ (* 128 (mod u 512)) (mod s 128)))))
141 (defun calendar-next-time-zone-transition (time)
142 "Return the time of the next time zone transition after TIME.
143 Both TIME and the result are acceptable arguments to `current-time-zone'.
144 Return nil if no such transition can be found."
145 (let* ((base 65536) ; 2^16 = base of current-time output
146 (quarter-multiple 120) ; approx = (seconds per quarter year) / base
147 (time-zone (current-time-zone time))
148 (time-utc-diff (car time-zone))
150 hi-zone
151 (hi-utc-diff time-utc-diff)
152 (quarters '(2 1 3)))
153 ;; Heuristic: probe the time zone offset in the next three calendar
154 ;; quarters, looking for a time zone offset different from TIME.
155 (while (and quarters (eq time-utc-diff hi-utc-diff))
156 (setq hi (cons (+ (car time) (* (car quarters) quarter-multiple)) 0)
157 hi-zone (current-time-zone hi)
158 hi-utc-diff (car hi-zone)
159 quarters (cdr quarters)))
160 (and
161 time-utc-diff
162 hi-utc-diff
163 (not (eq time-utc-diff hi-utc-diff))
164 ;; Now HI is after the next time zone transition.
165 ;; Set LO to TIME, and then binary search to increase LO and decrease HI
166 ;; until LO is just before and HI is just after the time zone transition.
167 (let* ((tail (cdr time))
168 (lo (cons (car time) (if (numberp tail) tail (car tail))))
169 probe)
170 (while
171 ;; Set PROBE to halfway between LO and HI, rounding down.
172 ;; If PROBE equals LO, we are done.
173 (let* ((lsum (+ (cdr lo) (cdr hi)))
174 (hsum (+ (car lo) (car hi) (/ lsum base)))
175 (hsumodd (logand 1 hsum)))
176 (setq probe (cons (/ (- hsum hsumodd) 2)
177 (/ (+ (* hsumodd base) (% lsum base)) 2)))
178 (not (equal lo probe)))
179 ;; Set either LO or HI to PROBE, depending on probe results.
180 (if (eq (car (current-time-zone probe)) hi-utc-diff)
181 (setq hi probe)
182 (setq lo probe)))
183 hi))))
185 (autoload 'calendar-absolute-from-persian "cal-persia")
187 (defun calendar-time-zone-daylight-rules (abs-date utc-diff)
188 "Return daylight transition rule for ABS-DATE, UTC-DIFF sec offset from UTC.
189 ABS-DATE must specify a day that contains a daylight saving transition.
190 The result has the proper form for `calendar-daylight-savings-starts'."
191 (let* ((date (calendar-gregorian-from-absolute abs-date))
192 (weekday (% abs-date 7))
193 (m (extract-calendar-month date))
194 (d (extract-calendar-day date))
195 (y (extract-calendar-year date))
196 (last (calendar-last-day-of-month m y))
197 (candidate-rules
198 (append
199 ;; Day D of month M.
200 (list (list 'list m d 'year))
201 ;; The first WEEKDAY of month M.
202 (if (< d 8)
203 (list (list 'calendar-nth-named-day 1 weekday m 'year)))
204 ;; The last WEEKDAY of month M.
205 (if (> d (- last 7))
206 (list (list 'calendar-nth-named-day -1 weekday m 'year)))
207 ;; The first WEEKDAY after day J of month M, for D-6 < J <= D.
208 (let (l)
209 (calendar-for-loop j from (max 2 (- d 6)) to (min d (- last 8)) do
210 (setq l
211 (cons
212 (list 'calendar-nth-named-day
213 1 weekday m 'year j)
214 l)))
216 ;; 01-01 and 07-01 for this year's Persian calendar.
217 (if (and (= m 3) (<= 20 d) (<= d 21))
218 '((calendar-gregorian-from-absolute
219 (calendar-absolute-from-persian
220 (list 1 1 (- year 621))))))
221 (if (and (= m 9) (<= 22 d) (<= d 23))
222 '((calendar-gregorian-from-absolute
223 (calendar-absolute-from-persian
224 (list 7 1 (- year 621))))))))
225 (prevday-sec (- -1 utc-diff)) ; last sec of previous local day
226 (year (1+ y)))
227 ;; Scan through the next few years until only one rule remains.
228 (while (let ((rules candidate-rules)
229 new-rules)
230 (dolist (rule rules)
231 (let ((date
232 ;; The following is much faster than
233 ;; (calendar-absolute-from-gregorian (eval rule)).
234 (cond ((eq (car rule) 'calendar-nth-named-day)
235 (eval (cons 'calendar-nth-named-absday
236 (cdr rule))))
237 ((eq (car rule) 'calendar-gregorian-from-absolute)
238 (eval (cadr rule)))
239 (t (calendar-absolute-from-gregorian
240 (eval rule))))))
241 (or (equal
242 (current-time-zone
243 (calendar-time-from-absolute date prevday-sec))
244 (current-time-zone
245 (calendar-time-from-absolute (1+ date) prevday-sec)))
246 (setq new-rules (cons rule new-rules)))))
247 ;; If no rules remain, just use the first candidate rule;
248 ;; it's wrong in general, but it's right for at least one year.
249 (setq candidate-rules (if new-rules (nreverse new-rules)
250 (list (car candidate-rules)))
251 year (1+ year))
252 (cdr candidate-rules)))
253 (car candidate-rules)))
255 ;; TODO it might be better to extract this information directly from
256 ;; the system timezone database. But cross-platform...?
257 ;; See thread
258 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2006-11/msg00060.html
259 (defun calendar-dst-find-data (&optional time)
260 "Find data on the first daylight saving time transitions after TIME.
261 TIME defaults to `current-time'. Return value is as described
262 for `calendar-current-time-zone'."
263 (let* ((t0 (or time (current-time)))
264 (t0-zone (current-time-zone t0))
265 (t0-utc-diff (car t0-zone))
266 (t0-name (cadr t0-zone)))
267 (if (not t0-utc-diff)
268 ;; Little or no time zone information is available.
269 (list nil nil t0-name t0-name nil nil nil nil)
270 (let* ((t1 (calendar-next-time-zone-transition t0))
271 (t2 (and t1 (calendar-next-time-zone-transition t1))))
272 (if (not t2)
273 ;; This locale does not have daylight saving time.
274 (list (/ t0-utc-diff 60) 0 t0-name t0-name nil nil 0 0)
275 ;; Use heuristics to find daylight saving parameters.
276 (let* ((t1-zone (current-time-zone t1))
277 (t1-utc-diff (car t1-zone))
278 (t1-name (cadr t1-zone))
279 (t1-date-sec (calendar-absolute-from-time t1 t0-utc-diff))
280 (t2-date-sec (calendar-absolute-from-time t2 t1-utc-diff))
281 ;; TODO When calendar-dst-check-each-year-flag is non-nil,
282 ;; the rules can be simpler than they currently are.
283 (t1-rules (calendar-time-zone-daylight-rules
284 (car t1-date-sec) t0-utc-diff))
285 (t2-rules (calendar-time-zone-daylight-rules
286 (car t2-date-sec) t1-utc-diff))
287 (t1-time (/ (cdr t1-date-sec) 60))
288 (t2-time (/ (cdr t2-date-sec) 60)))
289 (cons
290 (/ (min t0-utc-diff t1-utc-diff) 60)
291 (cons
292 (/ (abs (- t0-utc-diff t1-utc-diff)) 60)
293 (if (< t0-utc-diff t1-utc-diff)
294 (list t0-name t1-name t1-rules t2-rules t1-time t2-time)
295 (list t1-name t0-name t2-rules t1-rules t2-time t1-time)
296 )))))))))
298 (defvar calendar-dst-transition-cache nil
299 "Internal cal-dst variable storing date of daylight saving time transitions.
300 Value is a list with elements of the form (YEAR START END), where
301 START and END are expressions that when evaluated return the
302 start and end dates (respectively) for DST in YEAR. Used by the
303 function `calendar-dst-find-startend'.")
305 (defun calendar-dst-find-startend (year)
306 "Find the dates in YEAR on which daylight saving time starts and ends.
307 Returns a list (YEAR START END), where START and END are
308 expressions that when evaluated return the start and end dates,
309 respectively. This function first attempts to use pre-calculated
310 data from `calendar-dst-transition-cache', otherwise it calls
311 `calendar-dst-find-data' (and adds the results to the cache).
312 If dates in YEAR cannot be handled by `encode-time' (e.g. if they
313 are too large to be represented as a lisp integer), then rather
314 than an error this function returns the result appropriate for
315 the current year."
316 (let ((e (assoc year calendar-dst-transition-cache))
318 (or e
319 (progn
320 (setq e (calendar-dst-find-data
321 (condition-case nil
322 (encode-time 1 0 0 1 1 year)
323 (error
324 (encode-time 1 0 0 1 1 (nth 5 (decode-time))))))
325 f (nth 4 e)
326 e (list year f (nth 5 e))
327 calendar-dst-transition-cache
328 (append calendar-dst-transition-cache (list e)))
329 e))))
331 (defun calendar-current-time-zone ()
332 "Return UTC difference, dst offset, names and rules for current time zone.
334 Returns (UTC-DIFF DST-OFFSET STD-ZONE DST-ZONE DST-STARTS DST-ENDS
335 DST-STARTS-TIME DST-ENDS-TIME), based on a heuristic probing of what the
336 system knows:
338 UTC-DIFF is an integer specifying the number of minutes difference between
339 standard time in the current time zone and Coordinated Universal Time
340 (Greenwich Mean Time). A negative value means west of Greenwich.
341 DST-OFFSET is an integer giving the daylight saving time offset in minutes.
342 STD-ZONE is a string giving the name of the time zone when no seasonal time
343 adjustment is in effect.
344 DST-ZONE is a string giving the name of the time zone when there is a seasonal
345 time adjustment in effect.
346 DST-STARTS and DST-ENDS are sexps in the variable `year' giving the daylight
347 saving time start and end rules, in the form expected by
348 `calendar-daylight-savings-starts'.
349 DST-STARTS-TIME and DST-ENDS-TIME are integers giving the number of minutes
350 after midnight that daylight saving time starts and ends.
352 If the local area does not use a seasonal time adjustment, STD-ZONE and
353 DST-ZONE are equal, and all the DST-* integer variables are 0.
355 Some operating systems cannot provide all this information to Emacs; in this
356 case, `calendar-current-time-zone' returns a list containing nil for the data
357 it can't find."
358 (unless calendar-current-time-zone-cache
359 (setq calendar-current-time-zone-cache (calendar-dst-find-data))))
362 ;; Following options should be set based on conditions when the code
363 ;; is invoked, so are not suitable for dumping into loaddefs.el. They
364 ;; default to US Eastern time if time zone info is not available.
366 (calendar-current-time-zone)
368 (defcustom calendar-time-zone (or (car calendar-current-time-zone-cache) -300)
369 "Number of minutes difference between local standard time and UTC.
370 For example, -300 for New York City, -480 for Los Angeles."
371 :type 'integer
372 :group 'calendar-dst)
374 (defcustom calendar-daylight-time-offset
375 (or (cadr calendar-current-time-zone-cache) 60)
376 "Number of minutes difference between daylight saving and standard time.
377 If the locale never uses daylight saving time, set this to 0."
378 :type 'integer
379 :group 'calendar-dst)
381 (defcustom calendar-standard-time-zone-name
382 (or (nth 2 calendar-current-time-zone-cache) "EST")
383 "Abbreviated name of standard time zone at `calendar-location-name'.
384 For example, \"EST\" in New York City, \"PST\" for Los Angeles."
385 :type 'string
386 :group 'calendar-dst)
388 (defcustom calendar-daylight-time-zone-name
389 (or (nth 3 calendar-current-time-zone-cache) "EDT")
390 "Abbreviated name of daylight saving time zone at `calendar-location-name'.
391 For example, \"EDT\" in New York City, \"PDT\" for Los Angeles."
392 :type 'string
393 :group 'calendar-dst)
395 (defcustom calendar-daylight-savings-starts-time
396 (or (nth 6 calendar-current-time-zone-cache) 120)
397 "Number of minutes after midnight that daylight saving time starts."
398 :type 'integer
399 :group 'calendar-dst)
401 (defcustom calendar-daylight-savings-ends-time
402 (or (nth 7 calendar-current-time-zone-cache)
403 calendar-daylight-savings-starts-time)
404 "Number of minutes after midnight that daylight saving time ends."
405 :type 'integer
406 :group 'calendar-dst)
409 (defun calendar-dst-starts (year)
410 "Return the date of YEAR on which daylight saving time starts.
411 This function respects the value of `calendar-dst-check-each-year-flag'."
412 (or (let ((expr (if calendar-dst-check-each-year-flag
413 (cadr (calendar-dst-find-startend year))
414 (nth 4 calendar-current-time-zone-cache))))
415 (if expr (eval expr)))
416 ;; New US rules commencing 2007. ftp://elsie.nci.nih.gov/pub/.
417 (and (not (zerop calendar-daylight-time-offset))
418 (calendar-nth-named-day 2 0 3 year))))
420 (defun calendar-dst-ends (year)
421 "Return the date of YEAR on which daylight saving time ends.
422 This function respects the value of `calendar-dst-check-each-year-flag'."
423 (or (let ((expr (if calendar-dst-check-each-year-flag
424 (nth 2 (calendar-dst-find-startend year))
425 (nth 5 calendar-current-time-zone-cache))))
426 (if expr (eval expr)))
427 ;; New US rules commencing 2007. ftp://elsie.nci.nih.gov/pub/.
428 (and (not (zerop calendar-daylight-time-offset))
429 (calendar-nth-named-day 1 0 11 year))))
431 (defun dst-in-effect (date)
432 "True if on absolute DATE daylight saving time is in effect.
433 Fractional part of DATE is local standard time of day."
434 (let* ((year (extract-calendar-year
435 (calendar-gregorian-from-absolute (floor date))))
436 (dst-starts-gregorian (eval calendar-daylight-savings-starts))
437 (dst-ends-gregorian (eval calendar-daylight-savings-ends))
438 (dst-starts (and dst-starts-gregorian
439 (+ (calendar-absolute-from-gregorian
440 dst-starts-gregorian)
441 (/ calendar-daylight-savings-starts-time
442 60.0 24.0))))
443 (dst-ends (and dst-ends-gregorian
444 (+ (calendar-absolute-from-gregorian
445 dst-ends-gregorian)
446 (/ (- calendar-daylight-savings-ends-time
447 calendar-daylight-time-offset)
448 60.0 24.0)))))
449 (and dst-starts dst-ends
450 (if (< dst-starts dst-ends)
451 (and (<= dst-starts date) (< date dst-ends))
452 (or (<= dst-starts date) (< date dst-ends))))))
454 (defun dst-adjust-time (date time &optional style)
455 "Adjust, to account for dst on DATE, decimal fraction standard TIME.
456 Returns a list (date adj-time zone) where `date' and `adj-time' are the values
457 adjusted for `zone'; here `date' is a list (month day year), `adj-time' is a
458 decimal fraction time, and `zone' is a string.
460 Optional parameter STYLE forces the result time to be standard time when its
461 value is 'standard and daylight saving time (if available) when its value is
462 'daylight.
464 Conversion to daylight saving time is done according to
465 `calendar-daylight-savings-starts', `calendar-daylight-savings-ends',
466 `calendar-daylight-savings-starts-time',
467 `calendar-daylight-savings-ends-time', and `calendar-daylight-time-offset'."
469 (let* ((rounded-abs-date (+ (calendar-absolute-from-gregorian date)
470 (/ (round (* 60 time)) 60.0 24.0)))
471 (dst (dst-in-effect rounded-abs-date))
472 (time-zone (if dst
473 calendar-daylight-time-zone-name
474 calendar-standard-time-zone-name))
475 (time (+ rounded-abs-date
476 (if dst (/ calendar-daylight-time-offset 24.0 60.0) 0))))
477 (list (calendar-gregorian-from-absolute (truncate time))
478 (* 24.0 (- time (truncate time)))
479 time-zone)))
481 (provide 'cal-dst)
483 ;; arch-tag: a141d204-213c-4ca5-bdc6-f9df3aa92aad
484 ;;; cal-dst.el ends here