]> code.delx.au - gnu-emacs/blobdiff - lisp/calendar/cal-dst.el
Make typing into incomplete C++ raw strings work, and make it work fast enough
[gnu-emacs] / lisp / calendar / cal-dst.el
index 78d8b7f47937543283e00f53f8547da194d734e9..84831e7beea1236660a2c2f4f74a97164aef2344 100644 (file)
@@ -1,20 +1,20 @@
 ;;; cal-dst.el --- calendar functions for daylight saving rules
 
-;; Copyright (C) 1993, 1994, 1995, 1996, 2001, 2002, 2003, 2004, 2005,
-;;   2006, 2007, 2008  Free Software Foundation, Inc.
+;; Copyright (C) 1993-1996, 2001-2016 Free Software Foundation, Inc.
 
 ;; Author: Paul Eggert <eggert@twinsun.com>
 ;;         Edward M. Reingold <reingold@cs.uiuc.edu>
 ;; Maintainer: Glenn Morris <rgm@gnu.org>
 ;; Keywords: calendar
 ;; Human-Keywords: daylight saving time, calendar, diary, holidays
+;; Package: calendar
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
-;; This collection of functions implements the features of calendar.el and
-;; holiday.el that deal with daylight saving time.
+;; See calendar.el.
 
 ;;; Code:
 
 (require 'calendar)
-(require 'cal-persia)
 
 
 (defgroup calendar-dst nil
@@ -51,7 +47,7 @@ correct, since the dates of daylight saving transitions sometimes
 change."
   :type 'boolean
   :version "22.1"
-  :group 'calendar)
+  :group 'calendar-dst)
 
 ;;;###autoload
 (put 'calendar-daylight-savings-starts 'risky-local-variable t)
@@ -86,7 +82,7 @@ list and for correcting times of day in the solar and lunar calculations.
 
 For example, if daylight saving time ends on the last Sunday in October:
 
-      '(calendar-nth-named-day -1 0 10 year)
+      (calendar-nth-named-day -1 0 10 year)
 
 If the locale never uses daylight saving time, set this to nil."
   :type 'sexp
@@ -97,7 +93,9 @@ If the locale never uses daylight saving time, set this to nil."
 
 (defvar calendar-current-time-zone-cache nil
   "Cache for result of `calendar-current-time-zone'.")
-(put 'calendar-current-time-zone-cache 'risky-local-variable t) ; why?
+;; It gets eval'd, eg by calendar-dst-starts.
+;;;###autoload
+(put 'calendar-current-time-zone-cache 'risky-local-variable t)
 
 (defvar calendar-system-time-basis
   (calendar-absolute-from-gregorian '(1 1 1970))
@@ -143,8 +141,8 @@ midnight UTC on absolute date ABS-DATE."
   "Return the time of the next time zone transition after TIME.
 Both TIME and the result are acceptable arguments to `current-time-zone'.
 Return nil if no such transition can be found."
-  (let* ((base 65536)           ;; 2^16 = base of current-time output
-         (quarter-multiple 120) ;; approx = (seconds per quarter year) / base
+  (let* ((base 65536)           ; 2^16 = base of current-time output
+         (quarter-multiple 120) ; approx = (seconds per quarter year) / base
          (time-zone (current-time-zone time))
          (time-utc-diff (car time-zone))
          hi
@@ -154,10 +152,10 @@ Return nil if no such transition can be found."
     ;; Heuristic: probe the time zone offset in the next three calendar
     ;; quarters, looking for a time zone offset different from TIME.
     (while (and quarters (eq time-utc-diff hi-utc-diff))
-      (setq hi (cons (+ (car time) (* (car quarters) quarter-multiple)) 0))
-      (setq hi-zone (current-time-zone hi))
-      (setq hi-utc-diff (car hi-zone))
-      (setq quarters (cdr quarters)))
+      (setq hi (cons (+ (car time) (* (car quarters) quarter-multiple)) 0)
+            hi-zone (current-time-zone hi)
+            hi-utc-diff (car hi-zone)
+            quarters (cdr quarters)))
     (and
      time-utc-diff
      hi-utc-diff
@@ -181,77 +179,70 @@ Return nil if no such transition can be found."
          (if (eq (car (current-time-zone probe)) hi-utc-diff)
              (setq hi probe)
            (setq lo probe)))
+       (setcdr hi (list (cdr hi)))
        hi))))
 
+(autoload 'calendar-persian-to-absolute "cal-persia")
+
 (defun calendar-time-zone-daylight-rules (abs-date utc-diff)
   "Return daylight transition rule for ABS-DATE, UTC-DIFF sec offset from UTC.
 ABS-DATE must specify a day that contains a daylight saving transition.
 The result has the proper form for `calendar-daylight-savings-starts'."
   (let* ((date (calendar-gregorian-from-absolute abs-date))
          (weekday (% abs-date 7))
-         (m (extract-calendar-month date))
-         (d (extract-calendar-day date))
-         (y (extract-calendar-year date))
+         (m (calendar-extract-month date))
+         (d (calendar-extract-day date))
+         (y (calendar-extract-year date))
          (last (calendar-last-day-of-month m y))
-         (candidate-rules
+         j rlist
+         (candidate-rules               ; these return Gregorian dates
           (append
            ;; Day D of month M.
-           (list (list 'list m d 'year))
+           `((list ,m ,d year))
            ;; The first WEEKDAY of month M.
            (if (< d 8)
-               (list (list 'calendar-nth-named-day 1 weekday m 'year)))
+               `((calendar-nth-named-day 1 ,weekday ,m year)))
            ;; The last WEEKDAY of month M.
            (if (> d (- last 7))
-               (list (list 'calendar-nth-named-day -1 weekday m 'year)))
-           ;; The first WEEKDAY after day J of month M, for D-6 < J <= D.
-           (let (l)
-             (calendar-for-loop j from (max 2 (- d 6)) to (min d (- last 8)) do
-                                (setq l
-                                      (cons
-                                       (list 'calendar-nth-named-day
-                                             1 weekday m 'year j)
-                                       l)))
-             l)
+               `((calendar-nth-named-day -1 ,weekday ,m year)))
+           (progn
+             ;; The first WEEKDAY after day J of month M, for D-6 < J <= D.
+             (setq j (1- (max 2 (- d 6))))
+             (while (<= (setq j (1+ j)) (min d (- last 8)))
+               (push `(calendar-nth-named-day 1 ,weekday ,m year ,j) rlist))
+             rlist)
            ;; 01-01 and 07-01 for this year's Persian calendar.
-           (if (and (= m 3) (<= 20 d) (<= d 21))
-               '((calendar-gregorian-from-absolute
-                  (calendar-absolute-from-persian
-                   (list 1 1 (- year 621))))))
-           (if (and (= m 9) (<= 22 d) (<= d 23))
-               '((calendar-gregorian-from-absolute
-                  (calendar-absolute-from-persian
-                   (list 7 1 (- year 621))))))))
-         (prevday-sec (- -1 utc-diff)) ;; last sec of previous local day
-         (year (1+ y)))
+           ;; FIXME what does the Persian calendar have to do with this?
+           (and (= m 3) (memq d '(20 21))
+                '((calendar-gregorian-from-absolute
+                   (calendar-persian-to-absolute `(1 1 ,(- year 621))))))
+           (and (= m 9) (memq d '(22 23))
+                '((calendar-gregorian-from-absolute
+                   (calendar-persian-to-absolute `(7 1 ,(- year 621))))))))
+         (prevday-sec (- -1 utc-diff)) ; last sec of previous local day
+         (year (1+ y))
+         new-rules)
     ;; Scan through the next few years until only one rule remains.
