1require 'test/unit'
2require 'date'
3
4class TestSH < Test::Unit::TestCase
5
6  def test_new
7    [Date.new,
8     Date.civil,
9     DateTime.new,
10     DateTime.civil
11	].each do |d|
12      assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
13    end
14
15    [Date.new(2001),
16     Date.civil(2001),
17     DateTime.new(2001),
18     DateTime.civil(2001)
19	].each do |d|
20      assert_equal([2001, 1, 1], [d.year, d.mon, d.mday])
21    end
22
23    d = Date.new(2001, 2, 3)
24    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
25    d = Date.new(2001, 2, Rational('3.5'))
26    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
27    d = Date.new(2001,2, 3, Date::JULIAN)
28    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
29    d = Date.new(2001,2, 3, Date::GREGORIAN)
30    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
31
32    d = Date.new(2001,-12, -31)
33    assert_equal([2001, 1, 1], [d.year, d.mon, d.mday])
34    d = Date.new(2001,-12, -31, Date::JULIAN)
35    assert_equal([2001, 1, 1], [d.year, d.mon, d.mday])
36    d = Date.new(2001,-12, -31, Date::GREGORIAN)
37    assert_equal([2001, 1, 1], [d.year, d.mon, d.mday])
38
39    d = DateTime.new(2001, 2, 3, 4, 5, 6)
40    assert_equal([2001, 2, 3, 4, 5, 6, 0],
41		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
42    d = DateTime.new(2001, 2, 3, 4, 5, 6, 0)
43    assert_equal([2001, 2, 3, 4, 5, 6, 0],
44		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
45    d = DateTime.new(2001, 2, 3, 4, 5, 6, Rational(9,24))
46    assert_equal([2001, 2, 3, 4, 5, 6, Rational(9,24)],
47		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
48    d = DateTime.new(2001, 2, 3, 4, 5, 6, 0.375)
49    assert_equal([2001, 2, 3, 4, 5, 6, Rational(9,24)],
50		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
51    d = DateTime.new(2001, 2, 3, 4, 5, 6, '+09:00')
52    assert_equal([2001, 2, 3, 4, 5, 6, Rational(9,24)],
53		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
54    d = DateTime.new(2001, 2, 3, 4, 5, 6, '-09:00')
55    assert_equal([2001, 2, 3, 4, 5, 6, Rational(-9,24)],
56		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
57    d = DateTime.new(2001, -12, -31, -4, -5, -6, '-09:00')
58    assert_equal([2001, 1, 1, 20, 55, 54, Rational(-9,24)],
59		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
60    d = DateTime.new(2001, -12, -31, -4, -5, -6, '-09:00', Date::JULIAN)
61    assert_equal([2001, 1, 1, 20, 55, 54, Rational(-9,24)],
62		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
63    d = DateTime.new(2001, -12, -31, -4, -5, -6, '-09:00', Date::GREGORIAN)
64    assert_equal([2001, 1, 1, 20, 55, 54, Rational(-9,24)],
65		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
66  end
67
68  def test_jd
69    d = Date.jd
70    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
71    d = Date.jd(0)
72    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
73    d = Date.jd(2451944)
74    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
75
76    d = DateTime.jd
77    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
78		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
79    d = DateTime.jd(0)
80    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
81		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
82    d = DateTime.jd(2451944)
83    assert_equal([2001, 2, 3, 0, 0, 0, 0],
84		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
85    d = DateTime.jd(2451944, 4, 5, 6)
86    assert_equal([2001, 2, 3, 4, 5, 6, 0],
87		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
88    d = DateTime.jd(2451944, 4, 5, 6, 0)
89    assert_equal([2001, 2, 3, 4, 5, 6, 0],
90		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
91    d = DateTime.jd(2451944, 4, 5, 6, '+9:00')
92    assert_equal([2001, 2, 3, 4, 5, 6, Rational(9, 24)],
93		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
94    d = DateTime.jd(2451944, -4, -5, -6, '-9:00')
95    assert_equal([2001, 2, 3, 20, 55, 54, Rational(-9, 24)],
96		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
97  end
98
99  def test_ordinal
100    d = Date.ordinal
101    assert_equal([-4712, 1], [d.year, d.yday])
102    d = Date.ordinal(-4712, 1)
103    assert_equal([-4712, 1], [d.year, d.yday])
104
105    d = Date.ordinal(2001, 2)
106    assert_equal([2001, 2], [d.year, d.yday])
107    d = Date.ordinal(2001, 2, Date::JULIAN)
108    assert_equal([2001, 2], [d.year, d.yday])
109    d = Date.ordinal(2001, 2, Date::GREGORIAN)
110    assert_equal([2001, 2], [d.year, d.yday])
111
112    d = Date.ordinal(2001, -2, Date::JULIAN)
113    assert_equal([2001, 364], [d.year, d.yday])
114    d = Date.ordinal(2001, -2, Date::GREGORIAN)
115    assert_equal([2001, 364], [d.year, d.yday])
116
117    d = DateTime.ordinal
118    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
119		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
120    d = DateTime.ordinal(-4712, 1)
121    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
122		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
123    d = DateTime.ordinal(2001, 34)
124    assert_equal([2001, 2, 3, 0, 0, 0, 0],
125		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
126    d = DateTime.ordinal(2001, 34, 4, 5, 6)
127    assert_equal([2001, 2, 3, 4, 5, 6, 0],
128		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
129    d = DateTime.ordinal(2001, 34, 4, 5, 6, 0)
130    assert_equal([2001, 2, 3, 4, 5, 6, 0],
131		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
132    d = DateTime.ordinal(2001, 34, 4, 5, 6, '+9:00')
133    assert_equal([2001, 2, 3, 4, 5, 6, Rational(9, 24)],
134		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
135    d = DateTime.ordinal(2001, 34, -4, -5, -6, '-9:00')
136    assert_equal([2001, 2, 3, 20, 55, 54, Rational(-9, 24)],
137		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
138  end
139
140  def test_commercial
141    d = Date.commercial
142    assert_equal([-4712, 1, 1], [d.cwyear, d.cweek, d.cwday])
143    d = Date.commercial(-4712, 1, 1)
144    assert_equal([-4712, 1, 1], [d.cwyear, d.cweek, d.cwday])
145
146    d = Date.commercial(2001, 2, 3)
147    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
148    d = Date.commercial(2001, 2, 3, Date::JULIAN)
149    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
150    d = Date.commercial(2001, 2, 3, Date::GREGORIAN)
151    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
152
153    d = Date.commercial(2001, -2, -3)
154    assert_equal([2001, 51, 5], [d.cwyear, d.cweek, d.cwday])
155    d = Date.commercial(2001, -2, -3, Date::JULIAN)
156    assert_equal([2001, 51, 5], [d.cwyear, d.cweek, d.cwday])
157    d = Date.commercial(2001, -2, -3, Date::GREGORIAN)
158    assert_equal([2001, 51, 5], [d.cwyear, d.cweek, d.cwday])
159
160    d = DateTime.commercial
161    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
162		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
163    d = DateTime.commercial(-4712, 1, 1)
164    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
165		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
166    d = DateTime.commercial(2001, 5, 6)
167    assert_equal([2001, 2, 3, 0, 0, 0, 0],
168		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
169    d = DateTime.commercial(2001, 5, 6, 4, 5, 6)
170    assert_equal([2001, 2, 3, 4, 5, 6, 0],
171		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
172    d = DateTime.commercial(2001, 5, 6, 4, 5, 6, 0)
173    assert_equal([2001, 2, 3, 4, 5, 6, 0],
174		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
175    d = DateTime.commercial(2001, 5, 6, 4, 5, 6, '+9:00')
176    assert_equal([2001, 2, 3, 4, 5, 6, Rational(9, 24)],
177		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
178    d = DateTime.commercial(2001, 5, 6, -4, -5, -6, '-9:00')
179    assert_equal([2001, 2, 3, 20, 55, 54, Rational(-9, 24)],
180		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
181  end
182
183  def test_fractional
184    d = Date.jd(2451944.0)
185    assert_equal(2451944, d.jd)
186    d = Date.jd(Rational(2451944))
187    assert_equal(2451944, d.jd)
188    d = Date.jd(2451944.5)
189    assert_equal([2451944, 12], [d.jd, d.send('hour')])
190    d = Date.jd(Rational('2451944.5'))
191    assert_equal([2451944, 12], [d.jd, d.send('hour')])
192
193    d = Date.civil(2001, 2, 3.0)
194    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
195    d = Date.civil(2001, 2, Rational(3))
196    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
197    d = Date.civil(2001, 2, 3.5)
198    assert_equal([2001, 2, 3, 12], [d.year, d.mon, d.mday, d.send('hour')])
199    d = Date.civil(2001, 2, Rational('3.5'))
200    assert_equal([2001, 2, 3, 12], [d.year, d.mon, d.mday, d.send('hour')])
201
202    d = Date.ordinal(2001, 2.0)
203    assert_equal([2001, 2], [d.year, d.yday])
204    d = Date.ordinal(2001, Rational(2))
205    assert_equal([2001, 2], [d.year, d.yday])
206
207    d = Date.commercial(2001, 2, 3.0)
208    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
209    d = Date.commercial(2001, 2, Rational(3))
210    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
211
212    d = DateTime.jd(2451944.0)
213    assert_equal(2451944, d.jd)
214    d = DateTime.jd(Rational(2451944))
215    assert_equal(2451944, d.jd)
216    d = DateTime.jd(2451944.5)
217    assert_equal([2451944, 12], [d.jd, d.hour])
218    d = DateTime.jd(Rational('2451944.5'))
219    assert_equal([2451944, 12], [d.jd, d.hour])
220
221    d = DateTime.civil(2001, 2, 3.0)
222    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
223    d = DateTime.civil(2001, 2, Rational(3))
224    assert_equal([2001, 2, 3], [d.year, d.mon, d.mday])
225    d = DateTime.civil(2001, 2, 3.5)
226    assert_equal([2001, 2, 3, 12], [d.year, d.mon, d.mday, d.hour])
227    d = DateTime.civil(2001, 2, Rational('3.5'))
228    assert_equal([2001, 2, 3, 12], [d.year, d.mon, d.mday, d.hour])
229    d = DateTime.civil(2001, 2, 3, 4.5)
230    assert_equal([2001, 2, 3, 4, 30], [d.year, d.mon, d.mday, d.hour, d.min])
231    d = DateTime.civil(2001, 2, 3, Rational('4.5'))
232    assert_equal([2001, 2, 3, 4, 30], [d.year, d.mon, d.mday, d.hour, d.min])
233    d = DateTime.civil(2001, 2, 3, 4, 5.5)
234    assert_equal([2001, 2, 3, 4, 5, 30],
235		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
236    d = DateTime.civil(2001, 2, 3, 4, Rational('5.5'))
237    assert_equal([2001, 2, 3, 4, 5, 30],
238		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
239
240    d = DateTime.ordinal(2001, 2.0)
241    assert_equal([2001, 2], [d.year, d.yday])
242    d = DateTime.ordinal(2001, Rational(2))
243    assert_equal([2001, 2], [d.year, d.yday])
244
245    d = DateTime.commercial(2001, 2, 3.0)
246    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
247    d = DateTime.commercial(2001, 2, Rational(3))
248    assert_equal([2001, 2, 3], [d.cwyear, d.cweek, d.cwday])
249
250  end
251
252  def test_canon24oc
253    d = DateTime.jd(2451943,24)
254    assert_equal([2001, 2, 3, 0, 0, 0, 0],
255		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
256    d = DateTime.ordinal(2001,33,24)
257    assert_equal([2001, 2, 3, 0, 0, 0, 0],
258		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
259    d = DateTime.new(2001,2,2,24)
260    assert_equal([2001, 2, 3, 0, 0, 0, 0],
261		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
262    d = DateTime.commercial(2001,5,5,24)
263    assert_equal([2001, 2, 3, 0, 0, 0, 0],
264		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
265  end
266
267  def test_zone
268    d = Date.new(2001, 2, 3)
269    assert_equal(Encoding::US_ASCII, d.send(:zone).encoding)
270    d = DateTime.new(2001, 2, 3)
271    assert_equal(Encoding::US_ASCII, d.send(:zone).encoding)
272  end
273
274  def test_to_s
275    d = Date.new(2001, 2, 3)
276    assert_equal(Encoding::US_ASCII, d.to_s.encoding)
277    assert_equal(Encoding::US_ASCII, d.strftime.encoding)
278    d = DateTime.new(2001, 2, 3)
279    assert_equal(Encoding::US_ASCII, d.to_s.encoding)
280    assert_equal(Encoding::US_ASCII, d.strftime.encoding)
281  end
282
283  def test_inspect
284    d = Date.new(2001, 2, 3)
285    assert_equal(Encoding::US_ASCII, d.inspect.encoding)
286    d = DateTime.new(2001, 2, 3)
287    assert_equal(Encoding::US_ASCII, d.inspect.encoding)
288  end
289
290  def test_strftime
291    assert_raise(Errno::ERANGE) do
292      Date.today.strftime('%100000z')
293    end
294    assert_raise(Errno::ERANGE) do
295      Date.new(1 << 10000).strftime('%Y')
296    end
297    assert_equal('-3786825600', Date.new(1850).strftime('%s'))
298    assert_equal('-3786825600000', Date.new(1850).strftime('%Q'))
299  end
300
301  def test_cmp
302    assert_equal(-1, Date.new(2001,2,3) <=> Date.new(2001,2,4))
303    assert_equal(0, Date.new(2001,2,3) <=> Date.new(2001,2,3))
304    assert_equal(1, Date.new(2001,2,3) <=> Date.new(2001,2,2))
305
306    assert_equal(-1, Date.new(2001,2,3) <=> 2451944.0)
307    assert_equal(-1, Date.new(2001,2,3) <=> 2451944)
308    assert_equal(0, Date.new(2001,2,3) <=> 2451943.5)
309    assert_equal(1, Date.new(2001,2,3) <=> 2451943.0)
310    assert_equal(1, Date.new(2001,2,3) <=> 2451943)
311
312    assert_equal(-1, Date.new(2001,2,3) <=> Rational('4903888/2'))
313    assert_equal(0, Date.new(2001,2,3) <=> Rational('4903887/2'))
314    assert_equal(1, Date.new(2001,2,3) <=> Rational('4903886/2'))
315  end
316
317  def test_eqeqeq
318    assert_equal(false, Date.new(2001,2,3) === Date.new(2001,2,4))
319    assert_equal(true, Date.new(2001,2,3) === Date.new(2001,2,3))
320    assert_equal(false, Date.new(2001,2,3) === Date.new(2001,2,2))
321
322    assert_equal(true, Date.new(2001,2,3) === 2451944.0)
323    assert_equal(true, Date.new(2001,2,3) === 2451944)
324    assert_equal(false, Date.new(2001,2,3) === 2451943.5)
325    assert_equal(false, Date.new(2001,2,3) === 2451943.0)
326    assert_equal(false, Date.new(2001,2,3) === 2451943)
327
328    assert_equal(true, Date.new(2001,2,3) === Rational('4903888/2'))
329    assert_equal(false, Date.new(2001,2,3) === Rational('4903887/2'))
330    assert_equal(false, Date.new(2001,2,3) === Rational('4903886/2'))
331  end
332
333  def test_period
334    # -5000
335    d = Date.new(-5000,1,1)
336    assert_equal([-5000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
337    d2 = d.gregorian
338    assert_equal([-5001, 11, 22, 5], [d2.year, d2.mon, d2.mday, d.wday])
339
340    d = Date.new(-5000,1,1,Date::JULIAN)
341    assert_equal([-5000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
342    d2 = d.gregorian
343    assert_equal([-5001, 11, 22, 5], [d2.year, d2.mon, d2.mday, d.wday])
344
345    d = Date.new(-5000,1,1,Date::GREGORIAN)
346    assert_equal([-5000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
347    d2 = d.julian
348    assert_equal([-5000, 2, 10, 3], [d2.year, d2.mon, d2.mday, d.wday])
349
350    d = Date.jd(-105192)
351    assert_equal([-5000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
352    d2 = d.gregorian
353    assert_equal([-5001, 11, 22, 5], [d2.year, d2.mon, d2.mday, d.wday])
354
355    d = Date.jd(-105192,Date::JULIAN)
356    assert_equal([-5000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
357    d2 = d.gregorian
358    assert_equal([-5001, 11, 22, 5], [d2.year, d2.mon, d2.mday, d.wday])
359
360    d = Date.jd(-105152,Date::GREGORIAN)
361    assert_equal([-5000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
362    d2 = d.julian
363    assert_equal([-5000, 2, 10, 3], [d2.year, d2.mon, d2.mday, d.wday])
364
365    # -5000000
366    d = Date.new(-5_000_000,1,1)
367    assert_equal([-5_000_000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
368    d2 = d.gregorian
369    assert_equal([-5_000_103, 4, 28, 3], [d2.year, d2.mon, d2.mday, d.wday])
370
371    d = Date.new(-5_000_000,1,1,Date::JULIAN)
372    assert_equal([-5_000_000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
373    d2 = d.gregorian
374    assert_equal([-5_000_103, 4, 28, 3], [d2.year, d2.mon, d2.mday, d.wday])
375
376    d = Date.new(-5_000_000,1,1,Date::GREGORIAN)
377    assert_equal([-5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
378    d2 = d.julian
379    assert_equal([-4_999_898, 9, 4, 6], [d2.year, d2.mon, d2.mday, d.wday])
380
381    d = Date.jd(-1824528942)
382    assert_equal([-5_000_000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
383    d2 = d.gregorian
384    assert_equal([-5_000_103, 4, 28, 3], [d2.year, d2.mon, d2.mday, d.wday])
385
386    d = Date.jd(-1824528942,Date::JULIAN)
387    assert_equal([-5_000_000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
388    d2 = d.gregorian
389    assert_equal([-5_000_103, 4, 28, 3], [d2.year, d2.mon, d2.mday, d.wday])
390
391    d = Date.jd(-1824491440,Date::GREGORIAN)
392    assert_equal([-5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
393    d2 = d.julian
394    assert_equal([-4_999_898, 9, 4, 6], [d2.year, d2.mon, d2.mday, d.wday])
395
396    # 5000000
397    d = Date.new(5_000_000,1,1)
398    assert_equal([5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
399    d2 = d.julian
400    assert_equal([4_999_897, 5, 3, 6], [d2.year, d2.mon, d2.mday, d.wday])
401
402    d = Date.new(5_000_000,1,1,Date::JULIAN)
403    assert_equal([5_000_000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
404    d2 = d.gregorian
405    assert_equal([5_000_102, 9, 1, 5], [d2.year, d2.mon, d2.mday, d.wday])
406
407    d = Date.new(5_000_000,1,1,Date::GREGORIAN)
408    assert_equal([5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
409    d2 = d.julian
410    assert_equal([4_999_897, 5, 3, 6], [d2.year, d2.mon, d2.mday, d.wday])
411
412    d = Date.jd(1827933560)
413    assert_equal([5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
414    d2 = d.julian
415    assert_equal([4_999_897, 5, 3, 6], [d2.year, d2.mon, d2.mday, d.wday])
416
417    d = Date.jd(1827971058,Date::JULIAN)
418    assert_equal([5_000_000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
419    d2 = d.gregorian
420    assert_equal([5_000_102, 9, 1, 5], [d2.year, d2.mon, d2.mday, d.wday])
421
422    d = Date.jd(1827933560,Date::GREGORIAN)
423    assert_equal([5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
424    d2 = d.julian
425    assert_equal([4_999_897, 5, 3, 6], [d2.year, d2.mon, d2.mday, d.wday])
426
427    # dt
428    d = DateTime.new(-123456789,2,3,4,5,6,0)
429    assert_equal([-123456789, 2, 3, 4, 5, 6, 1],
430		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.wday])
431    d2 = d.gregorian
432    assert_equal([-123459325, 12, 27, 4, 5, 6, 1],
433		 [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec, d.wday])
434
435    d = DateTime.new(123456789,2,3,4,5,6,0)
436    assert_equal([123456789, 2, 3, 4, 5, 6, 5],
437		 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.wday])
438    d2 = d.julian
439    assert_equal([123454254, 1, 19, 4, 5, 6, 5],
440		 [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec, d.wday])
441  end
442
443  def period2_iter2(from, to, sg)
444    (from..to).each do |j|
445      d = Date.jd(j, sg)
446      d2 = Date.new(d.year, d.mon, d.mday, sg)
447      assert_equal(d2.jd, j)
448      assert_equal(d2.ajd, d.ajd)
449      assert_equal(d2.year, d.year)
450
451      d = DateTime.jd(j, 12,0,0, '+12:00', sg)
452      d2 = DateTime.new(d.year, d.mon, d.mday,
453			d.hour, d.min, d.sec, d.offset, sg)
454      assert_equal(d2.jd, j)
455      assert_equal(d2.ajd, d.ajd)
456      assert_equal(d2.year, d.year)
457    end
458  end
459
460  def period2_iter(from, to)
461    period2_iter2(from, to, Date::GREGORIAN)
462    period2_iter2(from, to, Date::ITALY)
463    period2_iter2(from, to, Date::ENGLAND)
464    period2_iter2(from, to, Date::JULIAN)
465  end
466
467  def test_period2
468    cm_period0 = 71149239
469    cm_period = 0xfffffff.div(cm_period0) * cm_period0
470    period2_iter(-cm_period * (1 << 64) - 3, -cm_period * (1 << 64) + 3)
471    period2_iter(-cm_period - 3, -cm_period + 3)
472    period2_iter(0 - 3, 0 + 3)
473    period2_iter(+cm_period - 3, +cm_period + 3)
474    period2_iter(+cm_period * (1 << 64) - 3, +cm_period * (1 << 64) + 3)
475  end
476
477  def test_different_alignments
478    assert_equal(0, Date.jd(0) <=> Date.civil(-4713, 11, 24, Date::GREGORIAN))
479    assert_equal(0, Date.jd(213447717) <=> Date.civil(579687, 11, 24))
480    assert_equal(0, Date.jd(-213447717) <=> Date.civil(-589113, 11, 24, Date::GREGORIAN))
481
482    assert_equal(0, Date.jd(0) <=> DateTime.civil(-4713, 11, 24, 0, 0, 0, 0, Date::GREGORIAN))
483    assert_equal(0, Date.jd(213447717) <=> DateTime.civil(579687, 11, 24))
484    assert_equal(0, Date.jd(-213447717) <=> DateTime.civil(-589113, 11, 24, 0, 0, 0, 0, Date::GREGORIAN))
485
486    assert(Date.jd(0) == Date.civil(-4713, 11, 24, Date::GREGORIAN))
487    assert(Date.jd(213447717) == Date.civil(579687, 11, 24))
488    assert(Date.jd(-213447717) == Date.civil(-589113, 11, 24, Date::GREGORIAN))
489
490    assert(Date.jd(0) == DateTime.civil(-4713, 11, 24, 0, 0, 0, 0, Date::GREGORIAN))
491    assert(Date.jd(213447717) == DateTime.civil(579687, 11, 24))
492    assert(Date.jd(-213447717) == DateTime.civil(-589113, 11, 24, 0, 0, 0, 0, Date::GREGORIAN))
493
494    assert(Date.jd(0) === Date.civil(-4713, 11, 24, Date::GREGORIAN))
495    assert(Date.jd(213447717) === Date.civil(579687, 11, 24))
496    assert(Date.jd(-213447717) === Date.civil(-589113, 11, 24, Date::GREGORIAN))
497
498    assert(Date.jd(0) === DateTime.civil(-4713, 11, 24, 12, 0, 0, 0, Date::GREGORIAN))
499    assert(Date.jd(213447717) === DateTime.civil(579687, 11, 24, 12))
500    assert(Date.jd(-213447717) === DateTime.civil(-589113, 11, 24, 12, 0, 0, 0, Date::GREGORIAN))
501
502    a = Date.jd(0)
503    b = Date.civil(-4713, 11, 24, Date::GREGORIAN)
504    assert_equal(0, a <=> b)
505
506    a = Date.civil(-4712, 1, 1, Date::JULIAN)
507    b = Date.civil(-4713, 11, 24, Date::GREGORIAN)
508    a.jd; b.jd
509    assert_equal(0, a <=> b)
510
511    a = Date.jd(0)
512    b = Date.civil(-4713, 11, 24, Date::GREGORIAN)
513    assert(a == b)
514
515    a = Date.civil(-4712, 1, 1, Date::JULIAN)
516    b = Date.civil(-4713, 11, 24, Date::GREGORIAN)
517    a.jd; b.jd
518    assert(a == b)
519
520    a = Date.jd(0)
521    b = Date.civil(-4713, 11, 24, Date::GREGORIAN)
522    assert(a === b)
523
524    a = Date.civil(-4712, 1, 1, Date::JULIAN)
525    b = Date.civil(-4713, 11, 24, Date::GREGORIAN)
526    a.jd; b.jd
527    assert(a === b)
528  end
529
530  def test_marshal14
531    s = "\x04\x03u:\x01\x04Date\x01\v\x04\x03[\x01\x02i\x03\xE8i%T"
532    d = Marshal.load(s)
533    assert_equal(Rational(4903887,2), d.ajd)
534    assert_equal(0, d.send(:offset))
535    assert_equal(Date::GREGORIAN, d.start)
536  end
537
538  def test_marshal16
539    s = "\x04\x06u:\tDate\x0F\x04\x06[\ai\x03\xE8i%T"
540    d = Marshal.load(s)
541    assert_equal(Rational(4903887,2), d.ajd)
542    assert_equal(0, d.send(:offset))
543    assert_equal(Date::GREGORIAN, d.start)
544  end
545
546  def test_marshal18
547    s = "\x04\bu:\tDateP\x04\b[\bo:\rRational\a:\x0F@numeratori\x03\xCF\xD3J:\x11@denominatori\ai\x00o:\x13Date::Infinity\x06:\a@di\xFA"
548    d = Marshal.load(s)
549    assert_equal(Rational(4903887,2), d.ajd)
550    assert_equal(0, d.send(:offset))
551    assert_equal(Date::GREGORIAN, d.start)
552
553    s = "\x04\bu:\rDateTime`\x04\b[\bo:\rRational\a:\x0F@numeratorl+\b\xC9\xB0\x81\xBD\x02\x00:\x11@denominatori\x02\xC0\x12o;\x00\a;\x06i\b;\ai\ro:\x13Date::Infinity\x06:\a@di\xFA"
554    d = Marshal.load(s)
555    assert_equal(Rational(11769327817,4800), d.ajd)
556    assert_equal(Rational(9,24), d.offset)
557    assert_equal(Date::GREGORIAN, d.start)
558  end
559
560  def test_marshal192
561    s = "\x04\bU:\tDate[\bU:\rRational[\ai\x03\xCF\xD3Ji\ai\x00o:\x13Date::Infinity\x06:\a@di\xFA"
562    d = Marshal.load(s)
563    assert_equal(Rational(4903887,2), d.ajd)
564    assert_equal(Rational(0,24), d.send(:offset))
565    assert_equal(Date::GREGORIAN, d.start)
566
567    s = "\x04\bU:\rDateTime[\bU:\rRational[\al+\b\xC9\xB0\x81\xBD\x02\x00i\x02\xC0\x12U;\x06[\ai\bi\ro:\x13Date::Infinity\x06:\a@di\xFA"
568    d = Marshal.load(s)
569    assert_equal(Rational(11769327817,4800), d.ajd)
570    assert_equal(Rational(9,24), d.offset)
571    assert_equal(Date::GREGORIAN, d.start)
572  end
573
574  def test_taint
575    h = Date._strptime('15:43+09:00', '%R%z')
576    assert_equal(false, h[:zone].tainted?)
577    h = Date._strptime('15:43+09:00'.taint, '%R%z')
578    assert_equal(true, h[:zone].tainted?)
579
580    h = Date._strptime('1;1/0', '%d')
581    assert_equal(false, h[:leftover].tainted?)
582    h = Date._strptime('1;1/0'.taint, '%d')
583    assert_equal(true, h[:leftover].tainted?)
584
585    h = Date._parse('15:43+09:00')
586    assert_equal(false, h[:zone].tainted?)
587    h = Date._parse('15:43+09:00'.taint)
588    assert_equal(true, h[:zone].tainted?)
589
590    s = Date.today.strftime('new 105')
591    assert_equal(false, s.tainted?)
592    s = Date.today.strftime('new 105'.taint)
593    assert_equal(true, s.tainted?)
594    s = Date.today.strftime("new \000 105".taint)
595    assert_equal(true, s.tainted?)
596
597    s = DateTime.now.strftime('super $record')
598    assert_equal(false, s.tainted?)
599    s = DateTime.now.strftime('super $record'.taint)
600    assert_equal(true, s.tainted?)
601  end
602
603  def test_enc
604    Date::MONTHNAMES.each do |s|
605      assert_equal(Encoding::US_ASCII, s.encoding) if s
606    end
607    Date::DAYNAMES.each do |s|
608      assert_equal(Encoding::US_ASCII, s.encoding) if s
609    end
610    Date::ABBR_MONTHNAMES.each do |s|
611      assert_equal(Encoding::US_ASCII, s.encoding) if s
612    end
613    Date::ABBR_DAYNAMES.each do |s|
614      assert_equal(Encoding::US_ASCII, s.encoding) if s
615    end
616
617    h = Date._strptime('15:43+09:00'.force_encoding('euc-jp'), '%R%z')
618    assert_equal(Encoding::EUC_JP, h[:zone].encoding)
619    h = Date._strptime('15:43+09:00'.force_encoding('ascii-8bit'), '%R%z')
620    assert_equal(Encoding::ASCII_8BIT, h[:zone].encoding)
621
622    h = Date._strptime('1;1/0'.force_encoding('euc-jp'), '%d')
623    assert_equal(Encoding::EUC_JP, h[:leftover].encoding)
624    h = Date._strptime('1;1/0'.force_encoding('ascii-8bit'), '%d')
625    assert_equal(Encoding::ASCII_8BIT, h[:leftover].encoding)
626
627    h = Date._parse('15:43+09:00'.force_encoding('euc-jp'))
628    assert_equal(Encoding::EUC_JP, h[:zone].encoding)
629    h = Date._parse('15:43+09:00'.force_encoding('ascii-8bit'))
630    assert_equal(Encoding::ASCII_8BIT, h[:zone].encoding)
631
632    s = Date.today.strftime('new 105'.force_encoding('euc-jp'))
633    assert_equal(Encoding::EUC_JP, s.encoding)
634    s = Date.today.strftime('new 105'.force_encoding('ascii-8bit'))
635    assert_equal(Encoding::ASCII_8BIT, s.encoding)
636
637    s = DateTime.now.strftime('super $record'.force_encoding('euc-jp'))
638    assert_equal(Encoding::EUC_JP, s.encoding)
639    s = DateTime.now.strftime('super $record'.force_encoding('ascii-8bit'))
640    assert_equal(Encoding::ASCII_8BIT, s.encoding)
641  end
642
643  def test_dup
644    d = Date.new(2001,2,3)
645    d2 = d.dup
646    assert_not_equal(d.object_id, d2.object_id)
647    assert_kind_of(Date, d2)
648    assert_equal(d, d2)
649
650    d = DateTime.new(2001,2,3)
651    d2 = d.dup
652    assert_not_equal(d.object_id, d2.object_id)
653    assert_kind_of(DateTime, d2)
654    assert_equal(d, d2)
655  end
656
657  def test_base
658    skip unless defined?(Date.test_all)
659    assert_equal(true, Date.test_all)
660  end
661
662end
663