1 ;;; cal-dst.el --- calendar functions for daylight savings rules.
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Paul Eggert <eggert@twinsun.com>
6 ;; Edward M. Reingold <reingold@cs.uiuc.edu>
8 ;; Human-Keywords: daylight savings time, calendar, diary, holidays
10 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; This collection of functions implements the features of calendar.el and
29 ;; holiday.el that deal with daylight savings time.
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
41 (defvar calendar-current-time-zone-cache nil
42 "Cache for result of calendar-current-time-zone.")
44 (defvar calendar-system-time-basis
45 (calendar-absolute-from-gregorian '(1 1 1970))
46 "Absolute date of starting date of system clock.")
48 (defun calendar-absolute-from-time (x utc-diff
)
49 "Absolute local date of time X; local time is UTC-DIFF seconds from UTC.
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.
55 Returns the pair (ABS-DATE . SECONDS) where SECONDS after local midnight on
56 absolute date ABS-DATE is the equivalent moment to 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)))))
68 (defun calendar-time-from-absolute (abs-date s
)
69 "Time of absolute date ABS-DATE, S seconds after midnight.
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!
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)))))
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
))
94 (hi-utc-diff time-utc-diff
)
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
)))
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
))))
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
)
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
))
141 (list (list 'list m d
'year
))
142 ;; The first WEEKDAY of month M.
144 (list (list 'calendar-nth-named-day
1 weekday m
'year
)))
145 ;; The last WEEKDAY of month M.
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.
150 (calendar-for-loop j from
(max 2 (- d
6)) to
(min d
(- last
8)) do
153 (list 'calendar-nth-named-day
1 weekday m
'year j
)
156 (prevday-sec (- -
1 utc-diff
)) ;; last sec of previous local day
158 ;; Scan through the next few years until only one rule remains.
160 (let ((rules candidate-rules
)
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
))))))
176 (calendar-time-from-absolute date prevday-sec
))
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
)))
189 (defun calendar-current-time-zone ()
190 "Return UTC difference, dst offset, names and rules for current time zone.
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
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.
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.
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
217 calendar-current-time-zone-cache
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
))))
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)))
245 (/ (min t0-utc-diff t1-utc-diff
) 60)
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
)
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.
260 (calendar-current-time-zone)
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.")
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.
271 If the locale never uses daylight savings time, set this to 0.")
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.")
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.")
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.
295 For example, if daylight savings time is mandated to start on October 1,
296 you would set `calendar-daylight-savings-starts' to
300 If it starts on the first Sunday in April, you would set it to
302 '(calendar-nth-named-day 1 0 4 year)
304 If the locale never uses daylight savings time, set this to nil.")
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.
318 For example, if daylight savings time ends on the last Sunday in October:
320 '(calendar-nth-named-day -1 0 10 year)
322 If the locale never uses daylight savings time, set this to nil.")
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.")
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.")
333 (defun dst-in-effect (date)
334 "True if on absolute DATE daylight savings time is in effect.
335 Fractional part of DATE is time of day."
336 (let* ((year (extract-calendar-year
337 (calendar-gregorian-from-absolute (floor date
))))
338 (dst-starts (and (eval calendar-daylight-savings-starts
)
339 (+ (calendar-absolute-from-gregorian
340 (eval calendar-daylight-savings-starts
))
341 (/ calendar-daylight-savings-starts-time
343 (dst-ends (and (eval calendar-daylight-savings-ends
)
344 (+ (calendar-absolute-from-gregorian
345 (eval calendar-daylight-savings-ends
))
346 (/ (- calendar-daylight-savings-ends-time
347 calendar-daylight-time-offset
)
349 (and (and dst-starts dst-ends
350 (or (and (< dst-starts dst-ends
);; northern hemi.
351 (<= dst-starts date
) (< date dst-ends
))
352 (and (< dst-ends dst-starts
);; southern hemi.
353 (<= dst-starts date
) (< date dst-ends
))
354 (and dst-starts
(not dst-ends
) (<= dst-starts date
))
355 (and dst-ends
(not dst-starts
) (< date dst-ends
)))))))
357 (defun dst-adjust-time (date time
&optional style
)
358 "Adjust, to account for dst on DATE, decimal fraction standard TIME.
359 Returns a list (date adj-time zone) where `date' and `adj-time' are the values
360 adjusted for `zone'; here `date' is a list (month day year), `adj-time' is a
361 decimal fraction time, and `zone' is a string.
363 Optional parameter STYLE forces the result time to be standard time when its
364 value is 'standard and daylight savings time (if available) when its value is
367 Conversion to daylight savings time is done according to
368 `calendar-daylight-savings-starts', `calendar-daylight-savings-ends',
369 `calendar-daylight-savings-starts-time',
370 `calendar-daylight-savings-ends-time', and
371 `calendar-daylight-savings-offset'."
373 (let* ((rounded-abs-date (+ (calendar-absolute-from-gregorian date
)
374 (/ (round (* 60 time
)) 60.0 24.0)))
375 (dst (dst-in-effect rounded-abs-date
))
377 calendar-daylight-time-zone-name
378 calendar-standard-time-zone-name
))
379 (time (+ rounded-abs-date
380 (if dst
(/ calendar-daylight-time-offset
24.0 60.0) 0))))
381 (list (calendar-gregorian-from-absolute (truncate time
))
382 (* 24.0 (- time
(truncate time
)))
387 ;;; cal-dst.el ends here