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