annotate lisp/calendar/solar.el @ 1009:bf78b5ea9b3a

* lread.c (defvar_per_buffer): Support new TYPE argument, by setting the appropriate slot in buffer_local_types.
author Jim Blandy <jimb@redhat.com>
date Wed, 19 Aug 1992 06:33:16 +0000
parents 61c6983219ff
children f45b07fa4e57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
956
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1 ;;; solar.el --- calendar functions for solar events.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
4
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
6 ;; Keywords: sunrise, sunset, equinox, solstice, calendar, diary, holidays
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
7
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10 ;; GNU Emacs is distributed in the hope that it will be useful,
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12 ;; accepts responsibility to anyone for the consequences of using it
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 ;; or for whether it serves any particular purpose or works at all,
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15 ;; License for full details.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17 ;; Everyone is granted permission to copy, modify and redistribute
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 ;; GNU Emacs, but only under the conditions described in the
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19 ;; GNU Emacs General Public License. A copy of this license is
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20 ;; supposed to have been given to you along with GNU Emacs so you
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21 ;; can know your rights and responsibilities. It should be in a
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22 ;; file named COPYING. Among other things, the copyright notice
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23 ;; and this notice must be preserved on all copies.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25 ;;; Commentary:
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27 ;; This collection of functions implements the features of calendar.el and
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28 ;; diary.el that deal with sunrise/sunset and eqinoxes/solstices.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31 ;; Almanac Office, United States Naval Observatory, Washington, 1984 and
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32 ;; on ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
33 ;; Willmann-Bell, Inc., 1985.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
34 ;;
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
35 ;; WARNINGS:
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
36 ;; 1. SUNRISE/SUNSET calculations will be accurate only to +/- 2 minutes.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37 ;; Locations should be between +/- 65 degrees of latitude.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38 ;; Dates should be in the latter half of the 20th century.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39 ;;
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 ;; 2. Equinox/solstice times will be accurate only to +/- 15 minutes.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42 ;; The author would be delighted to have an astronomically more sophisticated
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43 ;; person rewrite the code for the solar calculations in this file!
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45 ;; Comments, corrections, and improvements should be sent to
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46 ;; Edward M. Reingold Department of Computer Science
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49 ;; Urbana, Illinois 61801
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 ;;; Code:
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 (if (fboundp 'atan)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 (require 'lisp-float-type)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 (error "Solar calculations impossible since floating point is unavailable."))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 (require 'calendar)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 (defun solar-setup ()
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60 "Prompt user for latitude, longitude, and time zone."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 (beep)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 (if (not calendar-longitude)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 (setq calendar-longitude
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 (solar-get-number
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 "Enter longitude (decimal fraction; + east, - west): ")))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 (if (not calendar-latitude)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 (setq calendar-latitude
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 (solar-get-number
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 "Enter latitude (decimal fraction; + north, - south): ")))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 (if (not calendar-time-zone)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71 (setq calendar-time-zone
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 (solar-get-number
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 "Enter difference from Universal Time (in minutes): "))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 (defun solar-get-number (prompt)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 "Return a number from the minibuffer, prompting with PROMPT.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 Returns nil if nothing was entered."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 (let ((x (read-string prompt "")))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 (if (not (string-equal x ""))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80 (string-to-int x))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 (defun solar-sin-degrees (x)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 (sin (degrees-to-radians x)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
85 (defun solar-cosine-degrees (x)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86 (cos (degrees-to-radians x)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
88 (defun solar-tangent-degrees (x)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89 (tan (degrees-to-radians x)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 (defun solar-xy-to-quadrant (x y)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 "Determines the quadrant of the point X, Y."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93 (if (> x 0)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94 (if (> y 0) 1 4)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 (if (> y 0) 2 3)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97 (defun solar-degrees-to-quadrant (angle)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98 "Determines the quadrant of ANGLE."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 (1+ (truncate (/ (solar-mod angle 360.0) 90.0))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101 (defun solar-arctan (x quad)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 "Arctangent of X in quadrant QUAD."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103 (let ((deg (radians-to-degrees (atan x))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 (cond ((equal quad 2) (+ deg 180))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 ((equal quad 3) (+ deg 180))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106 ((equal quad 4) (+ deg 360))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
107 (t deg))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109 (defun solar-arccos (x)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
110 (let ((y (sqrt (- 1 (* x x)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 (defun solar-arcsin (y)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114 (let ((x (sqrt (- 1 (* y y)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117 (defun solar-mod (x y)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 "Returns X mod Y; value is *always* non-negative."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 (let ((v (% x y)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
120 (if (> 0 v)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121 (+ v y)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 v)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124 (defconst solar-earth-inclination 23.441884
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125 "Inclination of earth's equator to its solar orbit in degrees.")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127 (defconst solar-cos-inclination (solar-cosine-degrees solar-earth-inclination)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 "Cosine of earth's inclination.")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130 (defconst solar-sin-inclination (solar-sin-degrees solar-earth-inclination)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131 "Sine of earth's inclination.")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133 (defconst solar-earth-orbit-eccentricity 0.016718
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134 "Eccentricity of orbit of the earth around the sun.")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 (defmacro solar-degrees-to-hours (deg)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 (list '/ deg 15))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 (defmacro solar-hours-to-days (hour)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 (list '/ hour 24))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 (defun solar-longitude-of-sun (day)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 "Longitude of the sun at DAY in the year."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144 (let ((mean-anomaly (- (* 0.9856 day) 3.289)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 (solar-mod (+ mean-anomaly
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
146 (* 1.916 (solar-sin-degrees mean-anomaly))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147 (* 0.020 (solar-sin-degrees (* 2 mean-anomaly)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 282.634)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 360)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 (defun solar-right-ascension (longitude)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 "Right ascension of the sun, given its LONGITUDE."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153 (solar-degrees-to-hours
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
154 (solar-arctan
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
155 (* solar-cos-inclination (solar-tangent-degrees longitude))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
156 (solar-degrees-to-quadrant longitude))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
157
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
158 (defun solar-declination (longitude)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
159 "Declination of the sun, given its LONGITUDE."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
160 (solar-arcsin
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 (* solar-sin-inclination
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 (solar-sin-degrees longitude))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164 (defun solar-sunrise (date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165 "Calculates the *standard* time of sunrise for Gregorian DATE for location
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166 given by `calendar-latitude' and `calendar-longitude'. Returns a decimal fraction
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 of hours. Returns nil if the sun does not rise at that location on that day."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 (let* ((day-of-year (calendar-day-number date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 (approx-sunrise
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170 (+ day-of-year
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171 (solar-hours-to-days
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
172 (- 6 (solar-degrees-to-hours calendar-longitude)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
173 (solar-longitude-of-sun-at-sunrise
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
174 (solar-longitude-of-sun approx-sunrise))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
175 (solar-right-ascension-at-sunrise
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 (solar-right-ascension solar-longitude-of-sun-at-sunrise))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177 (solar-declination-at-sunrise
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 (solar-declination solar-longitude-of-sun-at-sunrise))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 (cos-local-sunrise
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 (/ (- (solar-cosine-degrees (+ 90 (/ 50.0 60.0)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 (* (solar-sin-degrees solar-declination-at-sunrise)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182 (solar-sin-degrees calendar-latitude)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 (* (solar-cosine-degrees solar-declination-at-sunrise)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 (solar-cosine-degrees calendar-latitude)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 (if (<= (abs cos-local-sunrise) 1);; otherwise, no sunrise that day
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
186 (let* ((local-sunrise (solar-degrees-to-hours
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
187 (- 360 (solar-arccos cos-local-sunrise))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188 (local-mean-sunrise
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 (solar-mod (- (+ local-sunrise solar-right-ascension-at-sunrise)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
190 (+ (* 0.065710 approx-sunrise)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
191 6.622))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
192 24)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193 (+ (- local-mean-sunrise (solar-degrees-to-hours calendar-longitude))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 (/ calendar-time-zone 60.0))))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196 (defun solar-sunset (date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 "Calculates the *standard* time of sunset for Gregorian DATE for location
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
198 given by `calendar-latitude' and `calendar-longitude'. Returns a decimal fractions
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
199 of hours. Returns nil if the sun does not set at that location on that day."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
200 (let* ((day-of-year (calendar-day-number date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
201 (approx-sunset
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
202 (+ day-of-year
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
203 (solar-hours-to-days
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
204 (- 18 (solar-degrees-to-hours calendar-longitude)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205 (solar-longitude-of-sun-at-sunset
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 (solar-longitude-of-sun approx-sunset))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207 (solar-right-ascension-at-sunset
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
208 (solar-right-ascension solar-longitude-of-sun-at-sunset))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209 (solar-declination-at-sunset
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210 (solar-declination solar-longitude-of-sun-at-sunset))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
211 (cos-local-sunset
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212 (/ (- (solar-cosine-degrees (+ 90 (/ 50.0 60.0)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213 (* (solar-sin-degrees solar-declination-at-sunset)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 (solar-sin-degrees calendar-latitude)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215 (* (solar-cosine-degrees solar-declination-at-sunset)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 (solar-cosine-degrees calendar-latitude)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 (if (<= (abs cos-local-sunset) 1);; otherwise, no sunset that day
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218 (let* ((local-sunset (solar-degrees-to-hours
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219 (solar-arccos cos-local-sunset)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220 (local-mean-sunset
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 (solar-mod (- (+ local-sunset solar-right-ascension-at-sunset)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
222 (+ (* 0.065710 approx-sunset) 6.622))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
223 24)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224 (+ (- local-mean-sunset (solar-degrees-to-hours calendar-longitude))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225 (/ calendar-time-zone 60.0))))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227 (defun solar-time-string (time date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
228 "Printable form for decimal fraction standard TIME on DATE.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229 Format used is given by `calendar-time-display-form'. Converted to daylight
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
230 savings time according to `calendar-daylight-savings-starts' and
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231 `calendar-daylight-savings-ends', if those variables are not nil."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 (let* ((year (extract-calendar-year date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 (abs-date (calendar-absolute-from-gregorian date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
234 (dst (and calendar-daylight-savings-starts
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235 calendar-daylight-savings-ends
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236 (<= (calendar-absolute-from-gregorian
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
237 (eval calendar-daylight-savings-starts))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238 abs-date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239 (< abs-date
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240 (calendar-absolute-from-gregorian
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
241 (eval calendar-daylight-savings-ends)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
242 (time (if dst (1+ time) time))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
243 (time-zone (if dst
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
244 calendar-daylight-time-zone-name
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
245 calendar-standard-time-zone-name))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
246 (24-hours (truncate time))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
247 (12-hours (format "%d" (if (> 24-hours 12)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
248 (- 24-hours 12)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
249 (if (= 24-hours 0) 12 24-hours))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
250 (am-pm (if (>= 24-hours 12) "pm" "am"))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
251 (minutes (format "%02d" (round (* 60 (- time 24-hours)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
252 (24-hours (format "%02d" 24-hours)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
253 (mapconcat 'eval calendar-time-display-form "")))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
254
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
255 (defun solar-sunrise-sunset (date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
256 "String giving local times of sunrise and sunset on Gregorian DATE."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
257 (let ((rise (solar-sunrise date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
258 (set (solar-sunset date)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
259 (format "%s, %s at %s"
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
260 (if rise
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
261 (concat "Sunrise " (solar-time-string rise date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
262 "No sunrise")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
263 (if set
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
264 (concat "sunset " (solar-time-string set date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
265 "no sunset")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
266 (eval calendar-location-name))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
267
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
268 (defun solar-apparent-longitude-of-sun (date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
269 "Apparent longitude of the sun on Gregorian DATE."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
270 (let* ((time (/ (- (calendar-absolute-from-gregorian date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
271 (calendar-absolute-from-gregorian '(1 0.5 1900)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
272 36525))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
273 (l (+ 279.69668
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
274 (* 36000.76892 time)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
275 (* 0.0003025 time time)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
276 (m (+ 358.47583
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
277 (* 35999.04975 time)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
278 (* -0.000150 time time)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
279 (* -0.0000033 time time time)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
280 (c (+ (* (+ 1.919460
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
281 (* -0.004789 time)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
282 (* -0.000014 time time))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
283 (solar-sin-degrees m))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
284 (* (+ 0.020094
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
285 (* -0.000100 time))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
286 (solar-sin-degrees (* 2 m)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
287 (* 0.000293
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
288 (solar-sin-degrees (* 3 m)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
289 (L (+ l c))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
290 (omega (+ 259.18
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
291 (* -1934.142 time)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
292 (app (+ L
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
293 -0.00569
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
294 (* -0.00479
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
295 (solar-sin-degrees omega)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
296 app))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
297
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
298 (defun solar-ephemeris-correction (year)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
299 "Difference in minutes between Ephemeris time an Universal time in YEAR.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
300 Value is only an approximation."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
301 (let ((T (/ (- year 1900) 100.0)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
302 (+ 0.41 (* 1.2053 T) (* 0.4992 T T))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
303
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
304 (defun solar-equinoxes/solstices (k year)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
305 "Date of equinox/solstice K for YEAR. K=0, spring equinox; K=1, summer
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
306 solstice; K=2, fall equinox; K=3, winter solstice. Accurate to within
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
307 several minutes."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
308 (let ((date (list (+ 3 (* k 3)) 21 year))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
309 (correction 1000))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
310 (while (> correction 0.00001)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
311 (setq app (solar-mod (solar-apparent-longitude-of-sun date) 360.0))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
312 (setq correction (* 58 (solar-sin-degrees (- (* k 90) app))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
313 (setq date (list (extract-calendar-month date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
314 (+ (extract-calendar-day date) correction)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
315 year)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
316 (list (extract-calendar-month date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
317 (+ (extract-calendar-day date) (/ calendar-time-zone 60.0 24.0)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
318 (- (/ (solar-ephemeris-correction year) 60.0 24.0)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
319 year)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
320
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
321 ;;;###autoload
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
322 (defun sunrise-sunset (&optional arg)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
323 "Local time of sunrise and sunset for today. Accurate to +/- 2 minutes.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
324 If called with an optional prefix argument, prompts for date.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
325
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
326 If called with an optional double prefix argument, prompts for longitude,
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
327 latitude, time zone, and date.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
328
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
329 This function is suitable for execution in a .emacs file."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
330 (interactive "p")
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
331 (if (and (< arg 16)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
332 (not (and calendar-latitude calendar-longitude calendar-time-zone)))
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
333 (solar-setup))
956
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
334 (let* ((calendar-longitude
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
335 (if (< arg 16) calendar-longitude
956
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
336 (solar-get-number
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
337 "Enter longitude (decimal fraction; + east, - west): ")))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
338 (calendar-latitude
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
339 (if (< arg 16) calendar-latitude
956
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
340 (solar-get-number
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
341 "Enter latitude (decimal fraction; + north, - south): ")))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
342 (calendar-time-zone
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
343 (if (< arg 16) calendar-time-zone
956
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
344 (solar-get-number
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
345 "Enter difference from Universal Time (in minutes): ")))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
346 (calendar-location-name
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
347 (if (< arg 16) calendar-location-name
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
348 (let ((float-output-format "%.1f"))
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
349 (format "%s%s, %s%s"
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
350 (abs calendar-latitude)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
351 (if (> calendar-latitude 0) "N" "S")
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
352 (abs calendar-longitude)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
353 (if (> calendar-longitude 0) "E" "W")))))
956
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
354 (calendar-standard-time-zone-name
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
355 (if (< arg 16) calendar-standard-time-zone-name
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
356 (cond ((= calendar-time-zone 0) "UT")
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
357 ((< calendar-time-zone 0)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
358 (format "UT%dmin" calendar-time-zone))
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
359 (t (format "UT+%dmin" calendar-time-zone)))))
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
360 (calendar-daylight-savings-starts
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
361 (if (< arg 16) calendar-daylight-savings-starts))
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
362 (calendar-daylight-savings-ends
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
363 (if (< arg 16) calendar-daylight-savings-ends))
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 956
diff changeset
364 (date (if (< arg 4) (calendar-current-date) (calendar-read-date)))
956
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
365 (date-string (calendar-date-string date t))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
366 (time-string (solar-sunrise-sunset date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
367 (msg (format "%s: %s" date-string time-string))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
368 (one-window (one-window-p t)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
369 (if (<= (length msg) (screen-width))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
370 (message msg)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
371 (with-output-to-temp-buffer "*temp*"
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
372 (princ (concat date-string "\n" time-string)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
373 (message (substitute-command-keys
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
374 (if one-window
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
375 (if pop-up-windows
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
376 "Type \\[delete-other-windows] to remove temp window."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
377 "Type \\[switch-to-buffer] RET to remove temp window.")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
378 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
379
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
380 (defun calendar-sunrise-sunset ()
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
381 "Local time of sunrise and sunset for date under cursor.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
382 Accurate to +/- 2 minutes."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
383 (interactive)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
384 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
385 (solar-setup))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
386 (message
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
387 (solar-sunrise-sunset
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
388 (or (calendar-cursor-to-date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
389 (error "Cursor is not on a date!")))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
390
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
391 (defun diary-sunrise-sunset ()
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
392 "Local time of sunrise and sunset as a diary entry.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
393 Accurate to +/- 2 minutes."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
394 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
395 (solar-setup))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
396 (solar-sunrise-sunset date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
397
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
398 (defun diary-sabbath-candles ()
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
399 "Local time of candle lighting diary entry--applies if date is a Friday.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
400 No diary entry if there is no sunset on that date."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
401 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
402 (solar-setup))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
403 (if (= (% (calendar-absolute-from-gregorian date) 7) 5);; Friday
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
404 (let* ((sunset (solar-sunset date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
405 (light (if sunset (- sunset (/ 18.0 60.0)))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
406 (if light (format "%s Sabbath candle lighting"
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
407 (solar-time-string light date))))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
408
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
409 (defun calendar-holiday-function-solar-equinoxes-solstices ()
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
410 "Date and time of equinoxes and solstices, if visible in the calendar window.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
411 Requires floating point."
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
412 (let* ((m displayed-month)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
413 (y displayed-year))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
414 (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
415 ((= 2 (% m 3)) 1)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
416 (t 0)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
417 (let* ((calendar-standard-time-zone-name
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
418 (if calendar-time-zone calendar-standard-time-zone-name "UT"))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
419 (calendar-daylight-savings-starts
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
420 (if calendar-time-zone calendar-daylight-savings-starts))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
421 (calendar-daylight-savings-ends
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
422 (if calendar-time-zone calendar-daylight-savings-ends))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
423 (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
424 (k (1- (/ m 3)))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
425 (date (solar-equinoxes/solstices k y))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
426 (day (extract-calendar-day date))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
427 (time (* 24 (- day (truncate day))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
428 ;; Time zone/DST can't move the date out of range,
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
429 ;; so let solar-time-string do the conversion.
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
430 (date (list (extract-calendar-month date)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
431 (truncate day)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
432 (extract-calendar-year date))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
433 (list (list date
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
434 (format "%s %s"
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
435 (cond ((= k 0) "Vernal Equinox")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
436 ((= k 1) "Summer Solstice")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
437 ((= k 2) "Fall Equinox")
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
438 ((= k 3) "Winter Solstice"))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
439 (solar-time-string time date)))))))
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
440
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
441 (provide 'solar)
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
442
c530dbc9a92a Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
443 ;;; solar.el ends here