summaryrefslogtreecommitdiff
path: root/jni/ruby/test/date/test_date_new.rb
blob: 0bbbfee44ba1929c493d4acc70e39af596d69c21 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require 'test/unit'
require 'date'

class TestDateNew < Test::Unit::TestCase

  def test_jd
    d = Date.jd
    dt = DateTime.jd
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])

    d2 = Date.jd
    dt2 = DateTime.jd
    assert_equal(d, d2)
    assert_equal(dt, dt2)

    d = Date.jd(0)
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
    d = DateTime.jd(0, 0,0,0, 0)
    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
    d = DateTime.jd(0, 0,0,0, '+0900')
    assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  end

  def test_jd__ex
    assert_raise(ArgumentError) do
      DateTime.jd(0, 23,59,60,0)
    end
  end

  def test_ordinal
    d = Date.ordinal
    dt = DateTime.ordinal
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])

    d2 = Date.ordinal
    dt2 = DateTime.ordinal
    assert_equal(d, d2)
    assert_equal(dt, dt2)

    d = Date.ordinal(-4712,1)
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])

    d = Date.ordinal(-4712,1.0)
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])

    d = DateTime.ordinal(-4712,1, 0,0,0, 0)
    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
    d = DateTime.ordinal(-4712,1, 0,0,0, '+0900')
    assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  end

  def test_ordinal__neg
    d = Date.ordinal(-1,-1)
    assert_equal([-1, 365], [d.year, d.yday])

    d = DateTime.ordinal(-1,-1, -1,-1,-1, 0)
    assert_equal([-1, 365, 23, 59, 59, 0],
                 [d.year, d.yday, d.hour, d.min, d.sec, d.offset])
  end

  def test_ordinal__ex
    assert_raise(ArgumentError) do
      Date.ordinal(2001,366)
    end
    assert_raise(ArgumentError) do
      DateTime.ordinal(2001,365, 23,59,60, 0)
    end
  end

  def test_civil
    d = Date.civil
    dt = DateTime.civil
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])

    d2 = Date.civil
    dt2 = DateTime.civil
    assert_equal(d, d2)
    assert_equal(dt, dt2)

    d = Date.civil(-4712,1,1)
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])

    d = Date.civil(-4712,1,1.0)
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])

    d = DateTime.civil(-4712,1,1, 0,0,0, 0)
    assert_equal([-4712, 1, 1, 0, 0, 0, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
    d = DateTime.civil(-4712,1,1, 0,0,0, '+0900')
    assert_equal([-4712, 1, 1, 0, 0, 0, 9.to_r/24],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])


    d = DateTime.civil(2001,2,3 + 1.to_r/2)
    assert_equal([2001, 2, 3, 12, 0, 0, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
    d = DateTime.civil(2001,2,3, 4 + 1.to_r/2)
    assert_equal([2001, 2, 3, 4, 30, 0, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
    d = DateTime.civil(2001,2,3, 4,5 + 1.to_r/2)
    assert_equal([2001, 2, 3, 4, 5, 30, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
    d = DateTime.civil(2001,2,3, 4,5,6 + 1.to_r/2)
    assert_equal([2001, 2, 3, 4, 5, 6, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
    assert_equal(1.to_r/2, d.sec_fraction)
  end

  def test_civil__neg
    d = Date.civil(-1,-1,-1)
    assert_equal([-1, 12, 31], [d.year, d.mon, d.mday])

    d = DateTime.civil(-1,-1,-1, -1,-1,-1, 0)
    assert_equal([-1, 12, 31, 23, 59, 59, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  end

  def test_civil__ex
    assert_raise(ArgumentError) do
      Date.civil(2001,2,29)
    end
    assert_raise(ArgumentError) do
      DateTime.civil(2001,2,28, 23,59,60, 0)
    end
    assert_raise(ArgumentError) do
      DateTime.civil(2001,2,28, 24,59,59, 0)
    end
  end

  def test_civil__reform
    d = Date.jd(Date::ENGLAND, Date::ENGLAND)
    dt = DateTime.jd(Date::ENGLAND, 0,0,0,0, Date::ENGLAND)
    assert_equal([1752, 9, 14], [d.year, d.mon, d.mday])
    assert_equal([1752, 9, 14], [dt.year, dt.mon, dt.mday])
    d -= 1
    dt -= 1
    assert_equal([1752, 9, 2], [d.year, d.mon, d.mday])
    assert_equal([1752, 9, 2], [dt.year, dt.mon, dt.mday])

    d = Date.jd(Date::ITALY, Date::ITALY)
    dt = DateTime.jd(Date::ITALY, 0,0,0,0, Date::ITALY)
    assert_equal([1582, 10, 15], [d.year, d.mon, d.mday])
    assert_equal([1582, 10, 15], [dt.year, dt.mon, dt.mday])
    d -= 1
    dt -= 1
    assert_equal([1582, 10, 4], [d.year, d.mon, d.mday])
    assert_equal([1582, 10, 4], [dt.year, dt.mon, dt.mday])
  end

  def test_commercial
    d = Date.commercial
    dt = DateTime.commercial
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])

    d2 = Date.commercial
    dt2 = DateTime.commercial
    assert_equal(d, d2)
    assert_equal(dt, dt2)

    d = Date.commercial(1582,40,5)
    assert_equal([1582, 10, 15], [d.year, d.mon, d.mday])

    d = Date.commercial(1582,40,5.0)
    assert_equal([1582, 10, 15], [d.year, d.mon, d.mday])

    d = DateTime.commercial(1582,40,5, 0,0,0, 0)
    assert_equal([1582, 10, 15, 0, 0, 0, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
    d = DateTime.commercial(1582,40,5, 0,0,0, '+0900')
    assert_equal([1582, 10, 15, 0, 0, 0, 9.to_r/24],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  end

  def test_commercial__neg
    d = Date.commercial(1998,-1,-1)
    assert_equal([1999, 1, 3], [d.year, d.mon, d.mday])

    d = DateTime.commercial(1998,-1,-1, -1,-1,-1, 0)
    assert_equal([1999, 1, 3, 23, 59, 59, 0],
                 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
  end

  def test_commercial__ex
    assert_raise(ArgumentError) do
      Date.commercial(1997,53,1)
    end
    assert_raise(ArgumentError) do
      DateTime.commercial(1997,52,1, 23,59,60, 0)
    end
  end

  def test_weeknum
    skip unless Date.respond_to?(:weeknum, true)
    d = Date.__send__(:weeknum)
    dt = DateTime.__send__(:weeknum)
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])

    d = Date.__send__(:weeknum, 2002,11,4, 0)
    assert_equal(2452355, d.jd)

    d = DateTime.__send__(:weeknum, 2002,11,4, 0, 11,22,33)
    assert_equal(2452355, d.jd)
    assert_equal([11,22,33], [d.hour, d.min, d.sec])

    assert_raise(ArgumentError) do
      Date.__send__(:weeknum, 1999,53,0, 0)
    end
    assert_raise(ArgumentError) do
      Date.__send__(:weeknum, 1999,-53,-1, 0)
    end
  end

  def test_nth_kday
    skip unless Date.respond_to?(:nth_kday, true)
    d = Date.__send__(:nth_kday)
    dt = DateTime.__send__(:nth_kday)
    assert_equal([-4712, 1, 1], [d.year, d.mon, d.mday])
    assert_equal([-4712, 1, 1], [dt.year, dt.mon, dt.mday])
    assert_equal([0, 0, 0], [dt.hour, dt.min, dt.sec])

    d = Date.__send__(:nth_kday, 1992,2, 5,6)
    assert_equal(2448682, d.jd)

    d = DateTime.__send__(:nth_kday, 1992,2, 5,6, 11,22,33)
    assert_equal(2448682, d.jd)
    assert_equal([11,22,33], [d.hour, d.min, d.sec])

    assert_raise(ArgumentError) do
      Date.__send__(:nth_kday, 2006,5, 5,0)
    end
    assert_raise(ArgumentError) do
      Date.__send__(:nth_kday, 2006,5, -5,0)
    end
  end

  def test_today
    z = Time.now
    d = Date.today
    t = Time.now
    t2 = Time.utc(t.year, t.mon, t.mday)
    t3 = Time.utc(d.year, d.mon, d.mday)
    assert_in_delta(t2, t3, t - z + 2)

    assert_equal(false, DateTime.respond_to?(:today))
  end

  def test_now
    assert_equal(false, Date.respond_to?(:now))

    z = Time.now
    d = DateTime.now
    t = Time.now
    t2 = Time.local(d.year, d.mon, d.mday, d.hour, d.min, d.sec)
    assert_in_delta(t, t2, t - z + 2)
  end

end