-    (while
-        (let ((rules candidate-rules)
-              new-rules)
-          (while
-              (let*
-                  ((rule (car rules))
-                   (date
-                    ;; The following is much faster than
-                    ;; (calendar-absolute-from-gregorian (eval rule)).
-                    (cond ((eq (car rule) 'calendar-nth-named-day)
-                           (eval (cons 'calendar-nth-named-absday (cdr rule))))
-                          ((eq (car rule) 'calendar-gregorian-from-absolute)
-                           (eval (car (cdr rule))))
-                          (t (let ((g (eval rule)))
-                               (calendar-absolute-from-gregorian g))))))
-                (or (equal
-                     (current-time-zone
-                      (calendar-time-from-absolute date prevday-sec))
-                     (current-time-zone
-                      (calendar-time-from-absolute (1+ date) prevday-sec)))
-                    (setq new-rules (cons rule new-rules)))
-                (setq rules (cdr rules))))
-          ;; If no rules remain, just use the first candidate rule;
-          ;; it's wrong in general, but it's right for at least one year.
-          (setq candidate-rules (if new-rules (nreverse new-rules)
-                                  (list (car candidate-rules))))
-          (setq year (1+ year))
-          (cdr candidate-rules)))
+    (while (cdr candidate-rules)
+      (dolist (rule candidate-rules)
+        ;; The rule we return should give a Gregorian date, but here
+        ;; we require an absolute date.  The following is for efficiency.
+        (setq date (cond ((eq (car rule) 'calendar-nth-named-day)
+                          (eval (cons 'calendar-nth-named-absday (cdr rule))))
+                         ((eq (car rule) 'calendar-gregorian-from-absolute)
+                          (eval (cadr rule)))
+                         (t (calendar-absolute-from-gregorian (eval rule)))))
+        (or (equal (current-time-zone
+                    (calendar-time-from-absolute date prevday-sec))
+                   (current-time-zone
+                    (calendar-time-from-absolute (1+ date) prevday-sec)))
+            (setq new-rules (cons rule new-rules))))
+      ;; If no rules remain, just use the first candidate rule;
+      ;; it's wrong in general, but it's right for at least one year.
+      (setq candidate-rules (if new-rules (nreverse new-rules)
+                              (list (car candidate-rules)))
+            new-rules nil
+            year (1+ year)))
     (car candidate-rules)))
 
 ;; TODO it might be better to extract this information directly from
@@ -265,7 +256,7 @@ for `calendar-current-time-zone'."
   (let* ((t0 (or time (current-time)))
          (t0-zone (current-time-zone t0))
          (t0-utc-diff (car t0-zone))
-         (t0-name (car (cdr t0-zone))))
+         (t0-name (cadr t0-zone)))
     (if (not t0-utc-diff)
         ;; Little or no time zone information is available.
         (list nil nil t0-name t0-name nil nil nil nil)
@@ -277,7 +268,7 @@ for `calendar-current-time-zone'."
           ;; Use heuristics to find daylight saving parameters.
           (let* ((t1-zone (current-time-zone t1))
                  (t1-utc-diff (car t1-zone))
-                 (t1-name (car (cdr t1-zone)))
+                 (t1-name (cadr t1-zone))
                  (t1-date-sec (calendar-absolute-from-time t1 t0-utc-diff))
                  (t2-date-sec (calendar-absolute-from-time t2 t1-utc-diff))
                  ;; TODO When calendar-dst-check-each-year-flag is non-nil,
@@ -357,8 +348,8 @@ DST-ZONE are equal, and all the DST-* integer variables are 0.
 Some operating systems cannot provide all this information to Emacs; in this
 case, `calendar-current-time-zone' returns a list containing nil for the data
 it can't find."
-  (unless calendar-current-time-zone-cache
-    (setq calendar-current-time-zone-cache (calendar-dst-find-data))))
+  (or calendar-current-time-zone-cache
+      (setq calendar-current-time-zone-cache (calendar-dst-find-data))))
 
 
 ;; Following options should be set based on conditions when the code
@@ -374,34 +365,34 @@ For example, -300 for New York City, -480 for Los Angeles."
   :group 'calendar-dst)
 
 (defcustom calendar-daylight-time-offset
-  (or (car (cdr calendar-current-time-zone-cache)) 60)
+  (or (cadr calendar-current-time-zone-cache) 60)
   "Number of minutes difference between daylight saving and standard time.
 If the locale never uses daylight saving time, set this to 0."
   :type 'integer
   :group 'calendar-dst)
 
 (defcustom calendar-standard-time-zone-name
-  (or (car (nthcdr 2 calendar-current-time-zone-cache)) "EST")
+  (or (nth 2 calendar-current-time-zone-cache) "EST")
   "Abbreviated name of standard time zone at `calendar-location-name'.
 For example, \"EST\" in New York City, \"PST\" for Los Angeles."
   :type 'string
   :group 'calendar-dst)
 
 (defcustom calendar-daylight-time-zone-name
-  (or (car (nthcdr 3 calendar-current-time-zone-cache)) "EDT")
+  (or (nth 3 calendar-current-time-zone-cache) "EDT")
   "Abbreviated name of daylight saving time zone at `calendar-location-name'.
 For example, \"EDT\" in New York City, \"PDT\" for Los Angeles."
   :type 'string
   :group 'calendar-dst)
 
 (defcustom calendar-daylight-savings-starts-time
-  (or (car (nthcdr 6 calendar-current-time-zone-cache)) 120)
+  (or (nth 6 calendar-current-time-zone-cache) 120)
   "Number of minutes after midnight that daylight saving time starts."
   :type 'integer
   :group 'calendar-dst)
 
 (defcustom calendar-daylight-savings-ends-time
-  (or (car (nthcdr 7 calendar-current-time-zone-cache))
+  (or (nth 7 calendar-current-time-zone-cache)
       calendar-daylight-savings-starts-time)
   "Number of minutes after midnight that daylight saving time ends."
   :type 'integer
@@ -430,10 +421,11 @@ This function respects the value of `calendar-dst-check-each-year-flag'."
       (and (not (zerop calendar-daylight-time-offset))
            (calendar-nth-named-day 1 0 11 year))))
 
+;; used by calc, solar.
 (defun dst-in-effect (date)
   "True if on absolute DATE daylight saving time is in effect.
 Fractional part of DATE is local standard time of day."
-  (let* ((year (extract-calendar-year
+  (let* ((year (calendar-extract-year
                 (calendar-gregorian-from-absolute (floor date))))
          (dst-starts-gregorian (eval calendar-daylight-savings-starts))
          (dst-ends-gregorian (eval calendar-daylight-savings-ends))
@@ -453,22 +445,17 @@ Fractional part of DATE is local standard time of day."
              (and (<= dst-starts date) (< date dst-ends))
            (or (<= dst-starts date) (< date dst-ends))))))
 
-(defun dst-adjust-time (date time &optional style)
+;; used by calc, lunar, solar.
+(defun dst-adjust-time (date time)
   "Adjust, to account for dst on DATE, decimal fraction standard TIME.
 Returns a list (date adj-time zone) where `date' and `adj-time' are the values
 adjusted for `zone'; here `date' is a list (month day year), `adj-time' is a
 decimal fraction time, and `zone' is a string.
 
-Optional parameter STYLE forces the result time to be standard time when its
-value is 'standard and daylight saving time (if available) when its value is
-'daylight.
-
 Conversion to daylight saving time is done according to
 `calendar-daylight-savings-starts', `calendar-daylight-savings-ends',
 `calendar-daylight-savings-starts-time',
-`calendar-daylight-savings-ends-time', and
-`calendar-daylight-savings-offset'."
-
+`calendar-daylight-savings-ends-time', and `calendar-daylight-time-offset'."
   (let* ((rounded-abs-date (+ (calendar-absolute-from-gregorian date)
                               (/ (round (* 60 time)) 60.0 24.0)))
          (dst (dst-in-effect rounded-abs-date))
@@ -483,5 +470,4 @@ Conversion to daylight saving time is done according to
 
 (provide 'cal-dst)
 
-;; arch-tag: a141d204-213c-4ca5-bdc6-f9df3aa92aad
 ;;; cal-dst.el ends here