]> code.delx.au - gnu-emacs/blobdiff - lisp/calendar/solar.el
Merge pending branch
[gnu-emacs] / lisp / calendar / solar.el
index 78071bb5db8b2f1932350399a6af144c07013c66..3d6ab73e778f45c73e351f75c916382cb8f1f6ee 100644 (file)
@@ -1,7 +1,7 @@
 ;;; solar.el --- calendar functions for solar events
 
 ;; Copyright (C) 1992, 1993, 1995, 1997, 2001, 2002, 2003, 2004, 2005,
-;;   2006, 2007, 2008  Free Software Foundation, Inc.
+;;   2006, 2007, 2008, 2009, 2010  Free Software Foundation, Inc.
 
 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
 ;;         Denis B. Roegel <Denis.Roegel@loria.fr>
 
 ;; 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,
-;; diary.el, and holiday.el that deal with times of day, sunrise/sunset, and
-;; equinoxes/solstices.
+;; See calendar.el.  This file implements features that deal with
+;; times of day, sunrise/sunset, and equinoxes/solstices.
 
 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
 ;; Almanac Office, United States Naval Observatory, Washington, 1984, on
 ;;    2. Equinox/solstice times will be accurate to the minute for years
 ;;       1951--2050.  For other years the times will be within +/- 1 minute.
 
-;; Technical details of all the calendrical calculations can be found in
-;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
-;; and Nachum Dershowitz, Cambridge University Press (2001).
-
 ;;; Code:
 
