annotate lisp/calendar/solar.el @ 3571:6c1a1b79536c

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