aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Kangas2019-06-01 01:40:57 -0700
committerPaul Eggert2019-06-01 01:44:31 -0700
commitb6c628a16b52ce964f6526f9770d2f7d52ce8747 (patch)
treedec93322e8ff4739df268cf020851b63f491502f
parent12f530a7358aefe32134eeec3bc20ce58305e473 (diff)
downloademacs-b6c628a16b52ce964f6526f9770d2f7d52ce8747.tar.gz
emacs-b6c628a16b52ce964f6526f9770d2f7d52ce8747.zip
Use lexical-binding in timezone.el and add tests
* lisp/timezone.el: Use lexical-binding. * test/lisp/timezone-tests.el: New file.
-rw-r--r--lisp/timezone.el8
-rw-r--r--test/lisp/timezone-tests.el169
2 files changed, 172 insertions, 5 deletions
diff --git a/lisp/timezone.el b/lisp/timezone.el
index 79720b42ec1..ff0b266245f 100644
--- a/lisp/timezone.el
+++ b/lisp/timezone.el
@@ -1,4 +1,4 @@
1;;; timezone.el --- time zone package for GNU Emacs 1;;; timezone.el --- time zone package for GNU Emacs -- lexical-binding: t -*-
2 2
3;; Copyright (C) 1990-1993, 1996, 1999, 2001-2019 Free Software 3;; Copyright (C) 1990-1993, 1996, 1999, 2001-2019 Free Software
4;; Foundation, Inc. 4;; Foundation, Inc.
@@ -72,8 +72,7 @@ if nil, the local time zone is assumed."
72 (timezone-make-arpa-date (aref new 0) (aref new 1) (aref new 2) 72 (timezone-make-arpa-date (aref new 0) (aref new 1) (aref new 2)
73 (timezone-make-time-string 73 (timezone-make-time-string
74 (aref new 3) (aref new 4) (aref new 5)) 74 (aref new 3) (aref new 4) (aref new 5))
75 (aref new 6)) 75 (aref new 6))))
76 ))
77 76
78(defun timezone-make-date-sortable (date &optional local timezone) 77(defun timezone-make-date-sortable (date &optional local timezone)
79 "Convert DATE to a sortable date string. 78 "Convert DATE to a sortable date string.
@@ -84,8 +83,7 @@ if nil, the local time zone is assumed."
84 (let ((new (timezone-fix-time date local timezone))) 83 (let ((new (timezone-fix-time date local timezone)))
85 (timezone-make-sortable-date (aref new 0) (aref new 1) (aref new 2) 84 (timezone-make-sortable-date (aref new 0) (aref new 1) (aref new 2)
86 (timezone-make-time-string 85 (timezone-make-time-string
87 (aref new 3) (aref new 4) (aref new 5))) 86 (aref new 3) (aref new 4) (aref new 5)))))
88 ))
89 87
90 88
91;; 89;;
diff --git a/test/lisp/timezone-tests.el b/test/lisp/timezone-tests.el
new file mode 100644
index 00000000000..4b5f5617ecd
--- /dev/null
+++ b/test/lisp/timezone-tests.el
@@ -0,0 +1,169 @@
1;;; timezone-tests.el --- Tests for timezone.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2019 Free Software Foundation, Inc.
4
5;; Author: Stefan Kangas <stefankangas@gmail.com>
6
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software: you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
21
22;;; Commentary:
23
24(require 'ert)
25(require 'timezone)
26
27(ert-deftest timezone-tests-make-date-arpa-standard ()
28 (should (equal (timezone-make-date-arpa-standard "14 Apr 89 03:20" "PST" "GMT")
29 "14 Apr 1989 11:20:00 GMT"))
30 (should (equal (timezone-make-date-arpa-standard "14 Apr 89 03:20" "GMT" "GMT")
31 "14 Apr 1989 03:20:00 GMT"))
32 (should (equal (timezone-make-date-arpa-standard "14 Apr 89 03:20" nil "GMT") ; assume GMT
33 "14 Apr 1989 03:20:00 GMT")))
34
35(ert-deftest timezone-tests-make-date-sortable ()
36 (should (equal (timezone-make-date-sortable "14 Apr 89 03:20" "GMT" "GMT")
37 "1989041403:20:00"))
38 (should (equal (timezone-make-date-sortable "14 Apr 89 03:20" nil "GMT") ; assume GMT
39 "1989041403:20:00")))
40
41(ert-deftest timezone-tests-make-arpa-date ()
42 (should (equal (timezone-make-arpa-date 2020 1 1 "00:00")
43 "01 Jan 2020 00:00 +0000"))
44 (should (equal (timezone-make-arpa-date 2020 1 1 "00:00" "GMT")
45 "01 Jan 2020 00:00 GMT")))
46
47(ert-deftest timezone-tests-make-sortable-date ()
48 (should (equal (timezone-make-sortable-date 2020 1 1 "00:00")
49 "2020010100:00")))
50
51(ert-deftest timezone-tests-make-time-string ()
52 (should (equal (timezone-make-time-string 1 2 3) "01:02:03")))
53
54(ert-deftest timezone-tests-parse-date ()
55 ;; Accepted style 1 from docstring
56 (should (equal (timezone-parse-date "14 Apr 89 03:20")
57 ["1989" "4" "14" "03:20" nil]))
58 (should (equal (timezone-parse-date "14 Apr 89 03:20 GMT")
59 ["1989" "4" "14" "03:20" "GMT"]))
60 (should (equal (timezone-parse-date "14 Apr 89 03:20:12")
61 ["1989" "4" "14" "03:20:12" nil]))
62 (should (equal (timezone-parse-date "14 Apr 89 03:20:12 GMT")
63 ["1989" "4" "14" "03:20:12" "GMT"]))
64 ;; Accepted style 2 from docstring
65 (should (equal (timezone-parse-date "Fri, 17 Mar 89 4:01")
66 ["1989" "3" "17" "4:01" nil]))
67 (should (equal (timezone-parse-date "Fri, 17 Mar 89 4:01 GMT")
68 ["1989" "3" "17" "4:01" "GMT"]))
69 (should (equal (timezone-parse-date "Fri, 17 Mar 89 4:01:33")
70 ["1989" "3" "17" "4:01:33" nil]))
71 (should (equal (timezone-parse-date "Fri, 17 Mar 89 4:01:33 GMT")
72 ["1989" "3" "17" "4:01:33" "GMT"]))
73 ;; Accepted style 3 from docstring
74 (should (equal (timezone-parse-date "Mon Jan 16 16:12 1989")
75 ["1989" "1" "16" "16:12" nil]))
76 (should (equal (timezone-parse-date "Mon Jan 16 16:12 GMT 1989")
77 ["1989" "1" "16" "16:12" "GMT"]))
78 (should (equal (timezone-parse-date "Mon Jan 16 16:12:37 1989")
79 ["1989" "1" "16" "16:12:37" nil]))
80 (should (equal (timezone-parse-date "Mon Jan 16 16:12:37 GMT 1989")
81 ["1989" "1" "16" "16:12:37" "GMT"]))
82 ;; Accepted style 4 from docstring
83 (should (equal (timezone-parse-date "6 May 1992 1641-JST (Wednesday)")
84 ["1992" "5" "6" "1641" "-JST"]))
85 ;; Accepted style 5 from docstring
86 (should (equal (timezone-parse-date "22-AUG-1993 10:59:12.82")
87 ["1993" "8" "22" "10:59:12" nil]))
88 ;; Accepted style 6 from docstring
89 (should (equal (timezone-parse-date "Thu, 11 Apr 16:17:12 91")
90 ["1991" "4" "11" "16:17:12" nil]))
91 (should (equal (timezone-parse-date "Thu, 11 Apr 16:17:12 91 MET")
92 ["1991" "4" "11" "16:17:12" "MET"]))
93 ;; Accepted style 7 from docstring
94 (should (equal (timezone-parse-date "Mon, 6 Jul 16:47:20 T 1992")
95 ["1992" "7" "6" "16:47:20" nil]))
96 (should (equal (timezone-parse-date "Mon, 6 Jul 16:47:20 T 1992 MET")
97 ["1992" "7" "6" "16:47:20" "MET"]))
98 ;; Accepted style 8 from docstring
99 (should (equal (timezone-parse-date "1996-06-24 21:13")
100 ["1996" "06" "24" "21:13" nil]))
101 (should (equal (timezone-parse-date "1996-06-24 21:13 GMT")
102 ["1996" "06" "24" "21:13" "GMT"]))
103 (should (equal (timezone-parse-date "1996-06-24 21:13:12")
104 ["1996" "06" "24" "21:13:12" nil]))
105 (should (equal (timezone-parse-date "1996-06-24 21:13:12 GMT")
106 ["1996" "06" "24" "21:13:12" "GMT"]))
107 ;; Accepted style 9 from docstring
108 (should (equal (timezone-parse-date "1996-06-24 21:13-ZONE")
109 ["1996" "06" "24" "21:13" "-ZONE"]))
110 ;; Accepted style 10 from docstring
111 (should (equal (timezone-parse-date "19960624T211312")
112 ["1996" "06" "24" "211312" nil]))
113 (should (equal (timezone-parse-date "*invalid*") ["0" "0" "0" "0" nil])))
114
115(ert-deftest timezone-tests-parse-date/windowed-dates ()
116 (should (equal (timezone-parse-date "14 Apr 69 10:00")
117 ["1969" "4" "14" "10:00" nil]))
118 (should (equal (timezone-parse-date "14 Apr 68 10:00")
119 ["2068" "4" "14" "10:00" nil]))
120 (should (equal (timezone-parse-date "14 Apr 199 10:00")
121 ["2099" "4" "14" "10:00" nil])))
122
123(ert-deftest timezone-tests-parse-time ()
124 (should (equal (timezone-parse-time "1234") ["12" "34" "0"]))
125 (should (equal (timezone-parse-time "12:34") ["12" "34" "0"]))
126 (should (equal (timezone-parse-time "123456") ["12" "34" "56"]))
127 (should (equal (timezone-parse-time "12:34:56") ["12" "34" "56"]))
128 (should (equal (timezone-parse-time "*invalid*") ["0" "0" "0"])))
129
130(ert-deftest timezone-tests-zone-to-minute ()
131 (should (equal (timezone-zone-to-minute "GMT") 0))
132 (should (equal (timezone-zone-to-minute "CDT") -300))
133 (should (equal (timezone-zone-to-minute "+100") 60))
134 (should (equal (timezone-zone-to-minute "-100") -60))
135 (should (equal (timezone-zone-to-minute "*invalid*") 0)))
136
137(ert-deftest timezone-tests-time-from-absolute ()
138 (should (equal (timezone-time-from-absolute (* 2020 365) ; Jan 1 2020
139 (* 12 60 60)) ; 12:00
140 '(23911 48704 0 0))))
141
142;; TODO: Write tests for timezone-tests-time-zone-from-absolute, which is a
143;; bit tricky since the results depend on `current-time-zone'.
144
145(ert-deftest timezone-tests-fix-time ()
146 (should (equal (timezone-fix-time "20200501 20:20" "GMT" "GMT")
147 [2020 5 1 20 20 0 "GMT"]))
148 (should (equal (timezone-fix-time "20200501 20:20" nil "GMT") ; assume GMT
149 [2020 5 1 20 20 0 "GMT"])))
150
151(ert-deftest timezone-tests-last-day-of-month ()
152 (should (equal (timezone-last-day-of-month 2 1999) 28))
153 (should (equal (timezone-last-day-of-month 2 2000) 29))) ; leap year
154
155(ert-deftest timezone-tests-leap-year-p ()
156 (should (equal (timezone-leap-year-p 1999) nil))
157 (should (equal (timezone-leap-year-p 2000) t))
158 (should (equal (timezone-leap-year-p 2024) t))
159 (should (equal (timezone-leap-year-p 2100) nil)))
160
161(ert-deftest timezone-tests-day-number ()
162 (should (equal (timezone-day-number 5 1 1999) 121))
163 (should (equal (timezone-day-number 5 1 2000) 122))) ; leap year
164
165(ert-deftest timezone-tests-absolute-from-gregorian ()
166 (should (equal (timezone-absolute-from-gregorian 1 1 2020) 737425)))
167
168(provide 'timezone-tests)
169;;; timezone-tests.el ends here