1 ;;; cal-dst.el --- calendar functions for daylight savings rules
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 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>
10 ;; Human-Keywords: daylight savings 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 2, or (at your option)
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.
31 ;; This collection of functions implements the features of calendar.el and
32 ;; holiday.el that deal with daylight savings time.
34 ;; Comments, corrections, and improvements should be sent to
35 ;; Edward M. Reingold Department of Computer Science
36 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
37 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
38 ;; Urbana, Illinois 61801
45 (defvar calendar-current-time-zone-cache nil
46 "Cache for result of calendar-current-time-zone.")
48 (defvar calendar-system-time-basis
49 (calendar-absolute-from-gregorian '(1 1 1970))
50 "Absolute date of starting date of system clock.")
52 (defun calendar-absolute-from-time (x utc-diff
)
53 "Absolute local date of time X; local time is UTC-DIFF seconds from UTC.
55 X is (HIGH . LOW) or (HIGH LOW . IGNORED) where HIGH and LOW are the
56 high and low 16 bits, respectively, of the number of seconds since
57 1970-01-01 00:00:00 UTC, ignoring leap seconds.
59 Returns the pair (ABS-DATE . SECONDS) where SECONDS after local midnight on
60 absolute date ABS-DATE is the equivalent moment to X."
63 (l (+ utc-diff
(if (numberp xtail
) xtail
(car xtail
))))
64 (u (+ (* 512 (mod h
675)) (floor l
128))))
65 ;; Overflow is a terrible thing!
66 (cons (+ calendar-system-time-basis
67 ;; floor((2^16 h +l) / (60*60*24))
68 (* 512 (floor h
675)) (floor u
675))
69 ;; (2^16 h +l) mod (60*60*24)
70 (+ (* (mod u
675) 128) (mod l
128)))))
72 (defun calendar-time-from-absolute (abs-date s
)
73 "Time of absolute date ABS-DATE, S seconds after midnight.
75 Returns the list (HIGH LOW) where HIGH and LOW are the high and low
76 16 bits, respectively, of the number of seconds 1970-01-01 00:00:00 UTC,
77 ignoring leap seconds, that is the equivalent moment to S seconds after
78 midnight UTC on absolute date ABS-DATE."
79 (let* ((a (- abs-date calendar-system-time-basis
))
80 (u (+ (* 163 (mod a
512)) (floor s
128))))
81 ;; Overflow is a terrible thing!
83 ;; floor((60*60*24*a + s) / 2^16)
84 (+ a
(* 163 (floor a
512)) (floor u
512))
85 ;; (60*60*24*a + s) mod 2^16
86 (+ (* 128 (mod u
512)) (mod s
128)))))
88 (defun calendar-next-time-zone-transition (time)
89 "Return the time of the next time zone transition after TIME.
90 Both TIME and the result are acceptable arguments to current-time-zone.
91 Return nil if no such transition can be found."
92 (let* ((base 65536);; 2^16 = base of current-time output
93 (quarter-multiple 120);; approx = (seconds per quarter year) / base
94 (time-zone (current-time-zone time
))
95 (time-utc-diff (car time-zone
))
98 (hi-utc-diff time-utc-diff
)
100 ;; Heuristic: probe the time zone offset in the next three calendar
101 ;; quarters, looking for a time zone offset different from TIME.
102 (while (and quarters
(eq time-utc-diff hi-utc-diff
))
103 (setq hi
(cons (+ (car time
) (* (car quarters
) quarter-multiple
)) 0))
104 (setq hi-zone
(current-time-zone hi
))
105 (setq hi-utc-diff
(car hi-zone
))
106 (setq quarters
(cdr quarters
)))
110 (not (eq time-utc-diff hi-utc-diff
))
111 ;; Now HI is after the next time zone transition.
112 ;; Set LO to TIME, and then binary search to increase LO and decrease HI
113 ;; until LO is just before and HI is just after the time zone transition.
114 (let* ((tail (cdr time
))
115 (lo (cons (car time
) (if (numberp tail
) tail
(car tail
))))
118 ;; Set PROBE to halfway between LO and HI, rounding down.
119 ;; If PROBE equals LO, we are done.
120 (let* ((lsum (+ (cdr lo
) (cdr hi
)))
121 (hsum (+ (car lo
) (car hi
) (/ lsum base
)))
122 (hsumodd (logand 1 hsum
)))
123 (setq probe
(cons (/ (- hsum hsumodd
) 2)
124 (/ (+ (* hsumodd base
) (% lsum base
)) 2)))
125 (not (equal lo probe
)))
126 ;; Set either LO or HI to PROBE, depending on probe results.
127 (if (eq (car (current-time-zone probe
)) hi-utc-diff
)
132 (defun calendar-time-zone-daylight-rules (abs-date utc-diff
)
133 "Return daylight transition rule for ABS-DATE, UTC-DIFF sec offset from UTC.
134 ABS-DATE must specify a day that contains a daylight savings transition.
135 The result has the proper form for calendar-daylight-savings-starts'."
136 (let* ((date (calendar-gregorian-from-absolute abs-date
))
137 (weekday (% abs-date
7))
138 (m (extract-calendar-month date
))
139 (d (extract-calendar-day date
))
140 (y (extract-calendar-year date
))
141 (last (calendar-last-day-of-month m y
))
145 (list (list 'list m d
'year
))
146 ;; The first WEEKDAY of month M.
148 (list (list 'calendar-nth-named-day
1 weekday m
'year
)))
149 ;; The last WEEKDAY of month M.
151 (list (list 'calendar-nth-named-day -
1 weekday m
'year
)))
152 ;; The first WEEKDAY after day J of month M, for D-6 < J <= D.
154 (calendar-for-loop j from
(max 2 (- d
6)) to
(min d
(- last
8)) do
157 (list 'calendar-nth-named-day
1 weekday m
'year j
)
160 ;; 01-01 and 07-01 for this year's Persian calendar.
161 (if (and (= m
3) (<= 20 d
) (<= d
21))
162 '((calendar-gregorian-from-absolute
163 (calendar-absolute-from-persian
164 (list 1 1 (- year
621))))))
165 (if (and (= m
9) (<= 22 d
) (<= d
23))
166 '((calendar-gregorian-from-absolute
167 (calendar-absolute-from-persian
168 (list 7 1 (- year
621))))))))
169 (prevday-sec (- -
1 utc-diff
)) ;; last sec of previous local day
171 ;; Scan through the next few years until only one rule remains.
173 (let ((rules candidate-rules
)
179 ;; The following is much faster than
180 ;; (calendar-absolute-from-gregorian (eval rule)).
181 (cond ((eq (car rule
) 'calendar-nth-named-day
)
182 (eval (cons 'calendar-nth-named-absday
(cdr rule
))))
183 ((eq (car rule
) 'calendar-gregorian-from-absolute
)
184 (eval (car (cdr rule
))))
185 (t (let ((g (eval rule
)))
186 (calendar-absolute-from-gregorian g
))))))
189 (calendar-time-from-absolute date prevday-sec
))
191 (calendar-time-from-absolute (1+ date
) prevday-sec
)))
192 (setq new-rules
(cons rule new-rules
)))
193 (setq rules
(cdr rules
))))
194 ;; If no rules remain, just use the first candidate rule;
195 ;; it's wrong in general, but it's right for at least one year.
196 (setq candidate-rules
(if new-rules
(nreverse new-rules
)
197 (list (car candidate-rules
))))
198 (setq year
(1+ year
))
199 (cdr candidate-rules
)))
200 (car candidate-rules
)))
202 (defun calendar-current-time-zone ()
203 "Return UTC difference, dst offset, names and rules for current time zone.
205 Returns (UTC-DIFF DST-OFFSET STD-ZONE DST-ZONE DST-STARTS DST-ENDS
206 DST-STARTS-TIME DST-ENDS-TIME), based on a heuristic probing of what the
209 UTC-DIFF is an integer specifying the number of minutes difference between
210 standard time in the current time zone and Coordinated Universal Time
211 (Greenwich Mean Time). A negative value means west of Greenwich.
212 DST-OFFSET is an integer giving the daylight savings time offset in minutes.
213 STD-ZONE is a string giving the name of the time zone when no seasonal time
214 adjustment is in effect.
215 DST-ZONE is a string giving the name of the time zone when there is a seasonal
216 time adjustment in effect.
217 DST-STARTS and DST-ENDS are sexps in the variable `year' giving the daylight
218 savings time start and end rules, in the form expected by
219 `calendar-daylight-savings-starts'.
220 DST-STARTS-TIME and DST-ENDS-TIME are integers giving the number of minutes
221 after midnight that daylight savings time starts and ends.
223 If the local area does not use a seasonal time adjustment, STD-ZONE and
224 DST-ZONE are equal, and all the DST-* integer variables are 0.
226 Some operating systems cannot provide all this information to Emacs; in this
227 case, `calendar-current-time-zone' returns a list containing nil for the data
230 calendar-current-time-zone-cache
232 calendar-current-time-zone-cache
233 (let* ((t0 (current-time))
234 (t0-zone (current-time-zone t0
))
235 (t0-utc-diff (car t0-zone
))
236 (t0-name (car (cdr t0-zone
))))
237 (if (not t0-utc-diff
)
238 ;; Little or no time zone information is available.
239 (list nil nil t0-name t0-name nil nil nil nil
)
240 (let* ((t1 (calendar-next-time-zone-transition t0
))
241 (t2 (and t1
(calendar-next-time-zone-transition t1
))))
243 ;; This locale does not have daylight savings time.
244 (list (/ t0-utc-diff
60) 0 t0-name t0-name nil nil
0 0)
245 ;; Use heuristics to find daylight savings parameters.
246 (let* ((t1-zone (current-time-zone t1
))
247 (t1-utc-diff (car t1-zone
))
248 (t1-name (car (cdr t1-zone
)))
249 (t1-date-sec (calendar-absolute-from-time t1 t0-utc-diff
))
250 (t2-date-sec (calendar-absolute-from-time t2 t1-utc-diff
))
251 (t1-rules (calendar-time-zone-daylight-rules
252 (car t1-date-sec
) t0-utc-diff
))
253 (t2-rules (calendar-time-zone-daylight-rules
254 (car t2-date-sec
) t1-utc-diff
))
255 (t1-time (/ (cdr t1-date-sec
) 60))
256 (t2-time (/ (cdr t2-date-sec
) 60)))
258 (/ (min t0-utc-diff t1-utc-diff
) 60)
260 (/ (abs (- t0-utc-diff t1-utc-diff
)) 60)
261 (if (< t0-utc-diff t1-utc-diff
)
262 (list t0-name t1-name t1-rules t2-rules t1-time t2-time
)
263 (list t1-name t0-name t2-rules t1-rules t2-time t1-time
)
266 ;;; The following eight defvars relating to daylight savings time should NOT be
267 ;;; marked to go into loaddefs.el where they would be evaluated when Emacs is
268 ;;; dumped. These variables' appropriate values depend on the conditions under
269 ;;; which the code is INVOKED; so it's inappropriate to initialize them when
270 ;;; Emacs is dumped---they should be initialized when calendar.el is loaded.
271 ;;; They default to US Eastern time if time zone info is not available.
273 (calendar-current-time-zone)
275 (defvar calendar-time-zone
(or (car calendar-current-time-zone-cache
) -
300)
276 "*Number of minutes difference between local standard time at
277 `calendar-location-name' and Coordinated Universal (Greenwich) Time. For
278 example, -300 for New York City, -480 for Los Angeles.")
280 (defvar calendar-daylight-time-offset
281 (or (car (cdr calendar-current-time-zone-cache
)) 60)
282 "*Number of minutes difference between daylight savings and standard time.
284 If the locale never uses daylight savings time, set this to 0.")
286 (defvar calendar-standard-time-zone-name
287 (or (car (nthcdr 2 calendar-current-time-zone-cache
)) "EST")
288 "*Abbreviated name of standard time zone at `calendar-location-name'.
289 For example, \"EST\" in New York City, \"PST\" for Los Angeles.")
291 (defvar calendar-daylight-time-zone-name
292 (or (car (nthcdr 3 calendar-current-time-zone-cache
)) "EDT")
293 "*Abbreviated name of daylight-savings time zone at `calendar-location-name'.
294 For example, \"EDT\" in New York City, \"PDT\" for Los Angeles.")
297 (put 'calendar-daylight-savings-starts
'risky-local-variable t
)
298 (defvar calendar-daylight-savings-starts
299 (or (car (nthcdr 4 calendar-current-time-zone-cache
))
300 (and (not (zerop calendar-daylight-time-offset
))
301 '(calendar-nth-named-day 1 0 4 year
)))
302 "*Sexp giving the date on which daylight savings time starts.
303 This is an expression in the variable `year' whose value gives the Gregorian
304 date in the form (month day year) on which daylight savings time starts. It is
305 used to determine the starting date of daylight savings time for the holiday
306 list and for correcting times of day in the solar and lunar calculations.
308 For example, if daylight savings time is mandated to start on October 1,
309 you would set `calendar-daylight-savings-starts' to
313 If it starts on the first Sunday in April, you would set it to
315 '(calendar-nth-named-day 1 0 4 year)
317 If the locale never uses daylight savings time, set this to nil.")
320 (put 'calendar-daylight-savings-ends
'risky-local-variable t
)
321 (defvar calendar-daylight-savings-ends
322 (or (car (nthcdr 5 calendar-current-time-zone-cache
))
323 (and (not (zerop calendar-daylight-time-offset
))
324 '(calendar-nth-named-day -
1 0 10 year
)))
325 "*Sexp giving the date on which daylight savings time ends.
326 This is an expression in the variable `year' whose value gives the Gregorian
327 date in the form (month day year) on which daylight savings time ends. It is
328 used to determine the starting date of daylight savings time for the holiday
329 list and for correcting times of day in the solar and lunar calculations.
331 For example, if daylight savings time ends on the last Sunday in October:
333 '(calendar-nth-named-day -1 0 10 year)
335 If the locale never uses daylight savings time, set this to nil.")
337 (defvar calendar-daylight-savings-starts-time
338 (or (car (nthcdr 6 calendar-current-time-zone-cache
)) 120)
339 "*Number of minutes after midnight that daylight savings time starts.")
341 (defvar calendar-daylight-savings-ends-time
342 (or (car (nthcdr 7 calendar-current-time-zone-cache
))
343 calendar-daylight-savings-starts-time
)
344 "*Number of minutes after midnight that daylight savings time ends.")
346 (defun dst-in-effect (date)
347 "True if on absolute DATE daylight savings time is in effect.
348 Fractional part of DATE is local standard time of day."
349 (let* ((year (extract-calendar-year
350 (calendar-gregorian-from-absolute (floor date
))))
351 (dst-starts-gregorian (eval calendar-daylight-savings-starts
))
352 (dst-ends-gregorian (eval calendar-daylight-savings-ends
))
353 (dst-starts (and dst-starts-gregorian
354 (+ (calendar-absolute-from-gregorian
355 dst-starts-gregorian
)
356 (/ calendar-daylight-savings-starts-time
358 (dst-ends (and dst-ends-gregorian
359 (+ (calendar-absolute-from-gregorian
361 (/ (- calendar-daylight-savings-ends-time
362 calendar-daylight-time-offset
)
364 (and dst-starts dst-ends
365 (if (< dst-starts dst-ends
)
366 (and (<= dst-starts date
) (< date dst-ends
))
367 (or (<= dst-starts date
) (< date dst-ends
))))))
369 (defun dst-adjust-time (date time
&optional style
)
370 "Adjust, to account for dst on DATE, decimal fraction standard TIME.
371 Returns a list (date adj-time zone) where `date' and `adj-time' are the values
372 adjusted for `zone'; here `date' is a list (month day year), `adj-time' is a
373 decimal fraction time, and `zone' is a string.
375 Optional parameter STYLE forces the result time to be standard time when its
376 value is 'standard and daylight savings time (if available) when its value is
379 Conversion to daylight savings time is done according to
380 `calendar-daylight-savings-starts', `calendar-daylight-savings-ends',
381 `calendar-daylight-savings-starts-time',
382 `calendar-daylight-savings-ends-time', and
383 `calendar-daylight-savings-offset'."
385 (let* ((rounded-abs-date (+ (calendar-absolute-from-gregorian date
)
386 (/ (round (* 60 time
)) 60.0 24.0)))
387 (dst (dst-in-effect rounded-abs-date
))
389 calendar-daylight-time-zone-name
390 calendar-standard-time-zone-name
))
391 (time (+ rounded-abs-date
392 (if dst
(/ calendar-daylight-time-offset
24.0 60.0) 0))))
393 (list (calendar-gregorian-from-absolute (truncate time
))
394 (* 24.0 (- time
(truncate time
)))
399 ;;; arch-tag: a141d204-213c-4ca5-bdc6-f9df3aa92aad
400 ;;; cal-dst.el ends here