-(if (fboundp 'atan)
-    (require 'lisp-float-type)
-  (error "Solar calculations impossible since floating point is unavailable"))
-
+(require 'calendar)
 (require 'cal-dst)
-(require 'cal-julian)
+;; calendar-astro-to-absolute and v versa are cal-autoloads.
+;;;(require 'cal-julian)
 
 
 (defcustom calendar-time-display-form
@@ -125,14 +116,14 @@ This variable should be set in `site-start'.el."
                   (/ (aref calendar-latitude 1) 60.0)))
              (if (numberp calendar-latitude)
                  (if (> calendar-latitude 0) "N" "S")
-               (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
+               (if (eq (aref calendar-latitude 2) 'north) "N" "S"))
              (if (numberp calendar-longitude)
                  (abs calendar-longitude)
                (+ (aref calendar-longitude 0)
                   (/ (aref calendar-longitude 1) 60.0)))
              (if (numberp calendar-longitude)
                  (if (> calendar-longitude 0) "E" "W")
-               (if (equal (aref calendar-longitude 2) 'east) "E" "W"))))
+               (if (eq (aref calendar-longitude 2) 'east) "E" "W"))))
   "Expression evaluating to the name of the calendar location.
 For example, \"New York City\".  The default value is just the
 variable `calendar-latitude' paired with the variable `calendar-longitude'.
@@ -156,26 +147,29 @@ delta.  At present, delta = 0.01 degrees, so the value of the variable
   :type 'number
   :group 'calendar)
 
-(defcustom diary-sabbath-candles-minutes 18
-  "Number of minutes before sunset for sabbath candle lighting."
-  :group 'diary
-  :type 'integer
-  :version "21.1")
-
-
-;;; End of user options.
-
-
-(defvar solar-n-hemi-seasons
+(defcustom solar-n-hemi-seasons
   '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
-  "List of season changes for the northern hemisphere.")
+  "List of season changes for the northern hemisphere."
+  :type '(list
+          (string :tag "Vernal Equinox")
+          (string :tag "Summer Solstice")
+          (string :tag "Autumnal Equinox")
+          (string :tag "Winter Solstice"))
+  :group 'calendar)
 
-(defvar solar-s-hemi-seasons
+(defcustom solar-s-hemi-seasons
   '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
-  "List of season changes for the southern hemisphere.")
+  "List of season changes for the southern hemisphere."
+  :type '(list
+          (string :tag "Autumnal Equinox")
+          (string :tag "Winter Solstice")
+          (string :tag "Vernal Equinox")
+          (string :tag "Summer Solstice"))
+  :group 'calendar)
 
-(defvar solar-sidereal-time-greenwich-midnight
-  nil
+;;; End of user options.
+
+(defvar solar-sidereal-time-greenwich-midnight nil
   "Sidereal time at Greenwich at midnight (universal time).")
 
 (defvar solar-northern-spring-or-summer-season nil
@@ -189,7 +183,7 @@ Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
       calendar-latitude
     (let ((lat (+ (aref calendar-latitude 0)
                   (/ (aref calendar-latitude 1) 60.0))))
-      (if (equal (aref calendar-latitude 2) 'north)
+      (if (eq (aref calendar-latitude 2) 'north)
           lat
         (- lat)))))
 
@@ -199,7 +193,7 @@ Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
       calendar-longitude
     (let ((long (+ (aref calendar-longitude 0)
                    (/ (aref calendar-longitude 1) 60.0))))
-      (if (equal (aref calendar-longitude 2) 'east)
+      (if (eq (aref calendar-longitude 2) 'east)
           long
         (- long)))))
 
@@ -252,10 +246,10 @@ Returns nil if nothing was entered."
 (defun solar-arctan (x quad)
   "Arctangent of X in quadrant QUAD."
   (let ((deg (radians-to-degrees (atan x))))
-    (cond ((equal quad 2) (+ deg 180))
-          ((equal quad 3) (+ deg 180))
-          ((equal quad 4) (+ deg 360))
-          (t              deg))))
+    (cond ((= quad 2) (+ deg 180))
+          ((= quad 3) (+ deg 180))
+          ((= quad 4) (+ deg 360))
+          (t          deg))))
 
 (defun solar-atn2 (x y)
   "Arctangent of point X, Y."
@@ -647,28 +641,30 @@ Corresponding value is nil if there is no sunrise/sunset."
              (list t0 (cadr exact-local-noon))
              (calendar-latitude)
              (calendar-longitude) -0.61)))
-         (rise (car rise-set))
-         (adj-rise (if rise (dst-adjust-time date rise)))
-         (set (cadr rise-set))          ; FIXME ?
-         (adj-set (if set (dst-adjust-time date set)))
+         (rise-time (car rise-set))
+         (adj-rise (if rise-time (dst-adjust-time date rise-time)))
+         (set-time (cadr rise-set))
+         (adj-set (if set-time (dst-adjust-time date set-time)))
          (length (nth 2 rise-set)))
     (list
-     (and rise (calendar-date-equal date (car adj-rise)) (cdr adj-rise))
-     (and set (calendar-date-equal date (car adj-set)) (cdr adj-set))
+     (and rise-time (calendar-date-equal date (car adj-rise)) (cdr adj-rise))
+     (and set-time (calendar-date-equal date (car adj-set)) (cdr adj-set))
      (solar-daylight length))))
 
-(defun solar-sunrise-sunset-string (date)
-  "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
+(defun solar-sunrise-sunset-string (date &optional nolocation)
+  "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
+Optional NOLOCATION non-nil means do not print the location."
   (let ((l (solar-sunrise-sunset date)))
     (format
-     "%s, %s at %s (%s hours daylight)"
+     "%s, %s%s (%s hours daylight)"
      (if (car l)
          (concat "Sunrise " (apply 'solar-time-string (car l)))
        "No sunrise")
      (if (cadr l)
          (concat "sunset " (apply 'solar-time-string (cadr l)))
        "no sunset")
-     (eval calendar-location-name)
+     (if nolocation ""
+       (format " at %s" (eval calendar-location-name)))
      (nth 2 l))))
 
 (defconst solar-data-list
@@ -731,7 +727,7 @@ The values of `calendar-daylight-savings-starts',
 `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
 `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset', and
 `calendar-time-zone' are used to interpret local time."
-  (let* ((a-d (calendar-absolute-from-astro d))
+  (let* ((a-d (calendar-astro-to-absolute d))
          ;; Get Universal Time.
          (date (calendar-astro-from-absolute
                 (- a-d
@@ -740,10 +736,10 @@ The values of `calendar-daylight-savings-starts',
                    (/ calendar-time-zone 60.0 24.0))))
          ;; Get Ephemeris Time.
          (date (+ date (solar-ephemeris-correction
-                        (extract-calendar-year
+                        (calendar-extract-year
                          (calendar-gregorian-from-absolute
                           (floor
-                           (calendar-absolute-from-astro
+                           (calendar-astro-to-absolute
                             date)))))))
          (U (/ (- date 2451545) 3652500))
          (longitude
@@ -796,6 +792,7 @@ and `calendar-time-zone' are used to interpret local time."
               end-long long)))
     (/ (+ start end) 2.0)))
 
+;; FIXME but there already is solar-sunrise-sunset.
 ;;;###autoload
 (defun sunrise-sunset (&optional arg)
   "Local time of sunrise and sunset for today.  Accurate to a few seconds.
@@ -831,18 +828,18 @@ This function is suitable for execution in a .emacs file."
                            (/ (aref calendar-latitude 1) 60.0)))
                       (if (numberp calendar-latitude)
                           (if (> calendar-latitude 0) "N" "S")
-                        (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
+                        (if (eq (aref calendar-latitude 2) 'north) "N" "S"))
                       (if (numberp calendar-longitude)
                           (abs calendar-longitude)
                         (+ (aref calendar-longitude 0)
                            (/ (aref calendar-longitude 1) 60.0)))
                       (if (numberp calendar-longitude)
                           (if (> calendar-longitude 0) "E" "W")
-                        (if (equal (aref calendar-longitude 2) 'east)
+                        (if (eq (aref calendar-longitude 2) 'east)
                             "E" "W"))))))
          (calendar-standard-time-zone-name
           (if (< arg 16) calendar-standard-time-zone-name
-            (cond ((= calendar-time-zone 0) "UTC")
+            (cond ((zerop calendar-time-zone) "UTC")
                   ((< calendar-time-zone 0)
                    (format "UTC%dmin" calendar-time-zone))
                   (t  (format "UTC+%dmin" calendar-time-zone)))))
@@ -869,20 +866,41 @@ This function is suitable for execution in a .emacs file."
 contents of temp window."))))))
 
 ;;;###cal-autoload
-(defun calendar-sunrise-sunset ()
+(defun calendar-sunrise-sunset (&optional event)
   "Local time of sunrise and sunset for date under cursor.
 Accurate to a few seconds."
-  (interactive)
+  (interactive (list last-nonmenu-event))
   (or (and calendar-latitude calendar-longitude calendar-time-zone)
       (solar-setup))
-  (let ((date (calendar-cursor-to-date t)))
+  (let ((date (calendar-cursor-to-date t event)))
     (message "%s: %s"
              (calendar-date-string date t t)
              (solar-sunrise-sunset-string date))))
 
+;;;###cal-autoload
+(defun calendar-sunrise-sunset-month (&optional event)
+  "Local time of sunrise and sunset for month under cursor or at EVENT."
+  (interactive (list last-nonmenu-event))
+  (or (and calendar-latitude calendar-longitude calendar-time-zone)
+      (solar-setup))
+  (let* ((date (calendar-cursor-to-date t event))
+         (month (car date))
+         (year (nth 2 date))
+         (last (calendar-last-day-of-month month year))
+         (title (format "Sunrise/sunset times for %s %d at %s"
+                        (calendar-month-name month) year
+                        (eval calendar-location-name))))
+    (calendar-in-read-only-buffer solar-sunrises-buffer
+      (calendar-set-mode-line title)
+      (insert title ":\n\n")
+      (dotimes (i last)
+        (setq date (list month (1+ i) year))
+        (insert (format "%s %2d: " (calendar-month-name month t) (1+ i))
+                (solar-sunrise-sunset-string date t) "\n")))))
+
 (defvar date)
 
-;; To be called from list-sexp-diary-entries, where DATE is bound.
+;; To be called from diary-list-sexp-entries, where DATE is bound.
 ;;;###diary-autoload
 (defun diary-sunrise-sunset ()
   "Local time of sunrise and sunset as a diary entry.
@@ -891,27 +909,6 @@ Accurate to a few seconds."
       (solar-setup))
   (solar-sunrise-sunset-string date))
 
-;; To be called from list-sexp-diary-entries, where DATE is bound.
-;;;###diary-autoload
-(defun diary-sabbath-candles (&optional mark)
-  "Local time of candle lighting diary entry--applies if date is a Friday.
-No diary entry if there is no sunset on that date.
-
-An optional parameter MARK specifies a face or single-character string to
-use when highlighting the day in the calendar."
-  (or (and calendar-latitude calendar-longitude calendar-time-zone)
-      (solar-setup))
-  (if (= (% (calendar-absolute-from-gregorian date) 7) 5) ; Friday
-      (let* ((sunset (cadr (solar-sunrise-sunset date)))
-             (light (if sunset
-                        (cons (- (car sunset)
-                                 (/ diary-sabbath-candles-minutes 60.0))
-                              (cdr sunset)))))
-        (if sunset
-            (cons mark
-                  (format "%s Sabbath candle lighting"
-                          (apply 'solar-time-string light)))))))
-
 ;; From Meeus, 1991, page 167.
 (defconst solar-seasons-data
   '((485 324.96 1934.136)
@@ -968,100 +965,101 @@ Accurate to within a minute between 1951 and 2050."
 (defun solar-mean-equinoxes/solstices (k year)
   "Julian day of mean equinox/solstice K for YEAR.
 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
-solstice.  These formulas are only to be used between 1000 BC and 3000 AD."
+solstice.  These formulae are only to be used between 1000 BC and 3000 AD."
   (let ((y (/ year 1000.0))
         (z (/ (- year 2000) 1000.0)))
     (if (< year 1000)                ; actually between -1000 and 1000
-        (cond ((equal k 0) (+ 1721139.29189
-                              (*  365242.13740 y)
-                              (* 0.06134 y y)
-                              (* 0.00111 y y y)
-                              (* -0.00071 y y y y)))
-              ((equal k 1) (+ 1721233.25401
-                              (* 365241.72562 y)
-                              (* -0.05323 y y)
-                              (* 0.00907 y y y)
-                              (* 0.00025 y y y y)))
-              ((equal k 2) (+ 1721325.70455
-                              (* 365242.49558 y)
-                              (* -0.11677 y y)
-                              (* -0.00297 y y y)
-                              (* 0.00074 y y y y)))
-              ((equal k 3) (+ 1721414.39987
-                              (* 365242.88257 y)
-                              (* -0.00769 y y)
-                              (* -0.00933 y y y)
-                              (* -0.00006 y y y y))))
+        (cond ((= k 0) (+ 1721139.29189
+                          (*  365242.13740 y)
+                          (* 0.06134 y y)
+                          (* 0.00111 y y y)
+                          (* -0.00071 y y y y)))
+              ((= k 1) (+ 1721233.25401
+                          (* 365241.72562 y)
+                          (* -0.05323 y y)
+                          (* 0.00907 y y y)
+                          (* 0.00025 y y y y)))
+              ((= k 2) (+ 1721325.70455
+                          (* 365242.49558 y)
+                          (* -0.11677 y y)
+                          (* -0.00297 y y y)
+                          (* 0.00074 y y y y)))
+              ((= k 3) (+ 1721414.39987
+                          (* 365242.88257 y)
+                          (* -0.00769 y y)
+                          (* -0.00933 y y y)
+                          (* -0.00006 y y y y))))
                                         ; actually between 1000 and 3000
-      (cond ((equal k 0) (+ 2451623.80984
-                            (* 365242.37404  z)
-                            (* 0.05169 z z)
-                            (* -0.00411 z z z)
-                            (* -0.00057 z z z z)))
-            ((equal k 1) (+ 2451716.56767
-                            (* 365241.62603 z)
-                            (* 0.00325 z z)
-                            (* 0.00888 z z z)
-                            (* -0.00030 z z z z)))
-            ((equal k 2) (+ 2451810.21715
-                            (* 365242.01767 z)
-                            (* -0.11575 z z)
-                            (* 0.00337 z z z)
-                            (* 0.00078 z z z z)))
-            ((equal k 3) (+ 2451900.05952
-                            (* 365242.74049 z)
-                            (* -0.06223 z z)
-                            (* -0.00823 z z z)
-                            (* 0.00032 z z z z)))))))
-
-(defvar displayed-month)                ; from generate-calendar
+      (cond ((= k 0) (+ 2451623.80984
+                        (* 365242.37404  z)
+                        (* 0.05169 z z)
+                        (* -0.00411 z z z)
+                        (* -0.00057 z z z z)))
+            ((= k 1) (+ 2451716.56767
+                        (* 365241.62603 z)
+                        (* 0.00325 z z)
+                        (* 0.00888 z z z)
+                        (* -0.00030 z z z z)))
+            ((= k 2) (+ 2451810.21715
+                        (* 365242.01767 z)
+                        (* -0.11575 z z)
+                        (* 0.00337 z z z)
+                        (* 0.00078 z z z z)))
+            ((= k 3) (+ 2451900.05952
+                        (* 365242.74049 z)
+                        (* -0.06223 z z)
+                        (* -0.00823 z z z)
+                        (* 0.00032 z z z z)))))))
+
+(defvar displayed-month)                ; from calendar-generate
 (defvar displayed-year)
 
 ;;;###holiday-autoload
 (defun solar-equinoxes-solstices ()
   "Local date and time of equinoxes and solstices, if visible in the calendar.
 Requires floating point."
-  (let ((m displayed-month)
-        (y displayed-year))
-    (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
-                                        ((= 2 (% m 3))  1)
-                                        (t              0)))
-    (let* ((calendar-standard-time-zone-name
-            (if calendar-time-zone calendar-standard-time-zone-name "UTC"))
-           (calendar-daylight-savings-starts
-            (if calendar-time-zone calendar-daylight-savings-starts))
-           (calendar-daylight-savings-ends
-            (if calendar-time-zone calendar-daylight-savings-ends))
-           (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
-           (k (1- (/ m 3)))
-           (d0 (solar-equinoxes/solstices k y))
-           (d1 (list (car d0) (floor (cadr d0)) (nth 2 d0)))
-           (h0 (* 24 (- (cadr d0) (floor (cadr d0)))))
-           (adj (dst-adjust-time d1 h0))
-           (d (list (caar adj)
-                    (+ (car (cdar adj))
-                       (/ (cadr adj) 24.0))
-                    (cadr (cdar adj))))
-           ;; The following is nearly as accurate, but not quite:
-           ;; (d0 (solar-date-next-longitude
-           ;;     (calendar-astro-from-absolute
-           ;;      (calendar-absolute-from-gregorian
-           ;;       (list (+ 3 (* k 3)) 15 y)))
-           ;;     90))
-           ;; (abs-day (calendar-absolute-from-astro d)))
-           (abs-day (calendar-absolute-from-gregorian d)))
-      (list
-       (list (calendar-gregorian-from-absolute (floor abs-day))
-             (format "%s %s"
-                     (nth k (if (and calendar-latitude
-                                     (< (calendar-latitude) 0))
-                                solar-s-hemi-seasons
-                              solar-n-hemi-seasons))
-                     (solar-time-string
-                      (* 24 (- abs-day (floor abs-day)))
-                      (if (dst-in-effect abs-day)
-                          calendar-daylight-time-zone-name
-                        calendar-standard-time-zone-name))))))))
+  (let* ((m displayed-month)
+         (y displayed-year)
+         (calendar-standard-time-zone-name
+          (if calendar-time-zone calendar-standard-time-zone-name "UTC"))
+         (calendar-daylight-savings-starts
+          (if calendar-time-zone calendar-daylight-savings-starts))
+         (calendar-daylight-savings-ends
+          (if calendar-time-zone calendar-daylight-savings-ends))
+         (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
+         (k (progn
+              (calendar-increment-month m y (cond ((= 1 (% m 3)) -1)
+                                                  ((= 2 (% m 3))  1)
+                                                  (t              0)))
+              (1- (/ m 3))))
+         (d0 (solar-equinoxes/solstices k y))
+         (d1 (list (car d0) (floor (cadr d0)) (nth 2 d0)))
+         (h0 (* 24 (- (cadr d0) (floor (cadr d0)))))
+         (adj (dst-adjust-time d1 h0))
+         (d (list (caar adj)
+                  (+ (car (cdar adj))
+                     (/ (cadr adj) 24.0))
+                  (cadr (cdar adj))))
+         ;; The following is nearly as accurate, but not quite:
+         ;; (d0 (solar-date-next-longitude
+         ;;     (calendar-astro-from-absolute
+         ;;      (calendar-absolute-from-gregorian
+         ;;       (list (+ 3 (* k 3)) 15 y)))
+         ;;     90))
+         ;; (abs-day (calendar-astro-to-absolute d)))
+         (abs-day (calendar-absolute-from-gregorian d)))
+    (list
+     (list (calendar-gregorian-from-absolute (floor abs-day))
+           (format "%s %s"
+                   (nth k (if (and calendar-latitude
+                                   (< (calendar-latitude) 0))
+                              solar-s-hemi-seasons
+                            solar-n-hemi-seasons))
+                   (solar-time-string
+                    (* 24 (- abs-day (floor abs-day)))
+                    (if (dst-in-effect abs-day)
+                        calendar-daylight-time-zone-name
+                      calendar-standard-time-zone-name)))))))
 
 
 (provide 'solar)