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