1# frozen_string_literal: true
2require 'test/unit'
3require 'date'
4
5class TestDateNew < Test::Unit::TestCase
6
7  def test_jd
8    d = Date.jd
9    dt = DateTime.jd
10    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
11    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
12    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
13
14    d2 = Date.jd
15    dt2 = DateTime.jd
16    assert_equal(d, d2)
17    assert_equal(dt, dt2)
18
19    d = Date.jd(0)
20    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
21    d = DateTime.jd(0, 0,0,0, 0)
22    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
23		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
24    d = DateTime.jd(0, 0,0,0, '+0900')
25    assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24],
26		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
27  end
28
29  def test_jd__ex
30    assert_raise(ArgumentError) do
31      DateTime.jd(0, 23,59,60,0)
32    end
33  end
34
35  def test_ordinal
36    d = Date.ordinal
37    dt = DateTime.ordinal
38    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
39    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
40    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
41
42    d2 = Date.ordinal
43    dt2 = DateTime.ordinal
44    assert_equal(d, d2)
45    assert_equal(dt, dt2)
46
47    d = Date.ordinal(-4712,1)
48    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
49
50    d = Date.ordinal(-4712,1.0)
51    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
52
53    d = DateTime.ordinal(-4712,1, 0,0,0, 0)
54    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
55		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
56    d = DateTime.ordinal(-4712,1, 0,0,0, '+0900')
57    assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24],
58		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
59  end
60
61  def test_ordinal__neg
62    d = Date.ordinal(-1,-1)
63    assert_equal([-1, 365], [d.year, d.yday])
64
65    d = DateTime.ordinal(-1,-1, -1,-1,-1, 0)
66    assert_equal([-1, 365, 23, 59, 59, 0],
67		 [d.year, d.yday, d.hour, d.min, d.sec, d.offset])
68  end
69
70  def test_ordinal__ex
71    assert_raise(ArgumentError) do
72      Date.ordinal(2001,366)
73    end
74    assert_raise(ArgumentError) do
75      DateTime.ordinal(2001,365, 23,59,60, 0)
76    end
77  end
78
79  def test_civil
80    d = Date.civil
81    dt = DateTime.civil
82    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
83    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
84    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
85
86    d2 = Date.civil
87    dt2 = DateTime.civil
88    assert_equal(d, d2)
89    assert_equal(dt, dt2)
90
91    d = Date.civil(-4712,1,1)
92    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
93
94    d = Date.civil(-4712,1,1.0)
95    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
96
97    d = DateTime.civil(-4712,1,1, 0,0,0, 0)
98    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
99		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
100    d = DateTime.civil(-4712,1,1, 0,0,0, '+0900')
101    assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24],
102		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
103
104
105    d = DateTime.civil(2001,2,3 + 1.to_r/2)
106    assert_equal([2001, 2, 3, 12, 0, 0, 0],
107		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
108    d = DateTime.civil(2001,2,3, 4 + 1.to_r/2)
109    assert_equal([2001, 2, 3, 4, 30, 0, 0],
110		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
111    d = DateTime.civil(2001,2,3, 4,5 + 1.to_r/2)
112    assert_equal([2001, 2, 3, 4, 5, 30, 0],
113		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
114    d = DateTime.civil(2001,2,3, 4,5,6 + 1.to_r/2)
115    assert_equal([2001, 2, 3, 4, 5, 6, 0],
116		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
117    assert_equal(1.to_r/2, d.sec_fraction)
118  end
119
120  def test_civil__neg
121    d = Date.civil(-1,-1,-1)
122    assert_equal([-1, 12, 31], [d.year, d.mon, d.mday])
123
124    d = DateTime.civil(-1,-1,-1, -1,-1,-1, 0)
125    assert_equal([-1, 12, 31, 23, 59, 59, 0],
126		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
127  end
128
129  def test_civil__ex
130    assert_raise(ArgumentError) do
131      Date.civil(2001,2,29)
132    end
133    assert_raise(ArgumentError) do
134      DateTime.civil(2001,2,28, 23,59,60, 0)
135    end
136    assert_raise(ArgumentError) do
137      DateTime.civil(2001,2,28, 24,59,59, 0)
138    end
139  end
140
141  def test_civil__reform
142    d = Date.jd(Date::ENGLAND, Date::ENGLAND)
143    dt = DateTime.jd(Date::ENGLAND, 0,0,0,0, Date::ENGLAND)
144    assert_equal([1752, 9, 14], [d.year, d.mon, d.mday])
145    assert_equal([1752, 9, 14], [dt.year, dt.mon, dt.mday])
146    d -= 1
147    dt -= 1
148    assert_equal([1752, 9, 2], [d.year, d.mon, d.mday])
149    assert_equal([1752, 9, 2], [dt.year, dt.mon, dt.mday])
150
151    d = Date.jd(Date::ITALY, Date::ITALY)
152    dt = DateTime.jd(Date::ITALY, 0,0,0,0, Date::ITALY)
153    assert_equal([1582, 10, 15], [d.year, d.mon, d.mday])
154    assert_equal([1582, 10, 15], [dt.year, dt.mon, dt.mday])
155    d -= 1
156    dt -= 1
157    assert_equal([1582, 10, 4], [d.year, d.mon, d.mday])
158    assert_equal([1582, 10, 4], [dt.year, dt.mon, dt.mday])
159  end
160
161  def test_commercial
162    d = Date.commercial
163    dt = DateTime.commercial
164    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
165    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
166    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
167
168    d2 = Date.commercial
169    dt2 = DateTime.commercial
170    assert_equal(d, d2)
171    assert_equal(dt, dt2)
172
173    d = Date.commercial(1582,40,5)
174    assert_equal([1582, 10, 15], [d.year, d.mon, d.mday])
175
176    d = Date.commercial(1582,40,5.0)
177    assert_equal([1582, 10, 15], [d.year, d.mon, d.mday])
178
179    d = DateTime.commercial(1582,40,5, 0,0,0, 0)
180    assert_equal([1582, 10, 15, 0, 0, 0, 0],
181		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
182    d = DateTime.commercial(1582,40,5, 0,0,0, '+0900')
183    assert_equal([1582, 10, 15, 0, 0, 0, 9.to_r/24],
184		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
185  end
186
187  def test_commercial__neg
188    d = Date.commercial(1998,-1,-1)
189    assert_equal([1999, 1, 3], [d.year, d.mon, d.mday])
190
191    d = DateTime.commercial(1998,-1,-1, -1,-1,-1, 0)
192    assert_equal([1999, 1, 3, 23, 59, 59, 0],
193		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
194  end
195
196  def test_commercial__ex
197    assert_raise(ArgumentError) do
198      Date.commercial(1997,53,1)
199    end
200    assert_raise(ArgumentError) do
201      DateTime.commercial(1997,52,1, 23,59,60, 0)
202    end
203  end
204
205  def test_weeknum
206    d = Date.weeknum
207    dt = DateTime.weeknum
208    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
209    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
210    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
211
212    d = Date.weeknum(2002,11,4, 0)
213    assert_equal(2452355, d.jd)
214
215    d = DateTime.weeknum(2002,11,4, 0, 11,22,33)
216    assert_equal(2452355, d.jd)
217    assert_equal([11,22,33], [d.hour, d.min, d.sec])
218
219    assert_raise(ArgumentError) do
220      Date.weeknum(1999,53,0, 0)
221    end
222    assert_raise(ArgumentError) do
223      Date.weeknum(1999,-53,-1, 0)
224    end
225  end if Date.respond_to?(:weeknum, true)
226
227  def test_nth_kday
228    d = Date.nth_kday
229    dt = DateTime.nth_kday
230    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
231    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
232    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])
233
234    d = Date.nth_kday(1992,2, 5,6)
235    assert_equal(2448682, d.jd)
236
237    d = DateTime.nth_kday(1992,2, 5,6, 11,22,33)
238    assert_equal(2448682, d.jd)
239    assert_equal([11,22,33], [d.hour, d.min, d.sec])
240
241    assert_raise(ArgumentError) do
242      Date.nth_kday(2006,5, 5,0)
243    end
244    assert_raise(ArgumentError) do
245      Date.nth_kday(2006,5, -5,0)
246    end
247  end if Date.respond_to?(:nth_kday, true)
248
249  def test_today
250    z = Time.now
251    d = Date.today
252    t = Time.now
253    t2 = Time.utc(t.year, t.mon, t.mday)
254    t3 = Time.utc(d.year, d.mon, d.mday)
255    assert_in_delta(t2, t3, t - z + 2)
256
257    assert_equal(false, DateTime.respond_to?(:today))
258  end
259
260  def test_now
261    assert_equal(false, Date.respond_to?(:now))
262
263    z = Time.now
264    d = DateTime.now
265    t = Time.now
266    t2 = Time.local(d.year, d.mon, d.mday, d.hour, d.min, d.sec)
267    assert_in_delta(t, t2, t - z + 2)
268  end
269
270  def test_memsize
271    require 'objspace'
272    t = DateTime.now
273    size = ObjectSpace.memsize_of(t)
274    t.__send__(:initialize_copy, Date.today)
275    assert_instance_of(DateTime, t)
276    assert_equal(size, ObjectSpace.memsize_of(t), "not reallocated but memsize changed")
277  end
278end
279