cal-dst.el 20 KB

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