summaryrefslogtreecommitdiff
path: root/test/lisp/calendar/time-date-tests.el
blob: 233d43cd01afa09acf878cfcfccebbb12f4155b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;;; time-date-tests.el --- tests for calendar/time-date.el    -*- lexical-binding:t -*-

;; Copyright (C) 2019-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/>.

;;; Code:

(require 'ert)
(require 'time-date)

(ert-deftest test-leap-year ()
  (should-not (date-leap-year-p 1999))
  (should-not (date-leap-year-p 1900))
  (should (date-leap-year-p 2000))
  (should (date-leap-year-p 2004)))

(ert-deftest test-days-in-month ()
  (should (= (date-days-in-month 2004 2) 29))
  (should (= (date-days-in-month 2004 3) 31))
  (should-not (= (date-days-in-month 1900 3) 28))
  (should-error (date-days-in-month 2020 15))
  (should-error (date-days-in-month 2020 'foo)))

(ert-deftest test-ordinal ()
  (should (equal (date-ordinal-to-time 2008 271)
                 '(nil nil nil 27 9 2008 nil nil nil)))
  (should (equal (date-ordinal-to-time 2008 1)
                 '(nil nil nil 1 1 2008 nil nil nil)))
  (should (equal (date-ordinal-to-time 2008 32)
                 '(nil nil nil 1 2 2008 nil nil nil)))
  (should (equal (date-ordinal-to-time 1981 095)
                 '(nil nil nil 5 4 1981 nil nil nil))))

(cl-defmethod mdec (&key second minute hour
                         day month year
                         dst zone)
  (list second minute hour day month year nil dst zone))

(ert-deftest test-decoded-add ()
  (let ((time '(12 15 16 8 7 2019 1 t 7200)))
    (should (equal (decoded-time-add time (mdec :year 1))
                   '(12 15 16 8 7 2020 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :year -2))
                   '(12 15 16 8 7 2017 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :month 1))
                   '(12 15 16 8 8 2019 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :month 10))
                   '(12 15 16 8 5 2020 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :day 1))
                   '(12 15 16 9 7 2019 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :day -1))
                   '(12 15 16 7 7 2019 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :day 30))
                   '(12 15 16 7 8 2019 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :day -365))
                   '(12 15 16 8 7 2018 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :day 365))
                   '(12 15 16 7 7 2020 1 t 7200)))

    ;; 2020 is a leap year.
    (should (equal (decoded-time-add time (mdec :day 366))
                   '(12 15 16 8 7 2020 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :second 1))
                   '(13 15 16 8 7 2019 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :second -1))
                   '(11 15 16 8 7 2019 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :second 61))
                   '(13 16 16 8 7 2019 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :hour 1 :minute 2 :second 3))
                   '(15 17 17 8 7 2019 1 t 7200)))

    (should (equal (decoded-time-add time (mdec :hour 24))
                   '(12 15 16 9 7 2019 1 t 7200)))
    ))

(ert-deftest test-decoded-add-zone ()
  (let ((time '(12 15 16 8 7 2019 1 t 7200)))
    (should (equal (decoded-time-add time (mdec :zone -3600))
                   '(12 15 15 8 7 2019 1 t 7200)))
    (should (equal (decoded-time-add time (mdec :zone -7200))
                   '(12 15 14 8 7 2019 1 t 7200)))))

(ert-deftest test-time-since ()
  (should (time-equal-p 0 (time-since nil))))

(ert-deftest test-time-decoded-period ()
  (should (equal (decoded-time-period '(nil nil 1 nil nil nil nil nil nil))
                 3600))

  (should (equal (decoded-time-period '(1 0 0 0 0 0 nil nil nil)) 1))
  (should (equal (decoded-time-period '(0 1 0 0 0 0 nil nil nil)) 60))
  (should (equal (decoded-time-period '(0 0 1 0 0 0 nil nil nil)) 3600))
  (should (equal (decoded-time-period '(0 0 0 1 0 0 nil nil nil)) 86400))
  (should (equal (decoded-time-period '(0 0 0 0 1 0 nil nil nil)) 2592000))
  (should (equal (decoded-time-period '(0 0 0 0 0 1 nil nil nil)) 31536000))

  (should (equal (decoded-time-period '((135 . 10) 0 0 0 0 0 nil nil nil))
                 13.5)))

(ert-deftest test-time-wrap-addition ()
  (should (equal (decoded-time-add '(0 0 0 1 11 2008 nil nil nil)
                                   (make-decoded-time :month 1))
                 '(0 0 0 1 12 2008 nil nil nil)))
  (should (equal (decoded-time-add '(0 0 0 1 12 2008 nil nil nil)
                                   (make-decoded-time :month 1))
                 '(0 0 0 1 1 2009 nil nil nil)))
  (should (equal (decoded-time-add '(0 0 0 1 11 2008 nil nil nil)
                                   (make-decoded-time :month 12))
                 '(0 0 0 1 11 2009 nil nil nil)))
  (should (equal (decoded-time-add '(0 0 0 1 11 2008 nil nil nil)
                                   (make-decoded-time :month 13))
                 '(0 0 0 1 12 2009 nil nil nil)))
  (should (equal (decoded-time-add '(0 0 0 30 12 2008 nil nil nil)
                                   (make-decoded-time :day 1))
                 '(0 0 0 31 12 2008 nil nil nil)))
  (should (equal (decoded-time-add '(0 0 0 30 12 2008 nil nil nil)
                                   (make-decoded-time :day 2))
                 '(0 0 0 1 1 2009 nil nil nil))))

;;; time-date-tests.el ends here