]> code.delx.au - gnu-emacs/blob - lisp/calendar/cal-dst.el
Fixed dst-in-effect for southern hemisphere. Also made doc string clearer.
[gnu-emacs] / lisp / calendar / cal-dst.el
1 ;;; cal-dst.el --- calendar functions for daylight savings rules.
2
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: Paul Eggert <eggert@twinsun.com>
6 ;; Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Keywords: calendar
8 ;; Human-Keywords: daylight savings time, calendar, diary, holidays
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; Commentary:
27
28 ;; This collection of functions implements the features of calendar.el and
29 ;; holiday.el that deal with daylight savings time.
30
31 ;; Comments, corrections, and improvements should be sent to
32 ;; Edward M. Reingold Department of Computer Science
33 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
34 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
35 ;; Urbana, Illinois 61801
36
37 ;;; Code:
38
39 (require 'calendar)
40
41 (defvar calendar-current-time-zone-cache nil
42 "Cache for result of calendar-current-time-zone.")
43
44 (defvar calendar-system-time-basis
45 (calendar-absolute-from-gregorian '(1 1 1970))
46 "Absolute date of starting date of system clock.")
47
48 (defun calendar-absolute-from-time (x utc-diff)
49 "Absolute local date of time X; local time is UTC-DIFF seconds from UTC.
50
51 X is (HIGH . LOW) or (HIGH LOW . IGNORED) where HIGH and LOW are the
52 high and low 16 bits, respectively, of the number of seconds since
53 1970-01-01 00:00:00 UTC, ignoring leap seconds.
54
55 Returns the pair (ABS-DATE . SECONDS) where SECONDS after local midnight on
56 absolute date ABS-DATE is the equivalent moment to X."
57 (let* ((h (car x))
58 (xtail (cdr x))
59 (l (+ utc-diff (if (numberp xtail) xtail (car xtail))))
60 (u (+ (* 512 (mod h 675)) (floor l 128))))
61 ;; Overflow is a terrible thing!
62 (cons (+ calendar-system-time-basis
63 ;; floor((2^16 h +l) / (60*60*24))
64 (* 512 (floor h 675)) (floor u 675))
65 ;; (2^16 h +l) mod (60*60*24)
66 (+ (* (mod u 675) 128) (mod l 128)))))
67
68 (defun calendar-time-from-absolute (abs-date s)
69 "Time of absolute date ABS-DATE, S seconds after midnight.
70
71 Returns the pair (HIGH . LOW) where HIGH and LOW are the high and low
72 16 bits, respectively, of the number of seconds 1970-01-01 00:00:00 UTC,
73 ignoring leap seconds, that is the equivalent moment to S seconds after
74 midnight UTC on absolute date ABS-DATE."
75 (let* ((a (- abs-date calendar-system-time-basis))
76 (u (+ (* 163 (mod a 512)) (floor s 128))))
77 ;; Overflow is a terrible thing!
78 (cons
79 ;; floor((60*60*24*a + s) / 2^16)
80 (+ a (* 163 (floor a 512)) (floor u 512))
81 ;; (60*60*24*a + s) mod 2^16
82 (+ (* 128 (mod u 512)) (mod s 128)))))
83
84 (defun calendar-next-time-zone-transition (time)
85 "Return the time of the next time zone transition after TIME.
86 Both TIME and the result are acceptable arguments to current-time-zone.
87 Return nil if no such transition can be found."
88 (let* ((base 65536);; 2^16 = base of current-time output
89 (quarter-multiple 120);; approx = (seconds per quarter year) / base
90 (time-zone (current-time-zone time))
91 (time-utc-diff (car time-zone))
92 hi
93 hi-zone
94 (hi-utc-diff time-utc-diff)
95 (quarters '(2 1 3)))
96 ;; Heuristic: probe the time zone offset in the next three calendar
97 ;; quarters, looking for a time zone offset different from TIME.
98 (while (and quarters (eq time-utc-diff hi-utc-diff))
99 (setq hi (cons (+ (car time) (* (car quarters) quarter-multiple)) 0))
100 (setq hi-zone (current-time-zone hi))
101 (setq hi-utc-diff (car hi-zone))
102 (setq quarters (cdr quarters)))
103 (and
104 time-utc-diff
105 hi-utc-diff
106 (not (eq time-utc-diff hi-utc-diff))
107 ;; Now HI is after the next time zone transition.
108 ;; Set LO to TIME, and then binary search to increase LO and decrease HI
109 ;; until LO is just before and HI is just after the time zone transition.
110 (let* ((tail (cdr time))
111 (lo (cons (car time) (if (numberp tail) tail (car tail))))
112 probe)
113 (while
114 ;; Set PROBE to halfway between LO and HI, rounding down.
115 ;; If PROBE equals LO, we are done.
116 (let* ((lsum (+ (cdr lo) (cdr hi)))
117 (hsum (+ (car lo) (car hi) (/ lsum base)))
118 (hsumodd (logand 1 hsum)))
119 (setq probe (cons (/ (- hsum hsumodd) 2)
120 (/ (+ (* hsumodd base) (% lsum base)) 2)))
121 (not (equal lo probe)))
122 ;; Set either LO or HI to PROBE, depending on probe results.
123 (if (eq (car (current-time-zone probe)) hi-utc-diff)
124 (setq hi probe)
125 (setq lo probe)))
126 hi))))
127
128 (defun calendar-time-zone-daylight-rules (abs-date utc-diff)
129 "Return daylight transition rule for ABS-DATE, UTC-DIFF sec offset from UTC.
130 ABS-DIFF must specify a day that contains a daylight savings transition.
131 The result has the proper form for calendar-daylight-savings-starts'."
132 (let* ((date (calendar-gregorian-from-absolute abs-date))
133 (weekday (% abs-date 7))
134 (m (extract-calendar-month date))
135 (d (extract-calendar-day date))
136 (y (extract-calendar-year date))
137 (last (calendar-last-day-of-month m y))
138 (candidate-rules
139 (append
140 ;; Day D of month M.
141 (list (list 'list m d 'year))
142 ;; The first WEEKDAY of month M.
143 (if (< d 8)
144 (list (list 'calendar-nth-named-day 1 weekday m 'year)))
145 ;; The last WEEKDAY of month M.
146 (if (> d (- last 7))
147 (list (list 'calendar-nth-named-day -1 weekday m 'year)))
148 ;; The first WEEKDAY after day J of month M, for D-6 < J <= D.
149 (let (l)
150 (calendar-for-loop j from (max 2 (- d 6)) to (min d (- last 8)) do
151 (setq l
152 (cons
153 (list 'calendar-nth-named-day 1 weekday m 'year j)
154 l)))
155 l)))
156 (prevday-sec (- -1 utc-diff)) ;; last sec of previous local day
157 (year (1+ y)))
158 ;; Scan through the next few years until only one rule remains.
159 (while
160 (let ((rules candidate-rules)
161 new-rules)
162 (while
163 (let*
164 ((rule (car rules))
165 (date
166 ;; The following is much faster than
167 ;; (calendar-absolute-from-gregorian (eval rule)).
168 (cond ((eq (car rule) 'calendar-nth-named-day)
169 (eval (cons 'calendar-nth-named-absday (cdr rule))))
170 ((eq (car rule) 'calendar-gregorian-from-absolute)
171 (eval (car (cdr rule))))
172 (t (let ((g (eval rule)))
173 (calendar-absolute-from-gregorian g))))))
174 (or (equal
175 (current-time-zone
176 (calendar-time-from-absolute date prevday-sec))
177 (current-time-zone
178 (calendar-time-from-absolute (1+ date) prevday-sec)))
179 (setq new-rules (cons rule new-rules)))
180 (setq rules (cdr rules))))
181 ;; If no rules remain, just use the first candidate rule;
182 ;; it's wrong in general, but it's right for at least one year.
183 (setq candidate-rules (if new-rules (nreverse new-rules)
184 (list (car candidate-rules))))
185 (setq year (1+ year))
186 (cdr candidate-rules)))
187 (car candidate-rules)))
188
189 (defun calendar-current-time-zone ()
190 "Return UTC difference, dst offset, names and rules for current time zone.
191
192 Returns (UTC-DIFF DST-OFFSET STD-ZONE DST-ZONE DST-STARTS DST-ENDS
193 DST-STARTS-TIME DST-ENDS-TIME), based on a heuristic probing of what the
194 system knows:
195
196 UTC-DIFF is an integer specifying the number of minutes difference between
197 standard time in the current time zone and Coordinated Universal Time
198 (Greenwich Mean Time). A negative value means west of Greenwich.
199 DST-OFFSET is an integer giving the daylight savings time offset in minutes.
200 STD-ZONE is a string giving the name of the time zone when no seasonal time
201 adjustment is in effect.
202 DST-ZONE is a string giving the name of the time zone when there is a seasonal
203 time adjustment in effect.
204 DST-STARTS and DST-ENDS are sexps in the variable `year' giving the daylight
205 savings time start and end rules, in the form expected by
206 `calendar-daylight-savings-starts'.
207 DST-STARTS-TIME and DST-ENDS-TIME are integers giving the number of minutes
208 after midnight that daylight savings time starts and ends.
209
210 If the local area does not use a seasonal time adjustment, STD-ZONE and
211 DST-ZONE are equal, and all the DST-* integer variables are 0.
212
213 Some operating systems cannot provide all this information to Emacs; in this
214 case, `calendar-current-time-zone' returns a list containing nil for the data
215 it can't find."
216 (or
217 calendar-current-time-zone-cache
218 (setq
219 calendar-current-time-zone-cache
220 (let* ((t0 (current-time))
221 (t0-zone (current-time-zone t0))
222 (t0-utc-diff (car t0-zone))
223 (t0-name (car (cdr t0-zone))))
224 (if (not t0-utc-diff)
225 ;; Little or no time zone information is available.
226 (list nil nil t0-name t0-name nil nil nil nil)
227 (let* ((t1 (calendar-next-time-zone-transition t0))
228 (t2 (and t1 (calendar-next-time-zone-transition t1))))
229 (if (not t2)
230 ;; This locale does not have daylight savings time.
231 (list (/ t0-utc-diff 60) 0 t0-name t0-name nil nil 0 0)
232 ;; Use heuristics to find daylight savings parameters.
233 (let* ((t1-zone (current-time-zone t1))
234 (t1-utc-diff (car t1-zone))
235 (t1-name (car (cdr t1-zone)))
236 (t1-date-sec (calendar-absolute-from-time t1 t0-utc-diff))
237 (t2-date-sec (calendar-absolute-from-time t2 t1-utc-diff))
238 (t1-rules (calendar-time-zone-daylight-rules
239 (car t1-date-sec) t0-utc-diff))
240 (t2-rules (calendar-time-zone-daylight-rules
241 (car t2-date-sec) t1-utc-diff))
242 (t1-time (/ (cdr t1-date-sec) 60))
243 (t2-time (/ (cdr t2-date-sec) 60)))
244 (cons
245 (/ (min t0-utc-diff t1-utc-diff) 60)
246 (cons
247 (/ (abs (- t0-utc-diff t1-utc-diff)) 60)
248 (if (< t0-utc-diff t1-utc-diff)
249 (list t0-name t1-name t1-rules t2-rules t1-time t2-time)
250 (list t1-name t0-name t2-rules t1-rules t2-time t1-time)
251 )))))))))))
252
253 ;;; The following eight defvars relating to daylight savings time should NOT be
254 ;;; marked to go into loaddefs.el where they would be evaluated when Emacs is
255 ;;; dumped. These variables' appropriate values depend on the conditions under
256 ;;; which the code is INVOKED; so it's inappropriate to initialize them when
257 ;;; Emacs is dumped---they should be initialized when calendar.el is loaded.
258 ;;; They default to US Eastern time if time zone info is not available.
259
260 (calendar-current-time-zone)
261
262 (defvar calendar-time-zone (or (car calendar-current-time-zone-cache) -300)
263 "*Number of minutes difference between local standard time at
264 `calendar-location-name' and Coordinated Universal (Greenwich) Time. For
265 example, -300 for New York City, -480 for Los Angeles.")
266
267 (defvar calendar-daylight-time-offset
268 (or (car (cdr calendar-current-time-zone-cache)) 60)
269 "*Number of minutes difference between daylight savings and standard time.
270
271 If the locale never uses daylight savings time, set this to 0.")
272
273 (defvar calendar-standard-time-zone-name
274 (or (car (nthcdr 2 calendar-current-time-zone-cache)) "EST")
275 "*Abbreviated name of standard time zone at `calendar-location-name'.
276 For example, \"EST\" in New York City, \"PST\" for Los Angeles.")
277
278 (defvar calendar-daylight-time-zone-name
279 (or (car (nthcdr 3 calendar-current-time-zone-cache)) "EDT")
280 "*Abbreviated name of daylight-savings time zone at `calendar-location-name'.
281 For example, \"EDT\" in New York City, \"PDT\" for Los Angeles.")
282
283 ;;;###autoload
284 (put 'calendar-daylight-savings-starts 'risky-local-variable t)
285 (defvar calendar-daylight-savings-starts
286 (or (car (nthcdr 4 calendar-current-time-zone-cache))
287 (and (not (zerop calendar-daylight-time-offset))
288 '(calendar-nth-named-day 1 0 4 year)))
289 "*Sexp giving the date on which daylight savings time starts.
290 This is an expression in the variable `year' whose value gives the Gregorian
291 date in the form (month day year) on which daylight savings time starts. It is
292 used to determine the starting date of daylight savings time for the holiday
293 list and for correcting times of day in the solar and lunar calculations.
294
295 For example, if daylight savings time is mandated to start on October 1,
296 you would set `calendar-daylight-savings-starts' to
297
298 '(10 1 year)
299
300 If it starts on the first Sunday in April, you would set it to
301
302 '(calendar-nth-named-day 1 0 4 year)
303
304 If the locale never uses daylight savings time, set this to nil.")
305
306 ;;;###autoload
307 (put 'calendar-daylight-savings-ends 'risky-local-variable t)
308 (defvar calendar-daylight-savings-ends
309 (or (car (nthcdr 5 calendar-current-time-zone-cache))
310 (and (not (zerop calendar-daylight-time-offset))
311 '(calendar-nth-named-day -1 0 10 year)))
312 "*Sexp giving the date on which daylight savings time ends.
313 This is an expression in the variable `year' whose value gives the Gregorian
314 date in the form (month day year) on which daylight savings time ends. It is
315 used to determine the starting date of daylight savings time for the holiday
316 list and for correcting times of day in the solar and lunar calculations.
317
318 For example, if daylight savings time ends on the last Sunday in October:
319
320 '(calendar-nth-named-day -1 0 10 year)
321
322 If the locale never uses daylight savings time, set this to nil.")
323
324 (defvar calendar-daylight-savings-starts-time
325 (or (car (nthcdr 6 calendar-current-time-zone-cache)) 120)
326 "*Number of minutes after midnight that daylight savings time starts.")
327
328 (defvar calendar-daylight-savings-ends-time
329 (or (car (nthcdr 7 calendar-current-time-zone-cache))
330 calendar-daylight-savings-starts-time)
331 "*Number of minutes after midnight that daylight savings time ends.")
332
333 (defun dst-in-effect (date)
334 "True if on absolute DATE daylight savings time is in effect.
335 Fractional part of DATE is local standard time of day."
336 (let* ((year (extract-calendar-year
337 (calendar-gregorian-from-absolute (floor date))))
338 (dst-starts-gregorian (eval calendar-daylight-savings-starts))
339 (dst-ends-gregorian (eval calendar-daylight-savings-ends))
340 (dst-starts (and dst-starts-gregorian
341 (+ (calendar-absolute-from-gregorian
342 dst-starts-gregorian)
343 (/ calendar-daylight-savings-starts-time
344 60.0 24.0))))
345 (dst-ends (and dst-ends-gregorian
346 (+ (calendar-absolute-from-gregorian
347 dst-ends-gregorian)
348 (/ (- calendar-daylight-savings-ends-time
349 calendar-daylight-time-offset)
350 60.0 24.0)))))
351 (and dst-starts dst-ends
352 (if (< dst-starts dst-ends)
353 (and (<= dst-starts date) (< date dst-ends))
354 (or (<= dst-starts date) (< date dst-ends))))))
355
356 (defun dst-adjust-time (date time &optional style)
357 "Adjust, to account for dst on DATE, decimal fraction standard TIME.
358 Returns a list (date adj-time zone) where `date' and `adj-time' are the values
359 adjusted for `zone'; here `date' is a list (month day year), `adj-time' is a
360 decimal fraction time, and `zone' is a string.
361
362 Optional parameter STYLE forces the result time to be standard time when its
363 value is 'standard and daylight savings time (if available) when its value is
364 'daylight.
365
366 Conversion to daylight savings time is done according to
367 `calendar-daylight-savings-starts', `calendar-daylight-savings-ends',
368 `calendar-daylight-savings-starts-time',
369 `calendar-daylight-savings-ends-time', and
370 `calendar-daylight-savings-offset'."
371
372 (let* ((rounded-abs-date (+ (calendar-absolute-from-gregorian date)
373 (/ (round (* 60 time)) 60.0 24.0)))
374 (dst (dst-in-effect rounded-abs-date))
375 (time-zone (if dst
376 calendar-daylight-time-zone-name
377 calendar-standard-time-zone-name))
378 (time (+ rounded-abs-date
379 (if dst (/ calendar-daylight-time-offset 24.0 60.0) 0))))
380 (list (calendar-gregorian-from-absolute (truncate time))
381 (* 24.0 (- time (truncate time)))
382 time-zone)))
383
384 (provide 'cal-dst)
385
386 ;;; cal-dst.el ends here