]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix sunrise and sunset calculation (bug#44237)
authorMattias Engdegård <mattiase@acm.org>
Mon, 26 Oct 2020 17:44:05 +0000 (18:44 +0100)
committerMattias Engdegård <mattiase@acm.org>
Tue, 27 Oct 2020 11:33:51 +0000 (12:33 +0100)
* lisp/calendar/solar.el (solar-moment): Use initial values for binary
search that won't end the loop prematurely and yield incorrect
answers.
* test/lisp/calendar/solar-tests.el: New file.

lisp/calendar/solar.el
test/lisp/calendar/solar-tests.el [new file with mode: 0644]

index 05bb3164e129c579ea3fa47494cc1e4f5176dbea..07562f62240f1a352089f08d310b98941b468c3e 100644 (file)
@@ -490,8 +490,8 @@ Uses binary search."
          (utmin (+ ut (* direction 12.0)))
          (utmax ut)     ; the time searched is between utmin and utmax
          ;; utmin and utmax are in hours.
-         (utmoment-old 0.0)             ; rise or set approximation
-         (utmoment 1.0)                 ; rise or set approximation
+         (utmoment-old utmin)           ; rise or set approximation
+         (utmoment utmax)               ; rise or set approximation
          (hut 0)                        ; sun height at utmoment
          (t0 (car time))
          (hmin (cadr (solar-horizontal-coordinates (list t0 utmin)
diff --git a/test/lisp/calendar/solar-tests.el b/test/lisp/calendar/solar-tests.el
new file mode 100644 (file)
index 0000000..441beaf
--- /dev/null
@@ -0,0 +1,42 @@
+;;; solar-tests.el --- tests for solar.el            -*- lexical-binding: t -*-
+
+;; Copyright (C) 2020 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+(require 'ert)
+(require 'solar)
+
+(ert-deftest solar-sunrise-sunset ()
+  ;; Bug#44237: wrong sunrise time on Dec 30 and 31, 2020 for Jaipur.
+  (let ((calendar-latitude 26.9)
+        (calendar-longitude 75.8)
+        (calendar-time-zone +330)
+        (calendar-standard-time-zone-name "IST")
+        (calendar-daylight-time-zone-name "IST")
+        (epsilon (/ 60.0)))             ; Minute accuracy is good enough.
+    (let* ((sunrise-sunset (solar-sunrise-sunset '(12 30 2020)))
+           (sunrise (car (nth 0 sunrise-sunset)))
+           (sunset (car (nth 1 sunrise-sunset))))
+      (should (< (abs (- sunrise 7.27)) epsilon))
+      (should (< (abs (- sunset 17.72)) epsilon)))
+    (let* ((sunrise-sunset (solar-sunrise-sunset '(12 31 2020)))
+           (sunrise (car (nth 0 sunrise-sunset)))
+           (sunset (car (nth 1 sunrise-sunset))))
+      (should (< (abs (- sunrise 7.28)) epsilon))
+      (should (< (abs (- sunset 17.72)) epsilon)))))
+
+(provide 'solar-tests)