1 ;;; solar.el --- calendar functions for solar events.
3 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary,
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; This collection of functions implements the features of calendar.el and
29 ;; diary.el that deal with times of day, sunrise/sunset, and
30 ;; eqinoxes/solstices.
32 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
33 ;; Almanac Office, United States Naval Observatory, Washington, 1984 and
34 ;; on ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
35 ;; Willmann-Bell, Inc., 1985.
38 ;; 1. SUNRISE/SUNSET calculations will be accurate only to +/- 2 minutes.
39 ;; Locations should be between +/- 65 degrees of latitude.
40 ;; Dates should be in the latter half of the 20th century.
42 ;; 2. Equinox/solstice times will be accurate only to +/- 15 minutes.
44 ;; The author would be delighted to have an astronomically more sophisticated
45 ;; person rewrite the code for the solar calculations in this file!
47 ;; Comments, corrections, and improvements should be sent to
48 ;; Edward M. Reingold Department of Computer Science
49 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
50 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
51 ;; Urbana, Illinois 61801
56 (require 'lisp-float-type
)
57 (error "Solar calculations impossible since floating point is unavailable."))
62 (defvar calendar-time-display-form
63 '(12-hours ":" minutes am-pm
64 (if time-zone
" (") time-zone
(if time-zone
")"))
65 "*The pseudo-pattern that governs the way a time of day is formatted.
67 A pseudo-pattern is a list of expressions that can involve the keywords
68 `12-hours', `24-hours', and `minutes', all numbers in string form,
69 and `am-pm' and `time-zone', both alphabetic strings.
73 '(24-hours \":\" minutes
74 (if time-zone \" (\") time-zone (if time-zone \")\"))
76 would give military-style times like `21:07 (UTC)'.")
79 (defvar calendar-latitude nil
80 "*Latitude of `calendar-location-name' in degrees, + north, - south.
81 For example, 40.7 for New York City.
82 It may not be a good idea to set this in advance for your site;
83 if there may be users running Emacs at your site
84 who are physically located elsewhere, they would get the wrong
85 value and might not know how to override it.")
88 (defvar calendar-longitude nil
89 "*Longitude of `calendar-location-name' in degrees, + east, - west.
90 For example, -74.0 for New York City.
91 It may not be a good idea to set this in advance for your site;
92 if there may be users running Emacs at your site
93 who are physically located elsewhere, they would get the wrong
94 value and might not know how to override it.")
97 (defvar calendar-location-name
98 '(let ((float-output-format "%.1f"))
100 (abs calendar-latitude
)
101 (if (> calendar-latitude
0) "N" "S")
102 (abs calendar-longitude
)
103 (if (> calendar-longitude
0) "E" "W")))
104 "*Expression evaluating to name of `calendar-longitude', calendar-latitude'.
105 Default value is just the latitude, longitude pair.")
107 (defvar solar-n-hemi-seasons
108 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
109 "List of season changes for the northern hemisphere.")
111 (defvar solar-s-hemi-seasons
112 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
113 "List of season changes for the southern hemisphere.")
115 (defun solar-setup ()
116 "Prompt user for latitude, longitude, and time zone."
118 (if (not calendar-longitude
)
119 (setq calendar-longitude
121 "Enter longitude (decimal fraction; + east, - west): ")))
122 (if (not calendar-latitude
)
123 (setq calendar-latitude
125 "Enter latitude (decimal fraction; + north, - south): ")))
126 (if (not calendar-time-zone
)
127 (setq calendar-time-zone
129 "Enter difference from Coordinated Universal Time (in minutes): "))))
131 (defun solar-get-number (prompt)
132 "Return a number from the minibuffer, prompting with PROMPT.
133 Returns nil if nothing was entered."
134 (let ((x (read-string prompt
"")))
135 (if (not (string-equal x
""))
138 (defun solar-sin-degrees (x)
139 (sin (degrees-to-radians x
)))
141 (defun solar-cosine-degrees (x)
142 (cos (degrees-to-radians x
)))
144 (defun solar-tangent-degrees (x)
145 (tan (degrees-to-radians x
)))
147 (defun solar-xy-to-quadrant (x y
)
148 "Determines the quadrant of the point X, Y."
153 (defun solar-degrees-to-quadrant (angle)
154 "Determines the quadrant of ANGLE."
155 (1+ (floor (mod angle
360) 90)))
157 (defun solar-arctan (x quad
)
158 "Arctangent of X in quadrant QUAD."
159 (let ((deg (radians-to-degrees (atan x
))))
160 (cond ((equal quad
2) (+ deg
180))
161 ((equal quad
3) (+ deg
180))
162 ((equal quad
4) (+ deg
360))
165 (defun solar-arccos (x)
166 (let ((y (sqrt (- 1 (* x x
)))))
167 (solar-arctan (/ y x
) (solar-xy-to-quadrant x y
))))
169 (defun solar-arcsin (y)
170 (let ((x (sqrt (- 1 (* y y
)))))
171 (solar-arctan (/ y x
) (solar-xy-to-quadrant x y
))))
173 (defconst solar-earth-inclination
23.441884
174 "Inclination of earth's equator to its solar orbit in degrees.")
176 (defconst solar-cos-inclination
(solar-cosine-degrees solar-earth-inclination
)
177 "Cosine of earth's inclination.")
179 (defconst solar-sin-inclination
(solar-sin-degrees solar-earth-inclination
)
180 "Sine of earth's inclination.")
182 (defconst solar-earth-orbit-eccentricity
0.016718
183 "Eccentricity of orbit of the earth around the sun.")
185 (defmacro solar-degrees-to-hours
(deg)
188 (defmacro solar-hours-to-days
(hour)
191 (defun solar-longitude-of-sun (day)
192 "Longitude of the sun at DAY in the year."
193 (let ((mean-anomaly (- (* 0.9856 day
) 3.289)))
195 (* 1.916 (solar-sin-degrees mean-anomaly
))
196 (* 0.020 (solar-sin-degrees (* 2 mean-anomaly
)))
200 (defun solar-right-ascension (longitude)
201 "Right ascension of the sun, given its LONGITUDE."
202 (solar-degrees-to-hours
204 (* solar-cos-inclination
(solar-tangent-degrees longitude
))
205 (solar-degrees-to-quadrant longitude
))))
207 (defun solar-declination (longitude)
208 "Declination of the sun, given its LONGITUDE."
210 (* solar-sin-inclination
211 (solar-sin-degrees longitude
))))
213 (defun solar-sunrise (date)
214 "Calculates the *standard* time of sunrise for Gregorian DATE for location
215 given by `calendar-latitude' and `calendar-longitude'. Returns a decimal fraction
216 of hours. Returns nil if the sun does not rise at that location on that day."
217 (let* ((day-of-year (calendar-day-number date
))
221 (- 6 (solar-degrees-to-hours calendar-longitude
)))))
222 (solar-longitude-of-sun-at-sunrise
223 (solar-longitude-of-sun approx-sunrise
))
224 (solar-right-ascension-at-sunrise
225 (solar-right-ascension solar-longitude-of-sun-at-sunrise
))
226 (solar-declination-at-sunrise
227 (solar-declination solar-longitude-of-sun-at-sunrise
))
229 (/ (- (solar-cosine-degrees (+ 90 (/ 50.0 60.0)))
230 (* (solar-sin-degrees solar-declination-at-sunrise
)
231 (solar-sin-degrees calendar-latitude
)))
232 (* (solar-cosine-degrees solar-declination-at-sunrise
)
233 (solar-cosine-degrees calendar-latitude
)))))
234 (if (<= (abs cos-local-sunrise
) 1);; otherwise, no sunrise that day
235 (let* ((local-sunrise (solar-degrees-to-hours
236 (- 360 (solar-arccos cos-local-sunrise
))))
238 (mod (- (+ local-sunrise solar-right-ascension-at-sunrise
)
239 (+ (* 0.065710 approx-sunrise
)
242 (+ (- local-mean-sunrise
(solar-degrees-to-hours calendar-longitude
))
243 (/ calendar-time-zone
60.0))))))
245 (defun solar-sunset (date)
246 "Calculates the *standard* time of sunset for Gregorian DATE for location
247 given by `calendar-latitude' and `calendar-longitude'. Returns a decimal fractions
248 of hours. Returns nil if the sun does not set at that location on that day."
249 (let* ((day-of-year (calendar-day-number date
))
253 (- 18 (solar-degrees-to-hours calendar-longitude
)))))
254 (solar-longitude-of-sun-at-sunset
255 (solar-longitude-of-sun approx-sunset
))
256 (solar-right-ascension-at-sunset
257 (solar-right-ascension solar-longitude-of-sun-at-sunset
))
258 (solar-declination-at-sunset
259 (solar-declination solar-longitude-of-sun-at-sunset
))
261 (/ (- (solar-cosine-degrees (+ 90 (/ 50.0 60.0)))
262 (* (solar-sin-degrees solar-declination-at-sunset
)
263 (solar-sin-degrees calendar-latitude
)))
264 (* (solar-cosine-degrees solar-declination-at-sunset
)
265 (solar-cosine-degrees calendar-latitude
)))))
266 (if (<= (abs cos-local-sunset
) 1);; otherwise, no sunset that day
267 (let* ((local-sunset (solar-degrees-to-hours
268 (solar-arccos cos-local-sunset
)))
270 (mod (- (+ local-sunset solar-right-ascension-at-sunset
)
271 (+ (* 0.065710 approx-sunset
) 6.622))
273 (+ (- local-mean-sunset
(solar-degrees-to-hours calendar-longitude
))
274 (/ calendar-time-zone
60.0))))))
276 (defun solar-time-string (time date
&optional style
)
277 "Printable form for decimal fraction *standard* TIME on DATE.
278 Optional parameter STYLE forces the time to be standard time when its value
279 is 'standard and daylight savings time (if available) when its value is
282 Format used is given by `calendar-time-display-form'. Converted to daylight
283 savings time according to `calendar-daylight-savings-starts',
284 `calendar-daylight-savings-ends', `calendar-daylight-savings-starts-time',
285 `calendar-daylight-savings-ends-time', and `calendar-daylight-savings-offset'."
286 (let* ((year (extract-calendar-year date
))
287 (time (round (* 60 time
)))
288 (rounded-abs-date (+ (calendar-absolute-from-gregorian date
)
290 (dst-starts (and calendar-daylight-savings-starts
291 (+ (calendar-absolute-from-gregorian
292 (eval calendar-daylight-savings-starts
))
293 (/ calendar-daylight-savings-starts-time
295 (dst-ends (and calendar-daylight-savings-ends
296 (+ (calendar-absolute-from-gregorian
297 (eval calendar-daylight-savings-ends
))
298 (/ (- calendar-daylight-savings-ends-time
299 calendar-daylight-time-offset
)
301 (dst (and (not (eq style
'standard
))
302 (or (eq style
'daylight
)
303 (and dst-starts dst-ends
304 (or (and (< dst-starts dst-ends
);; northern hemi.
305 (<= dst-starts rounded-abs-date
)
306 (< rounded-abs-date dst-ends
))
307 (and (< dst-ends dst-starts
);; southern hemi.
308 (or (< rounded-abs-date dst-ends
)
309 (<= dst-starts rounded-abs-date
)))))
310 (and dst-starts
(not dst-ends
)
311 (<= dst-starts rounded-abs-date
))
312 (and dst-ends
(not dst-starts
)
313 (< rounded-abs-date dst-ends
)))))
315 calendar-daylight-time-zone-name
316 calendar-standard-time-zone-name
))
317 (time (+ time
(if dst calendar-daylight-time-offset
0)))
318 (24-hours (/ time
60))
319 (minutes (format "%02d" (% time
60)))
320 (12-hours (format "%d" (1+ (%
(+ 24-hours
11) 12))))
321 (am-pm (if (>= 24-hours
12) "pm" "am"))
322 (24-hours (format "%02d" 24-hours
)))
323 (mapconcat 'eval calendar-time-display-form
"")))
325 (defun solar-sunrise-sunset (date)
326 "String giving local times of sunrise and sunset on Gregorian DATE."
327 (let ((rise (solar-sunrise date
))
328 (set (solar-sunset date
)))
329 (format "%s, %s at %s"
331 (concat "Sunrise " (solar-time-string rise date
))
334 (concat "sunset " (solar-time-string set date
))
336 (eval calendar-location-name
))))
338 (defun solar-apparent-longitude-of-sun (date)
339 "Apparent longitude of the sun on Gregorian DATE."
340 (let* ((time (/ (- (calendar-absolute-from-gregorian date
)
341 (calendar-absolute-from-gregorian '(1 0.5 1900)))
345 (* 0.0003025 time time
)))
348 (* -
0.000150 time time
)
349 (* -
0.0000033 time time time
)))
352 (* -
0.000014 time time
))
353 (solar-sin-degrees m
))
356 (solar-sin-degrees (* 2 m
)))
358 (solar-sin-degrees (* 3 m
)))))
365 (solar-sin-degrees omega
)))))
368 (defun solar-ephemeris-correction (year)
369 "Difference in minutes between Ephemeris time and UTC in YEAR.
370 Value is only an approximation."
371 (let ((T (/ (- year
1900) 100.0)))
372 (+ 0.41 (* 1.2053 T
) (* 0.4992 T T
))))
374 (defun solar-equinoxes/solstices
(k year
)
375 "Date of equinox/solstice K for YEAR. K=0, spring equinox; K=1, summer
376 solstice; K=2, fall equinox; K=3, winter solstice. Accurate to within
378 (let ((date (list (+ 3 (* k
3)) 21 year
))
381 (while (> correction
0.00001)
382 (setq app
(mod (solar-apparent-longitude-of-sun date
) 360))
383 (setq correction
(* 58 (solar-sin-degrees (- (* k
90) app
))))
384 (setq date
(list (extract-calendar-month date
)
385 (+ (extract-calendar-day date
) correction
)
387 (list (extract-calendar-month date
)
388 (+ (extract-calendar-day date
) (/ calendar-time-zone
60.0 24.0)
389 (- (/ (solar-ephemeris-correction year
) 60.0 24.0)))
393 (defun sunrise-sunset (&optional arg
)
394 "Local time of sunrise and sunset for today. Accurate to +/- 2 minutes.
395 If called with an optional prefix argument, prompts for date.
397 If called with an optional double prefix argument, prompts for longitude,
398 latitude, time zone, and date.
400 This function is suitable for execution in a .emacs file."
402 (or arg
(setq arg
1))
404 (not (and calendar-latitude calendar-longitude calendar-time-zone
)))
406 (let* ((calendar-longitude
407 (if (< arg
16) calendar-longitude
409 "Enter longitude (decimal fraction; + east, - west): ")))
411 (if (< arg
16) calendar-latitude
413 "Enter latitude (decimal fraction; + north, - south): ")))
415 (if (< arg
16) calendar-time-zone
417 "Enter difference from Coordinated Universal Time (in minutes): ")))
418 (calendar-location-name
419 (if (< arg
16) calendar-location-name
420 (let ((float-output-format "%.1f"))
422 (abs calendar-latitude
)
423 (if (> calendar-latitude
0) "N" "S")
424 (abs calendar-longitude
)
425 (if (> calendar-longitude
0) "E" "W")))))
426 (calendar-standard-time-zone-name
427 (if (< arg
16) calendar-standard-time-zone-name
428 (cond ((= calendar-time-zone
0) "UTC")
429 ((< calendar-time-zone
0)
430 (format "UTC%dmin" calendar-time-zone
))
431 (t (format "UTC+%dmin" calendar-time-zone
)))))
432 ;; Use outer context values always, unless you're going to prompt for
433 ;; the values to use. PKH
434 ;; (calendar-daylight-savings-starts
435 ;; (if (< arg 16) calendar-daylight-savings-starts))
436 ;; (calendar-daylight-savings-ends
437 ;; (if (< arg 16) calendar-daylight-savings-ends))
438 (date (if (< arg
4) (calendar-current-date) (calendar-read-date)))
439 (date-string (calendar-date-string date t
))
440 (time-string (solar-sunrise-sunset date
))
441 (msg (format "%s: %s" date-string time-string
))
442 (one-window (one-window-p t
)))
443 (if (<= (length msg
) (frame-width))
445 (with-output-to-temp-buffer "*temp*"
446 (princ (concat date-string
"\n" time-string
)))
447 (message (substitute-command-keys
450 "Type \\[delete-other-windows] to remove temp window."
451 "Type \\[switch-to-buffer] RET to remove temp window.")
452 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
454 (defun calendar-sunrise-sunset ()
455 "Local time of sunrise and sunset for date under cursor.
456 Accurate to +/- 2 minutes."
458 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
460 (let ((date (calendar-cursor-to-date t
)))
462 (calendar-date-string date t t
)
463 (solar-sunrise-sunset date
))))
465 (defun diary-sunrise-sunset ()
466 "Local time of sunrise and sunset as a diary entry.
467 Accurate to +/- 2 minutes."
468 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
470 (solar-sunrise-sunset date
))
472 (defun diary-sabbath-candles ()
473 "Local time of candle lighting diary entry--applies if date is a Friday.
474 No diary entry if there is no sunset on that date."
475 (if (not (and calendar-latitude calendar-longitude calendar-time-zone
))
477 (if (= (%
(calendar-absolute-from-gregorian date
) 7) 5);; Friday
478 (let* ((sunset (solar-sunset date
))
479 (light (if sunset
(- sunset
(/ 18.0 60.0)))))
480 (if light
(format "%s Sabbath candle lighting"
481 (solar-time-string light date
))))))
484 (defun solar-equinoxes-solstices ()
485 "Date and time of equinoxes and solstices, if visible in the calendar window.
486 Requires floating point."
487 (let ((m displayed-month
)
489 (increment-calendar-month m y
(cond ((= 1 (% m
3)) -
1)
492 (let* ((calendar-standard-time-zone-name
493 (if calendar-time-zone calendar-standard-time-zone-name
"UTC"))
494 (calendar-daylight-savings-starts
495 (if calendar-time-zone calendar-daylight-savings-starts
))
496 (calendar-daylight-savings-ends
497 (if calendar-time-zone calendar-daylight-savings-ends
))
498 (calendar-time-zone (if calendar-time-zone calendar-time-zone
0))
500 (date (solar-equinoxes/solstices k y
))
501 (day (extract-calendar-day date
))
502 (time (* 24 (- day
(truncate day
))))
503 (s-hemi (and calendar-latitude
(< calendar-latitude
0)))
504 ;; Time zone/DST can't move the date out of range,
505 ;; so let solar-time-string do the conversion.
506 (date (list (extract-calendar-month date
)
508 (extract-calendar-year date
))))
511 (nth k
(if s-hemi solar-s-hemi-seasons
512 solar-n-hemi-seasons
))
513 (solar-time-string time date
)))))))
517 ;;; solar.el ends here