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