(calendar-location-name, calendar-latitude)
[emacs.git] / lisp / calendar / solar.el
blob3bc5d091c03bc25028af3bb949b81f0731d6afcb
1 ;;; solar.el --- calendar functions for solar events
3 ;; Copyright (C) 1992, 1993, 1995, 1997, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Denis B. Roegel <Denis.Roegel@loria.fr>
8 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Keywords: calendar
10 ;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary, holidays
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; This collection of functions implements the features of calendar.el,
32 ;; diary.el, and holiday.el that deal with times of day, sunrise/sunset, and
33 ;; equinoxes/solstices.
35 ;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
36 ;; Almanac Office, United States Naval Observatory, Washington, 1984, on
37 ;; ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
38 ;; Willmann-Bell, Inc., 1985, on ``Astronomical Algorithms'' by Jean Meeus,
39 ;; Willmann-Bell, Inc., 1991, and on ``Planetary Programs and Tables from
40 ;; -4000 to +2800'' by Pierre Bretagnon and Jean-Louis Simon, Willmann-Bell,
41 ;; Inc., 1986.
44 ;; Accuracy:
45 ;; 1. Sunrise/sunset times will be accurate to the minute for years
46 ;; 1951--2050. For other years the times will be within +/- 2 minutes.
48 ;; 2. Equinox/solstice times will be accurate to the minute for years
49 ;; 1951--2050. For other years the times will be within +/- 1 minute.
51 ;; Technical details of all the calendrical calculations can be found in
52 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
53 ;; and Nachum Dershowitz, Cambridge University Press (2001).
55 ;;; Code:
57 (if (fboundp 'atan)
58 (require 'lisp-float-type)
59 (error "Solar calculations impossible since floating point is unavailable"))
61 (require 'cal-dst)
62 (require 'cal-julian)
65 (defcustom calendar-time-display-form
66 '(12-hours ":" minutes am-pm
67 (if time-zone " (") time-zone (if time-zone ")"))
68 "The pseudo-pattern that governs the way a time of day is formatted.
70 A pseudo-pattern is a list of expressions that can involve the keywords
71 `12-hours', `24-hours', and `minutes', all numbers in string form,
72 and `am-pm' and `time-zone', both alphabetic strings.
74 For example, the form
76 '(24-hours \":\" minutes
77 (if time-zone \" (\") time-zone (if time-zone \")\"))
79 would give military-style times like `21:07 (UTC)'."
80 :type 'sexp
81 :group 'calendar)
83 (defcustom calendar-latitude nil
84 "Latitude of `calendar-location-name' in degrees.
85 The value can be either a decimal fraction (one place of accuracy is
86 sufficient), + north, - south, such as 40.7 for New York City, or the value
87 can be a vector [degrees minutes north/south] such as [40 50 north] for New
88 York City.
90 This variable should be set in `site-start'.el."
91 :type '(choice (const nil)
92 (number :tag "Exact")
93 (vector :value [0 0 north]
94 (integer :tag "Degrees")
95 (integer :tag "Minutes")
96 (choice :tag "Position"
97 (const north)
98 (const south))))
99 :group 'calendar)
101 (defcustom calendar-longitude nil
102 "Longitude of `calendar-location-name' in degrees.
103 The value can be either a decimal fraction (one place of accuracy is
104 sufficient), + east, - west, such as -73.9 for New York City, or the value
105 can be a vector [degrees minutes east/west] such as [73 55 west] for New
106 York City.
108 This variable should be set in `site-start'.el."
109 :type '(choice (const nil)
110 (number :tag "Exact")
111 (vector :value [0 0 west]
112 (integer :tag "Degrees")
113 (integer :tag "Minutes")
114 (choice :tag "Position"
115 (const east)
116 (const west))))
117 :group 'calendar)
119 (defcustom calendar-location-name
120 '(let ((float-output-format "%.1f"))
121 (format "%s%s, %s%s"
122 (if (numberp calendar-latitude)
123 (abs calendar-latitude)
124 (+ (aref calendar-latitude 0)
125 (/ (aref calendar-latitude 1) 60.0)))
126 (if (numberp calendar-latitude)
127 (if (> calendar-latitude 0) "N" "S")
128 (if (eq (aref calendar-latitude 2) 'north) "N" "S"))
129 (if (numberp calendar-longitude)
130 (abs calendar-longitude)
131 (+ (aref calendar-longitude 0)
132 (/ (aref calendar-longitude 1) 60.0)))
133 (if (numberp calendar-longitude)
134 (if (> calendar-longitude 0) "E" "W")
135 (if (eq (aref calendar-longitude 2) 'east) "E" "W"))))
136 "Expression evaluating to the name of the calendar location.
137 For example, \"New York City\". The default value is just the
138 variable `calendar-latitude' paired with the variable `calendar-longitude'.
140 This variable should be set in `site-start'.el."
141 :type 'sexp
142 :group 'calendar)
144 (defcustom solar-error 0.5
145 "Tolerance (in minutes) for sunrise/sunset calculations.
147 A larger value makes the calculations for sunrise/sunset faster, but less
148 accurate. The default is half a minute (30 seconds), so that sunrise/sunset
149 times will be correct to the minute.
151 It is useless to set the value smaller than 4*delta, where delta is the
152 accuracy in the longitude of the sun (given by the function
153 `solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
154 delta. At present, delta = 0.01 degrees, so the value of the variable
155 `solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
156 :type 'number
157 :group 'calendar)
159 (defcustom diary-sabbath-candles-minutes 18
160 "Number of minutes before sunset for sabbath candle lighting."
161 :group 'diary
162 :type 'integer
163 :version "21.1")
166 ;;; End of user options.
169 (defconst solar-n-hemi-seasons
170 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
171 "List of season changes for the northern hemisphere.")
173 (defconst solar-s-hemi-seasons
174 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
175 "List of season changes for the southern hemisphere.")
177 (defvar solar-sidereal-time-greenwich-midnight nil
178 "Sidereal time at Greenwich at midnight (universal time).")
180 (defvar solar-northern-spring-or-summer-season nil
181 "Non-nil if northern spring or summer and nil otherwise.
182 Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
185 (defsubst calendar-latitude ()
186 "Ensure the variable `calendar-latitude' is a signed decimal fraction."
187 (if (numberp calendar-latitude)
188 calendar-latitude
189 (let ((lat (+ (aref calendar-latitude 0)
190 (/ (aref calendar-latitude 1) 60.0))))
191 (if (eq (aref calendar-latitude 2) 'north)
193 (- lat)))))
195 (defsubst calendar-longitude ()
196 "Ensure the variable `calendar-longitude' is a signed decimal fraction."
197 (if (numberp calendar-longitude)
198 calendar-longitude
199 (let ((long (+ (aref calendar-longitude 0)
200 (/ (aref calendar-longitude 1) 60.0))))
201 (if (eq (aref calendar-longitude 2) 'east)
202 long
203 (- long)))))
205 (defun solar-get-number (prompt)
206 "Return a number from the minibuffer, prompting with PROMPT.
207 Returns nil if nothing was entered."
208 (let ((x (read-string prompt "")))
209 (unless (string-equal x "")
210 (string-to-number x))))
212 (defun solar-setup ()
213 "Prompt for `calendar-longitude', `calendar-latitude', `calendar-time-zone'."
214 (beep)
215 (or calendar-longitude
216 (setq calendar-longitude
217 (solar-get-number
218 "Enter longitude (decimal fraction; + east, - west): ")))
219 (or calendar-latitude
220 (setq calendar-latitude
221 (solar-get-number
222 "Enter latitude (decimal fraction; + north, - south): ")))
223 (or calendar-time-zone
224 (setq calendar-time-zone
225 (solar-get-number
226 "Enter difference from Coordinated Universal Time (in minutes): ")
229 (defun solar-sin-degrees (x)
230 "Return sin of X degrees."
231 (sin (degrees-to-radians (mod x 360.0))))
233 (defun solar-cosine-degrees (x)
234 "Return cosine of X degrees."
235 (cos (degrees-to-radians (mod x 360.0))))
237 (defun solar-tangent-degrees (x)
238 "Return tangent of X degrees."
239 (tan (degrees-to-radians (mod x 360.0))))
241 (defun solar-xy-to-quadrant (x y)
242 "Determine the quadrant of the point X, Y."
243 (if (> x 0)
244 (if (> y 0) 1 4)
245 (if (> y 0) 2 3)))
247 (defun solar-degrees-to-quadrant (angle)
248 "Determine the quadrant of ANGLE degrees."
249 (1+ (floor (mod angle 360) 90)))
251 (defun solar-arctan (x quad)
252 "Arctangent of X in quadrant QUAD."
253 (let ((deg (radians-to-degrees (atan x))))
254 (cond ((= quad 2) (+ deg 180))
255 ((= quad 3) (+ deg 180))
256 ((= quad 4) (+ deg 360))
257 (t deg))))
259 (defun solar-atn2 (x y)
260 "Arctangent of point X, Y."
261 (if (zerop x)
262 (if (> y 0) 90 270)
263 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
265 (defun solar-arccos (x)
266 "Arccosine of X."
267 (let ((y (sqrt (- 1 (* x x)))))
268 (solar-atn2 x y)))
270 (defun solar-arcsin (y)
271 "Arcsin of Y."
272 (let ((x (sqrt (- 1 (* y y)))))
273 (solar-atn2 x y)))
275 (defsubst solar-degrees-to-hours (degrees)
276 "Convert DEGREES to hours."
277 (/ degrees 15.0))
279 (defsubst solar-hours-to-days (hour)
280 "Convert HOUR to decimal fraction of a day."
281 (/ hour 24.0))
283 (defun solar-right-ascension (longitude obliquity)
284 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
285 Both arguments are in degrees."
286 (solar-degrees-to-hours
287 (solar-arctan
288 (* (solar-cosine-degrees obliquity) (solar-tangent-degrees longitude))
289 (solar-degrees-to-quadrant longitude))))
291 (defun solar-declination (longitude obliquity)
292 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
293 Both arguments are in degrees."
294 (solar-arcsin
295 (* (solar-sin-degrees obliquity)
296 (solar-sin-degrees longitude))))
298 (defun solar-ecliptic-coordinates (time sunrise-flag)
299 "Return solar longitude, ecliptic inclination, equation of time, nutation.
300 Values are for TIME in Julian centuries of Ephemeris Time since
301 January 1st, 2000, at 12 ET. Longitude and inclination are in
302 degrees, equation of time in hours, and nutation in seconds of longitude.
303 If SUNRISE-FLAG is non-nil, only calculate longitude and inclination."
304 (let* ((l (+ 280.46645
305 (* 36000.76983 time)
306 (* 0.0003032 time time))) ; sun mean longitude
307 (ml (+ 218.3165
308 (* 481267.8813 time))) ; moon mean longitude
309 (m (+ 357.52910
310 (* 35999.05030 time)
311 (* -0.0001559 time time)
312 (* -0.00000048 time time time))) ; sun mean anomaly
313 (i (+ 23.43929111 (* -0.013004167 time)
314 (* -0.00000016389 time time)
315 (* 0.0000005036 time time time))) ; mean inclination
316 (c (+ (* (+ 1.914600
317 (* -0.004817 time)
318 (* -0.000014 time time))
319 (solar-sin-degrees m))
320 (* (+ 0.019993 (* -0.000101 time))
321 (solar-sin-degrees (* 2 m)))
322 (* 0.000290
323 (solar-sin-degrees (* 3 m))))) ; center equation
324 (L (+ l c)) ; total longitude
325 ;; Longitude of moon's ascending node on the ecliptic.
326 (omega (+ 125.04
327 (* -1934.136 time)))
328 ;; nut = nutation in longitude, measured in seconds of angle.
329 (nut (unless sunrise-flag
330 (+ (* -17.20 (solar-sin-degrees omega))
331 (* -1.32 (solar-sin-degrees (* 2 l)))
332 (* -0.23 (solar-sin-degrees (* 2 ml)))
333 (* 0.21 (solar-sin-degrees (* 2 omega))))))
334 (ecc (unless sunrise-flag ; eccentricity of earth's orbit
335 (+ 0.016708617
336 (* -0.000042037 time)
337 (* -0.0000001236 time time))))
338 (app (+ L ; apparent longitude of sun
339 -0.00569
340 (* -0.00478
341 (solar-sin-degrees omega))))
342 (y (unless sunrise-flag
343 (* (solar-tangent-degrees (/ i 2))
344 (solar-tangent-degrees (/ i 2)))))
345 ;; Equation of time, in hours.
346 (time-eq (unless sunrise-flag
347 (/ (* 12 (+ (* y (solar-sin-degrees (* 2 l)))
348 (* -2 ecc (solar-sin-degrees m))
349 (* 4 ecc y (solar-sin-degrees m)
350 (solar-cosine-degrees (* 2 l)))
351 (* -0.5 y y (solar-sin-degrees (* 4 l)))
352 (* -1.25 ecc ecc (solar-sin-degrees (* 2 m)))))
353 3.1415926535))))
354 (list app i time-eq nut)))
356 (defun solar-ephemeris-correction (year)
357 "Ephemeris time minus Universal Time during Gregorian YEAR.
358 Result is in days. For the years 1800-1987, the maximum error is
359 1.9 seconds. For the other years, the maximum error is about 30 seconds."
360 (cond ((and (<= 1988 year) (< year 2020))
361 (/ (+ year -2000 67.0) 60.0 60.0 24.0))
362 ((and (<= 1900 year) (< year 1988))
363 (let* ((theta (/ (- (calendar-astro-from-absolute
364 (calendar-absolute-from-gregorian
365 (list 7 1 year)))
366 (calendar-astro-from-absolute
367 (calendar-absolute-from-gregorian
368 '(1 1 1900))))
369 36525.0))
370 (theta2 (* theta theta))
371 (theta3 (* theta2 theta))
372 (theta4 (* theta2 theta2))
373 (theta5 (* theta3 theta2)))
374 (+ -0.00002
375 (* 0.000297 theta)
376 (* 0.025184 theta2)
377 (* -0.181133 theta3)
378 (* 0.553040 theta4)
379 (* -0.861938 theta5)
380 (* 0.677066 theta3 theta3)
381 (* -0.212591 theta4 theta3))))
382 ((and (<= 1800 year) (< year 1900))
383 (let* ((theta (/ (- (calendar-astro-from-absolute
384 (calendar-absolute-from-gregorian
385 (list 7 1 year)))
386 (calendar-astro-from-absolute
387 (calendar-absolute-from-gregorian
388 '(1 1 1900))))
389 36525.0))
390 (theta2 (* theta theta))
391 (theta3 (* theta2 theta))
392 (theta4 (* theta2 theta2))
393 (theta5 (* theta3 theta2)))
394 (+ -0.000009
395 (* 0.003844 theta)
396 (* 0.083563 theta2)
397 (* 0.865736 theta3)
398 (* 4.867575 theta4)
399 (* 15.845535 theta5)
400 (* 31.332267 theta3 theta3)
401 (* 38.291999 theta4 theta3)
402 (* 28.316289 theta4 theta4)
403 (* 11.636204 theta4 theta5)
404 (* 2.043794 theta5 theta5))))
405 ((and (<= 1620 year) (< year 1800))
406 (let ((x (/ (- year 1600) 10.0)))
407 (/ (+ (* 2.19167 x x) (* -40.675 x) 196.58333) 60.0 60.0 24.0)))
408 (t (let* ((tmp (- (calendar-astro-from-absolute
409 (calendar-absolute-from-gregorian
410 (list 1 1 year)))
411 2382148))
412 (second (- (/ (* tmp tmp) 41048480.0) 15)))
413 (/ second 60.0 60.0 24.0)))))
415 (defun solar-ephemeris-time (time)
416 "Ephemeris Time at moment TIME.
417 TIME is a pair with the first component being the number of Julian centuries
418 elapsed at 0 Universal Time, and the second component being the universal
419 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
420 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
421 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
423 Result is in Julian centuries of ephemeris time."
424 (let* ((t0 (car time))
425 (ut (cadr time))
426 (t1 (+ t0 (/ (/ ut 24.0) 36525)))
427 (y (+ 2000 (* 100 t1)))
428 (dt (* 86400 (solar-ephemeris-correction (floor y)))))
429 (+ t1 (/ (/ dt 86400) 36525))))
431 (defun solar-equatorial-coordinates (time sunrise-flag)
432 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
433 TIME is a pair with the first component being the number of
434 Julian centuries elapsed at 0 Universal Time, and the second
435 component being the universal time. For instance, the pair
436 corresponding to November 28, 1995 at 16 UT is (-0.040945 16),
437 -0.040945 being the number of Julian centuries elapsed between
438 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT. SUNRISE-FLAG is passed
439 to `solar-ecliptic-coordinates'."
440 (let ((ec (solar-ecliptic-coordinates (solar-ephemeris-time time)
441 sunrise-flag)))
442 (list (solar-right-ascension (car ec) (cadr ec))
443 (solar-declination (car ec) (cadr ec)))))
445 (defun solar-horizontal-coordinates (time latitude longitude sunrise-flag)
446 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
447 TIME is a pair with the first component being the number of
448 Julian centuries elapsed at 0 Universal Time, and the second
449 component being the universal time. For instance, the pair
450 corresponding to November 28, 1995 at 16 UT is (-0.040945 16),
451 -0.040945 being the number of Julian centuries elapsed between
452 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT. SUNRISE-FLAG
453 is passed to `solar-ecliptic-coordinates'. Azimuth and
454 height (between -180 and 180) are both in degrees."
455 (let* ((ut (cadr time))
456 (ec (solar-equatorial-coordinates time sunrise-flag))
457 (st (+ solar-sidereal-time-greenwich-midnight
458 (* ut 1.00273790935)))
459 ;; Hour angle (in degrees).
460 (ah (- (* st 15) (* 15 (car ec)) (* -1 (calendar-longitude))))
461 (de (cadr ec))
462 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah)
463 (solar-sin-degrees latitude))
464 (* (solar-tangent-degrees de)
465 (solar-cosine-degrees latitude)))
466 (solar-sin-degrees ah)))
467 (height (solar-arcsin
468 (+ (* (solar-sin-degrees latitude) (solar-sin-degrees de))
469 (* (solar-cosine-degrees latitude)
470 (solar-cosine-degrees de)
471 (solar-cosine-degrees ah))))))
472 (if (> height 180) (setq height (- height 360)))
473 (list azimuth height)))
475 (defun solar-moment (direction latitude longitude time height)
476 "Sunrise/sunset at location.
477 Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
478 being TIME.
480 TIME is a pair with the first component being the number of Julian centuries
481 elapsed at 0 Universal Time, and the second component being the universal
482 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
483 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
484 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
486 HEIGHT is the angle the center of the sun has over the horizon for the contact
487 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
488 accounting for the edge of the sun being on the horizon.
490 Uses binary search."
491 (let* ((ut (cadr time))
492 (possible t) ; we assume that rise or set are possible
493 (utmin (+ ut (* direction 12.0)))
494 (utmax ut) ; the time searched is between utmin and utmax
495 ;; utmin and utmax are in hours.
496 (utmoment-old 0.0) ; rise or set approximation
497 (utmoment 1.0) ; rise or set approximation
498 (hut 0) ; sun height at utmoment
499 (t0 (car time))
500 (hmin (cadr (solar-horizontal-coordinates (list t0 utmin)
501 latitude longitude t)))
502 (hmax (cadr (solar-horizontal-coordinates (list t0 utmax)
503 latitude longitude t))))
504 ;; -0.61 degrees is the height of the middle of the sun, when it
505 ;; rises or sets.
506 (if (< hmin height)
507 (if (> hmax height)
508 (while ;;; (< i 20) ; we perform a simple dichotomy
509 ;;; (> (abs (- hut height)) epsilon)
510 (>= (abs (- utmoment utmoment-old))
511 (/ solar-error 60))
512 (setq utmoment-old utmoment
513 utmoment (/ (+ utmin utmax) 2)
514 hut (cadr (solar-horizontal-coordinates
515 (list t0 utmoment) latitude longitude t)))
516 (if (< hut height) (setq utmin utmoment))
517 (if (> hut height) (setq utmax utmoment)))
518 (setq possible nil)) ; the sun never rises
519 (setq possible nil)) ; the sun never sets
520 (if possible utmoment)))
522 (defun solar-sunrise-and-sunset (time latitude longitude height)
523 "Sunrise, sunset and length of day.
524 Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
526 TIME is a pair with the first component being the number of Julian centuries
527 elapsed at 0 Universal Time, and the second component being the universal
528 time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
529 \(-0.040945 16), -0.040945 being the number of Julian centuries elapsed between
530 Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
532 HEIGHT is the angle the center of the sun has over the horizon for the contact
533 we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
534 accounting for the edge of the sun being on the horizon.
536 Coordinates are included because this function is called with latitude=1
537 degrees to find out if polar regions have 24 hours of sun or only night."
538 (let ((rise-time (solar-moment -1 latitude longitude time height))
539 (set-time (solar-moment 1 latitude longitude time height))
540 day-length)
541 (if (not (and rise-time set-time))
542 (if (or (and (> latitude 0)
543 solar-northern-spring-or-summer-season)
544 (and (< latitude 0)
545 (not solar-northern-spring-or-summer-season)))
546 (setq day-length 24)
547 (setq day-length 0))
548 (setq day-length (- set-time rise-time)))
549 (list (if rise-time (+ rise-time (/ calendar-time-zone 60.0)) nil)
550 (if set-time (+ set-time (/ calendar-time-zone 60.0)) nil)
551 day-length)))
553 (defun solar-time-string (time time-zone)
554 "Printable form for decimal fraction TIME in TIME-ZONE.
555 Format used is given by `calendar-time-display-form'."
556 (let* ((time (round (* 60 time)))
557 (24-hours (/ time 60))
558 (minutes (format "%02d" (% time 60)))
559 (12-hours (format "%d" (1+ (% (+ 24-hours 11) 12))))
560 (am-pm (if (>= 24-hours 12) "pm" "am"))
561 (24-hours (format "%02d" 24-hours)))
562 (mapconcat 'eval calendar-time-display-form "")))
564 (defun solar-daylight (time)
565 "Printable form for TIME expressed in hours."
566 (format "%d:%02d"
567 (floor time)
568 (floor (* 60 (- time (floor time))))))
570 (defun solar-julian-ut-centuries (date)
571 "Number of Julian centuries since 1 Jan, 2000 at noon UT for Gregorian DATE."
572 (/ (- (calendar-absolute-from-gregorian date)
573 (calendar-absolute-from-gregorian '(1 1.5 2000)))
574 36525.0))
576 (defun solar-date-to-et (date ut)
577 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
578 Expressed in Julian centuries of Ephemeris Time."
579 (solar-ephemeris-time (list (solar-julian-ut-centuries date) ut)))
581 (defun solar-time-equation (date ut)
582 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
583 (nth 2 (solar-ecliptic-coordinates (solar-date-to-et date ut) nil)))
585 (defun solar-exact-local-noon (date)
586 "Date and Universal Time of local noon at *local date* DATE.
587 The date may be different from the one asked for, but it will be the right
588 local date. The second component of date should be an integer."
589 (let* ((nd date)
590 (ut (- 12.0 (/ (calendar-longitude) 15)))
591 (te (solar-time-equation date ut)))
592 (setq ut (- ut te))
593 (if (>= ut 24)
594 (setq nd (list (car date) (1+ (cadr date))
595 (nth 2 date))
596 ut (- ut 24)))
597 (if (< ut 0)
598 (setq nd (list (car date) (1- (cadr date))
599 (nth 2 date))
600 ut (+ ut 24)))
601 (setq nd (calendar-gregorian-from-absolute ; date standardization
602 (calendar-absolute-from-gregorian nd)))
603 (list nd ut)))
605 (defun solar-sidereal-time (t0)
606 "Sidereal time (in hours) in Greenwich at T0 Julian centuries.
607 T0 must correspond to 0 hours UT."
608 (let* ((mean-sid-time (+ 6.6973746
609 (* 2400.051337 t0)
610 (* 0.0000258622 t0 t0)
611 (* -0.0000000017222 t0 t0 t0)))
612 (et (solar-ephemeris-time (list t0 0.0)))
613 (nut-i (solar-ecliptic-coordinates et nil))
614 (nut (nth 3 nut-i)) ; nutation
615 (i (cadr nut-i))) ; inclination
616 (mod (+ (mod (+ mean-sid-time
617 (/ (/ (* nut (solar-cosine-degrees i)) 15) 3600)) 24.0)
618 24.0)
619 24.0)))
621 (defun solar-sunrise-sunset (date)
622 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
623 Corresponding value is nil if there is no sunrise/sunset."
624 ;; First, get the exact moment of local noon.
625 (let* ((exact-local-noon (solar-exact-local-noon date))
626 ;; Get the time from the 2000 epoch.
627 (t0 (solar-julian-ut-centuries (car exact-local-noon)))
628 ;; Store the sidereal time at Greenwich at midnight of UT time.
629 ;; Find if summer or winter slightly above the equator.
630 (equator-rise-set
631 (progn (setq solar-sidereal-time-greenwich-midnight
632 (solar-sidereal-time t0))
633 (solar-sunrise-and-sunset
634 (list t0 (cadr exact-local-noon))
636 (calendar-longitude) 0)))
637 ;; Store the spring/summer information, compute sunrise and
638 ;; sunset (two first components of rise-set). Length of day
639 ;; is the third component (it is only the difference between
640 ;; sunset and sunrise when there is a sunset and a sunrise)
641 (rise-set
642 (progn
643 (setq solar-northern-spring-or-summer-season
644 (> (nth 2 equator-rise-set) 12))
645 (solar-sunrise-and-sunset
646 (list t0 (cadr exact-local-noon))
647 (calendar-latitude)
648 (calendar-longitude) -0.61)))
649 (rise-time (car rise-set))
650 (adj-rise (if rise-time (dst-adjust-time date rise-time)))
651 (set-time (cadr rise-set))
652 (adj-set (if set-time (dst-adjust-time date set-time)))
653 (length (nth 2 rise-set)))
654 (list
655 (and rise-time (calendar-date-equal date (car adj-rise)) (cdr adj-rise))
656 (and set-time (calendar-date-equal date (car adj-set)) (cdr adj-set))
657 (solar-daylight length))))
659 (defun solar-sunrise-sunset-string (date)
660 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
661 (let ((l (solar-sunrise-sunset date)))
662 (format
663 "%s, %s at %s (%s hours daylight)"
664 (if (car l)
665 (concat "Sunrise " (apply 'solar-time-string (car l)))
666 "No sunrise")
667 (if (cadr l)
668 (concat "sunset " (apply 'solar-time-string (cadr l)))
669 "no sunset")
670 (eval calendar-location-name)
671 (nth 2 l))))
673 (defconst solar-data-list
674 '((403406 4.721964 1.621043)
675 (195207 5.937458 62830.348067)
676 (119433 1.115589 62830.821524)
677 (112392 5.781616 62829.634302)
678 (3891 5.5474 125660.5691)
679 (2819 1.5120 125660.984)
680 (1721 4.1897 62832.4766)
681 (0 1.163 0.813)
682 (660 5.415 125659.31)
683 (350 4.315 57533.85)
684 (334 4.553 -33.931)
685 (314 5.198 777137.715)
686 (268 5.989 78604.191)
687 (242 2.911 5.412)
688 (234 1.423 39302.098)
689 (158 0.061 -34.861)
690 (132 2.317 115067.698)
691 (129 3.193 15774.337)
692 (114 2.828 5296.670)
693 (99 0.52 58849.27)
694 (93 4.65 5296.11)
695 (86 4.35 -3980.70)
696 (78 2.75 52237.69)
697 (72 4.50 55076.47)
698 (68 3.23 261.08)
699 (64 1.22 15773.85)
700 (46 0.14 188491.03)
701 (38 3.44 -7756.55)
702 (37 4.37 264.89)
703 (32 1.14 117906.27)
704 (29 2.84 55075.75)
705 (28 5.96 -7961.39)
706 (27 5.09 188489.81)
707 (27 1.72 2132.19)
708 (25 2.56 109771.03)
709 (24 1.92 54868.56)
710 (21 0.09 25443.93)
711 (21 5.98 -55731.43)
712 (20 4.03 60697.74)
713 (18 4.47 2132.79)
714 (17 0.79 109771.63)
715 (14 4.24 -7752.82)
716 (13 2.01 188491.91)
717 (13 2.65 207.81)
718 (13 4.98 29424.63)
719 (12 0.93 -7.99)
720 (10 2.21 46941.14)
721 (10 3.59 -68.29)
722 (10 1.50 21463.25)
723 (10 2.55 157208.40))
724 "Data used for calculation of solar longitude.")
726 (defun solar-longitude (d)
727 "Longitude of sun on astronomical (Julian) day number D.
728 Accuracy is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
729 The values of `calendar-daylight-savings-starts',
730 `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
731 `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset', and
732 `calendar-time-zone' are used to interpret local time."
733 (let* ((a-d (calendar-absolute-from-astro d))
734 ;; Get Universal Time.
735 (date (calendar-astro-from-absolute
736 (- a-d
737 (if (dst-in-effect a-d)
738 (/ calendar-daylight-time-offset 24.0 60.0) 0)
739 (/ calendar-time-zone 60.0 24.0))))
740 ;; Get Ephemeris Time.
741 (date (+ date (solar-ephemeris-correction
742 (extract-calendar-year
743 (calendar-gregorian-from-absolute
744 (floor
745 (calendar-absolute-from-astro
746 date)))))))
747 (U (/ (- date 2451545) 3652500))
748 (longitude
749 (+ 4.9353929
750 (* 62833.1961680 U)
751 (* 0.0000001
752 (apply '+
753 (mapcar (lambda (x)
754 (* (car x)
755 (sin (mod
756 (+ (cadr x)
757 (* (nth 2 x) U))
758 (* 2 pi)))))
759 solar-data-list)))))
760 (aberration
761 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U)))) 973)))
762 (A1 (mod (+ 2.18 (* U (+ -3375.70 (* 0.36 U)))) (* 2 pi)))
763 (A2 (mod (+ 3.51 (* U (+ 125666.39 (* 0.10 U)))) (* 2 pi)))
764 (nutation (* -0.0000001 (+ (* 834 (sin A1)) (* 64 (sin A2))))))
765 (mod (radians-to-degrees (+ longitude aberration nutation)) 360.0)))
767 (defun solar-date-next-longitude (d l)
768 "First time after day D when solar longitude is a multiple of L degrees.
769 D is a Julian day number. L must be an integer divisor of 360.
770 The result is for `calendar-location-name', and is in local time
771 \(including any daylight saving rules) expressed in astronomical (Julian)
772 day numbers. The values of `calendar-daylight-savings-starts',
773 `calendar-daylight-savings-starts-time', `calendar-daylight-savings-ends',
774 `calendar-daylight-savings-ends-time', `calendar-daylight-time-offset',
775 and `calendar-time-zone' are used to interpret local time."
776 (let* ((long)
777 (start d)
778 (start-long (solar-longitude d))
779 (next (mod (* l (1+ (floor (/ start-long l)))) 360))
780 (end (+ d (* (/ l 360.0) 400)))
781 (end-long (solar-longitude end)))
782 (while ; bisection search for nearest minute
783 (< 0.00001 (- end start))
784 ;; start <= d < end
785 ;; start-long <= next < end-long when next != 0
786 ;; when next = 0, we look for the discontinuity (start-long is near 360
787 ;; and end-long is small (less than l).
788 (setq d (/ (+ start end) 2.0)
789 long (solar-longitude d))
790 (if (or (and (not (zerop next)) (< long next))
791 (and (zerop next) (< l long)))
792 (setq start d
793 start-long long)
794 (setq end d
795 end-long long)))
796 (/ (+ start end) 2.0)))
798 ;;;###autoload
799 (defun sunrise-sunset (&optional arg)
800 "Local time of sunrise and sunset for today. Accurate to a few seconds.
801 If called with an optional prefix argument ARG, prompt for date.
802 If called with an optional double prefix argument, prompt for
803 longitude, latitude, time zone, and date, and always use standard time.
805 This function is suitable for execution in a .emacs file."
806 (interactive "p")
807 (or arg (setq arg 1))
808 (if (and (< arg 16)
809 (not (and calendar-latitude calendar-longitude calendar-time-zone)))
810 (solar-setup))
811 (let* ((calendar-longitude
812 (if (< arg 16) calendar-longitude
813 (solar-get-number
814 "Enter longitude (decimal fraction; + east, - west): ")))
815 (calendar-latitude
816 (if (< arg 16) calendar-latitude
817 (solar-get-number
818 "Enter latitude (decimal fraction; + north, - south): ")))
819 (calendar-time-zone
820 (if (< arg 16) calendar-time-zone
821 (solar-get-number
822 "Enter difference from Coordinated Universal Time (in minutes): ")))
823 (calendar-location-name
824 (if (< arg 16) calendar-location-name
825 (let ((float-output-format "%.1f"))
826 (format "%s%s, %s%s"
827 (if (numberp calendar-latitude)
828 (abs calendar-latitude)
829 (+ (aref calendar-latitude 0)
830 (/ (aref calendar-latitude 1) 60.0)))
831 (if (numberp calendar-latitude)
832 (if (> calendar-latitude 0) "N" "S")
833 (if (eq (aref calendar-latitude 2) 'north) "N" "S"))
834 (if (numberp calendar-longitude)
835 (abs calendar-longitude)
836 (+ (aref calendar-longitude 0)
837 (/ (aref calendar-longitude 1) 60.0)))
838 (if (numberp calendar-longitude)
839 (if (> calendar-longitude 0) "E" "W")
840 (if (eq (aref calendar-longitude 2) 'east)
841 "E" "W"))))))
842 (calendar-standard-time-zone-name
843 (if (< arg 16) calendar-standard-time-zone-name
844 (cond ((zerop calendar-time-zone) "UTC")
845 ((< calendar-time-zone 0)
846 (format "UTC%dmin" calendar-time-zone))
847 (t (format "UTC+%dmin" calendar-time-zone)))))
848 (calendar-daylight-savings-starts
849 (if (< arg 16) calendar-daylight-savings-starts))
850 (calendar-daylight-savings-ends
851 (if (< arg 16) calendar-daylight-savings-ends))
852 (date (if (< arg 4) (calendar-current-date) (calendar-read-date)))
853 (date-string (calendar-date-string date t))
854 (time-string (solar-sunrise-sunset-string date))
855 (msg (format "%s: %s" date-string time-string))
856 (one-window (one-window-p t)))
857 (if (<= (length msg) (frame-width))
858 (message "%s" msg)
859 (with-output-to-temp-buffer "*temp*"
860 (princ (concat date-string "\n" time-string)))
861 (message "%s"
862 (substitute-command-keys
863 (if one-window
864 (if pop-up-windows
865 "Type \\[delete-other-windows] to remove temp window."
866 "Type \\[switch-to-buffer] RET to remove temp window.")
867 "Type \\[switch-to-buffer-other-window] RET to restore old \
868 contents of temp window."))))))
870 ;;;###cal-autoload
871 (defun calendar-sunrise-sunset ()
872 "Local time of sunrise and sunset for date under cursor.
873 Accurate to a few seconds."
874 (interactive)
875 (or (and calendar-latitude calendar-longitude calendar-time-zone)
876 (solar-setup))
877 (let ((date (calendar-cursor-to-date t)))
878 (message "%s: %s"
879 (calendar-date-string date t t)
880 (solar-sunrise-sunset-string date))))
882 (defvar date)
884 ;; To be called from list-sexp-diary-entries, where DATE is bound.
885 ;;;###diary-autoload
886 (defun diary-sunrise-sunset ()
887 "Local time of sunrise and sunset as a diary entry.
888 Accurate to a few seconds."
889 (or (and calendar-latitude calendar-longitude calendar-time-zone)
890 (solar-setup))
891 (solar-sunrise-sunset-string date))
893 ;; To be called from list-sexp-diary-entries, where DATE is bound.
894 ;;;###diary-autoload
895 (defun diary-sabbath-candles (&optional mark)
896 "Local time of candle lighting diary entry--applies if date is a Friday.
897 No diary entry if there is no sunset on that date.
899 An optional parameter MARK specifies a face or single-character string to
900 use when highlighting the day in the calendar."
901 (or (and calendar-latitude calendar-longitude calendar-time-zone)
902 (solar-setup))
903 (if (= (% (calendar-absolute-from-gregorian date) 7) 5) ; Friday
904 (let* ((sunset (cadr (solar-sunrise-sunset date)))
905 (light (if sunset
906 (cons (- (car sunset)
907 (/ diary-sabbath-candles-minutes 60.0))
908 (cdr sunset)))))
909 (if sunset
910 (cons mark
911 (format "%s Sabbath candle lighting"
912 (apply 'solar-time-string light)))))))
914 ;; From Meeus, 1991, page 167.
915 (defconst solar-seasons-data
916 '((485 324.96 1934.136)
917 (203 337.23 32964.467)
918 (199 342.08 20.186)
919 (182 27.85 445267.112)
920 (156 73.14 45036.886)
921 (136 171.52 22518.443)
922 (77 222.54 65928.934)
923 (74 296.72 3034.906)
924 (70 243.58 9037.513)
925 (58 119.81 33718.147)
926 (52 297.17 150.678)
927 (50 21.02 2281.226)
928 (45 247.54 29929.562)
929 (44 325.15 31555.956)
930 (29 60.93 4443.417)
931 (18 155.12 67555.328)
932 (17 288.79 4562.452)
933 (16 198.04 62894.029)
934 (14 199.76 31436.921)
935 (12 95.39 14577.848)
936 (12 287.11 31931.756)
937 (12 320.81 34777.259)
938 (9 227.73 1222.114)
939 (8 15.45 16859.074))
940 "Data for solar equinox/solstice calculations.")
942 (defun solar-equinoxes/solstices (k year)
943 "Date of equinox/solstice K for YEAR.
944 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
945 K=3, winter solstice. RESULT is a Gregorian local date.
946 Accurate to within a minute between 1951 and 2050."
947 (let* ((JDE0 (solar-mean-equinoxes/solstices k year))
948 (T (/ (- JDE0 2451545.0) 36525))
949 (W (- (* 35999.373 T) 2.47))
950 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W))
951 (* 0.0007 (solar-cosine-degrees (* 2 W)))))
952 (S (apply '+ (mapcar (lambda(x)
953 (* (car x) (solar-cosine-degrees
954 (+ (* (nth 2 x) T) (cadr x)))))
955 solar-seasons-data)))
956 (JDE (+ JDE0 (/ (* 0.00001 S) Delta-lambda)))
957 ;; Ephemeris time correction.
958 (correction (+ 102.3 (* 123.5 T) (* 32.5 T T)))
959 (JD (- JDE (/ correction 86400)))
960 (date (calendar-gregorian-from-absolute (floor (- JD 1721424.5))))
961 (time (- (- JD 0.5) (floor (- JD 0.5)))))
962 (list (car date) (+ (cadr date) time
963 (/ (/ calendar-time-zone 60.0) 24.0))
964 (nth 2 date))))
966 ;; From Meeus, 1991, page 166.
967 (defun solar-mean-equinoxes/solstices (k year)
968 "Julian day of mean equinox/solstice K for YEAR.
969 K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
970 solstice. These formulae are only to be used between 1000 BC and 3000 AD."
971 (let ((y (/ year 1000.0))
972 (z (/ (- year 2000) 1000.0)))
973 (if (< year 1000) ; actually between -1000 and 1000
974 (cond ((= k 0) (+ 1721139.29189
975 (* 365242.13740 y)
976 (* 0.06134 y y)
977 (* 0.00111 y y y)
978 (* -0.00071 y y y y)))
979 ((= k 1) (+ 1721233.25401
980 (* 365241.72562 y)
981 (* -0.05323 y y)
982 (* 0.00907 y y y)
983 (* 0.00025 y y y y)))
984 ((= k 2) (+ 1721325.70455
985 (* 365242.49558 y)
986 (* -0.11677 y y)
987 (* -0.00297 y y y)
988 (* 0.00074 y y y y)))
989 ((= k 3) (+ 1721414.39987
990 (* 365242.88257 y)
991 (* -0.00769 y y)
992 (* -0.00933 y y y)
993 (* -0.00006 y y y y))))
994 ; actually between 1000 and 3000
995 (cond ((= k 0) (+ 2451623.80984
996 (* 365242.37404 z)
997 (* 0.05169 z z)
998 (* -0.00411 z z z)
999 (* -0.00057 z z z z)))
1000 ((= k 1) (+ 2451716.56767
1001 (* 365241.62603 z)
1002 (* 0.00325 z z)
1003 (* 0.00888 z z z)
1004 (* -0.00030 z z z z)))
1005 ((= k 2) (+ 2451810.21715
1006 (* 365242.01767 z)
1007 (* -0.11575 z z)
1008 (* 0.00337 z z z)
1009 (* 0.00078 z z z z)))
1010 ((= k 3) (+ 2451900.05952
1011 (* 365242.74049 z)
1012 (* -0.06223 z z)
1013 (* -0.00823 z z z)
1014 (* 0.00032 z z z z)))))))
1016 (defvar displayed-month) ; from generate-calendar
1017 (defvar displayed-year)
1019 ;;;###holiday-autoload
1020 (defun solar-equinoxes-solstices ()
1021 "Local date and time of equinoxes and solstices, if visible in the calendar.
1022 Requires floating point."
1023 (let ((m displayed-month)
1024 (y displayed-year))
1025 (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
1026 ((= 2 (% m 3)) 1)
1027 (t 0)))
1028 (let* ((calendar-standard-time-zone-name
1029 (if calendar-time-zone calendar-standard-time-zone-name "UTC"))
1030 (calendar-daylight-savings-starts
1031 (if calendar-time-zone calendar-daylight-savings-starts))
1032 (calendar-daylight-savings-ends
1033 (if calendar-time-zone calendar-daylight-savings-ends))
1034 (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
1035 (k (1- (/ m 3)))
1036 (d0 (solar-equinoxes/solstices k y))
1037 (d1 (list (car d0) (floor (cadr d0)) (nth 2 d0)))
1038 (h0 (* 24 (- (cadr d0) (floor (cadr d0)))))
1039 (adj (dst-adjust-time d1 h0))
1040 (d (list (caar adj)
1041 (+ (car (cdar adj))
1042 (/ (cadr adj) 24.0))
1043 (cadr (cdar adj))))
1044 ;; The following is nearly as accurate, but not quite:
1045 ;; (d0 (solar-date-next-longitude
1046 ;; (calendar-astro-from-absolute
1047 ;; (calendar-absolute-from-gregorian
1048 ;; (list (+ 3 (* k 3)) 15 y)))
1049 ;; 90))
1050 ;; (abs-day (calendar-absolute-from-astro d)))
1051 (abs-day (calendar-absolute-from-gregorian d)))
1052 (list
1053 (list (calendar-gregorian-from-absolute (floor abs-day))
1054 (format "%s %s"
1055 (nth k (if (and calendar-latitude
1056 (< (calendar-latitude) 0))
1057 solar-s-hemi-seasons
1058 solar-n-hemi-seasons))
1059 (solar-time-string
1060 (* 24 (- abs-day (floor abs-day)))
1061 (if (dst-in-effect abs-day)
1062 calendar-daylight-time-zone-name
1063 calendar-standard-time-zone-name))))))))
1066 (provide 'solar)
1068 ;; arch-tag: bc0ff693-df58-4666-bde4-2a7837ccb8fe
1069 ;;; solar.el ends here