date-constructor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +10 -0
- data/LICENSE.txt +19 -0
- data/README.rdoc +0 -0
- data/Rakefile +84 -0
- data/date-constructor.gemspec +66 -0
- data/lib/date/constructor.rb +51 -0
- data/test/test_date.rb +145 -0
- data/test/test_date_arith.rb +286 -0
- data/test/test_date_attr.rb +112 -0
- data/test/test_date_base.rb +442 -0
- data/test/test_date_compat.rb +21 -0
- data/test/test_date_conv.rb +137 -0
- data/test/test_date_marshal.rb +29 -0
- data/test/test_date_new.rb +271 -0
- data/test/test_date_parse.rb +1097 -0
- data/test/test_date_strftime.rb +401 -0
- data/test/test_date_strptime.rb +486 -0
- data/test/test_switch_hitter.rb +577 -0
- metadata +142 -0
@@ -0,0 +1,577 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
class 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
|
+
end
|
298
|
+
|
299
|
+
def test_cmp
|
300
|
+
assert_equal(-1, Date.new(2001,2,3) <=> Date.new(2001,2,4))
|
301
|
+
assert_equal(0, Date.new(2001,2,3) <=> Date.new(2001,2,3))
|
302
|
+
assert_equal(1, Date.new(2001,2,3) <=> Date.new(2001,2,2))
|
303
|
+
|
304
|
+
assert_equal(-1, Date.new(2001,2,3) <=> 2451944.0)
|
305
|
+
assert_equal(-1, Date.new(2001,2,3) <=> 2451944)
|
306
|
+
assert_equal(0, Date.new(2001,2,3) <=> 2451943.5)
|
307
|
+
assert_equal(1, Date.new(2001,2,3) <=> 2451943.0)
|
308
|
+
assert_equal(1, Date.new(2001,2,3) <=> 2451943)
|
309
|
+
|
310
|
+
assert_equal(-1, Date.new(2001,2,3) <=> Rational('4903888/2'))
|
311
|
+
assert_equal(0, Date.new(2001,2,3) <=> Rational('4903887/2'))
|
312
|
+
assert_equal(1, Date.new(2001,2,3) <=> Rational('4903886/2'))
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_eqeqeq
|
316
|
+
assert_equal(false, Date.new(2001,2,3) === Date.new(2001,2,4))
|
317
|
+
assert_equal(true, Date.new(2001,2,3) === Date.new(2001,2,3))
|
318
|
+
assert_equal(false, Date.new(2001,2,3) === Date.new(2001,2,2))
|
319
|
+
|
320
|
+
assert_equal(true, Date.new(2001,2,3) === 2451944.0)
|
321
|
+
assert_equal(true, Date.new(2001,2,3) === 2451944)
|
322
|
+
assert_equal(false, Date.new(2001,2,3) === 2451943.5)
|
323
|
+
assert_equal(false, Date.new(2001,2,3) === 2451943.0)
|
324
|
+
assert_equal(false, Date.new(2001,2,3) === 2451943)
|
325
|
+
|
326
|
+
assert_equal(true, Date.new(2001,2,3) === Rational('4903888/2'))
|
327
|
+
assert_equal(false, Date.new(2001,2,3) === Rational('4903887/2'))
|
328
|
+
assert_equal(false, Date.new(2001,2,3) === Rational('4903886/2'))
|
329
|
+
end
|
330
|
+
|
331
|
+
def test_period
|
332
|
+
# -5000
|
333
|
+
d = Date.new(-5000,1,1)
|
334
|
+
assert_equal([-5000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
|
335
|
+
d2 = d.gregorian
|
336
|
+
assert_equal([-5001, 11, 22, 5], [d2.year, d2.mon, d2.mday, d.wday])
|
337
|
+
|
338
|
+
d = Date.new(-5000,1,1,Date::JULIAN)
|
339
|
+
assert_equal([-5000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
|
340
|
+
d2 = d.gregorian
|
341
|
+
assert_equal([-5001, 11, 22, 5], [d2.year, d2.mon, d2.mday, d.wday])
|
342
|
+
|
343
|
+
d = Date.new(-5000,1,1,Date::GREGORIAN)
|
344
|
+
assert_equal([-5000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
|
345
|
+
d2 = d.julian
|
346
|
+
assert_equal([-5000, 2, 10, 3], [d2.year, d2.mon, d2.mday, d.wday])
|
347
|
+
|
348
|
+
d = Date.jd(-105192)
|
349
|
+
assert_equal([-5000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
|
350
|
+
d2 = d.gregorian
|
351
|
+
assert_equal([-5001, 11, 22, 5], [d2.year, d2.mon, d2.mday, d.wday])
|
352
|
+
|
353
|
+
d = Date.jd(-105192,Date::JULIAN)
|
354
|
+
assert_equal([-5000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
|
355
|
+
d2 = d.gregorian
|
356
|
+
assert_equal([-5001, 11, 22, 5], [d2.year, d2.mon, d2.mday, d.wday])
|
357
|
+
|
358
|
+
d = Date.jd(-105152,Date::GREGORIAN)
|
359
|
+
assert_equal([-5000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
|
360
|
+
d2 = d.julian
|
361
|
+
assert_equal([-5000, 2, 10, 3], [d2.year, d2.mon, d2.mday, d.wday])
|
362
|
+
|
363
|
+
# -5000000
|
364
|
+
d = Date.new(-5_000_000,1,1)
|
365
|
+
assert_equal([-5_000_000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
|
366
|
+
d2 = d.gregorian
|
367
|
+
assert_equal([-5_000_103, 4, 28, 3], [d2.year, d2.mon, d2.mday, d.wday])
|
368
|
+
|
369
|
+
d = Date.new(-5_000_000,1,1,Date::JULIAN)
|
370
|
+
assert_equal([-5_000_000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
|
371
|
+
d2 = d.gregorian
|
372
|
+
assert_equal([-5_000_103, 4, 28, 3], [d2.year, d2.mon, d2.mday, d.wday])
|
373
|
+
|
374
|
+
d = Date.new(-5_000_000,1,1,Date::GREGORIAN)
|
375
|
+
assert_equal([-5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
|
376
|
+
d2 = d.julian
|
377
|
+
assert_equal([-4_999_898, 9, 4, 6], [d2.year, d2.mon, d2.mday, d.wday])
|
378
|
+
|
379
|
+
d = Date.jd(-1824528942)
|
380
|
+
assert_equal([-5_000_000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
|
381
|
+
d2 = d.gregorian
|
382
|
+
assert_equal([-5_000_103, 4, 28, 3], [d2.year, d2.mon, d2.mday, d.wday])
|
383
|
+
|
384
|
+
d = Date.jd(-1824528942,Date::JULIAN)
|
385
|
+
assert_equal([-5_000_000, 1, 1, 3], [d.year, d.mon, d.mday, d.wday])
|
386
|
+
d2 = d.gregorian
|
387
|
+
assert_equal([-5_000_103, 4, 28, 3], [d2.year, d2.mon, d2.mday, d.wday])
|
388
|
+
|
389
|
+
d = Date.jd(-1824491440,Date::GREGORIAN)
|
390
|
+
assert_equal([-5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
|
391
|
+
d2 = d.julian
|
392
|
+
assert_equal([-4_999_898, 9, 4, 6], [d2.year, d2.mon, d2.mday, d.wday])
|
393
|
+
|
394
|
+
# 5000000
|
395
|
+
d = Date.new(5_000_000,1,1)
|
396
|
+
assert_equal([5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
|
397
|
+
d2 = d.julian
|
398
|
+
assert_equal([4_999_897, 5, 3, 6], [d2.year, d2.mon, d2.mday, d.wday])
|
399
|
+
|
400
|
+
d = Date.new(5_000_000,1,1,Date::JULIAN)
|
401
|
+
assert_equal([5_000_000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
|
402
|
+
d2 = d.gregorian
|
403
|
+
assert_equal([5_000_102, 9, 1, 5], [d2.year, d2.mon, d2.mday, d.wday])
|
404
|
+
|
405
|
+
d = Date.new(5_000_000,1,1,Date::GREGORIAN)
|
406
|
+
assert_equal([5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
|
407
|
+
d2 = d.julian
|
408
|
+
assert_equal([4_999_897, 5, 3, 6], [d2.year, d2.mon, d2.mday, d.wday])
|
409
|
+
|
410
|
+
d = Date.jd(1827933560)
|
411
|
+
assert_equal([5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
|
412
|
+
d2 = d.julian
|
413
|
+
assert_equal([4_999_897, 5, 3, 6], [d2.year, d2.mon, d2.mday, d.wday])
|
414
|
+
|
415
|
+
d = Date.jd(1827971058,Date::JULIAN)
|
416
|
+
assert_equal([5_000_000, 1, 1, 5], [d.year, d.mon, d.mday, d.wday])
|
417
|
+
d2 = d.gregorian
|
418
|
+
assert_equal([5_000_102, 9, 1, 5], [d2.year, d2.mon, d2.mday, d.wday])
|
419
|
+
|
420
|
+
d = Date.jd(1827933560,Date::GREGORIAN)
|
421
|
+
assert_equal([5_000_000, 1, 1, 6], [d.year, d.mon, d.mday, d.wday])
|
422
|
+
d2 = d.julian
|
423
|
+
assert_equal([4_999_897, 5, 3, 6], [d2.year, d2.mon, d2.mday, d.wday])
|
424
|
+
|
425
|
+
# dt
|
426
|
+
d = DateTime.new(-123456789,2,3,4,5,6,0)
|
427
|
+
assert_equal([-123456789, 2, 3, 4, 5, 6, 1],
|
428
|
+
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.wday])
|
429
|
+
d2 = d.gregorian
|
430
|
+
assert_equal([-123459325, 12, 27, 4, 5, 6, 1],
|
431
|
+
[d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec, d.wday])
|
432
|
+
|
433
|
+
d = DateTime.new(123456789,2,3,4,5,6,0)
|
434
|
+
assert_equal([123456789, 2, 3, 4, 5, 6, 5],
|
435
|
+
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.wday])
|
436
|
+
d2 = d.julian
|
437
|
+
assert_equal([123454254, 1, 19, 4, 5, 6, 5],
|
438
|
+
[d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec, d.wday])
|
439
|
+
end
|
440
|
+
|
441
|
+
def period2_iter2(from, to, sg)
|
442
|
+
(from..to).each do |j|
|
443
|
+
d = Date.jd(j, sg)
|
444
|
+
d2 = Date.new(d.year, d.mon, d.mday, sg)
|
445
|
+
assert_equal(d2.jd, j)
|
446
|
+
assert_equal(d2.ajd, d.ajd)
|
447
|
+
assert_equal(d2.year, d.year)
|
448
|
+
|
449
|
+
d = DateTime.jd(j, 12,0,0, '+12:00', sg)
|
450
|
+
d2 = DateTime.new(d.year, d.mon, d.mday,
|
451
|
+
d.hour, d.min, d.sec, d.offset, sg)
|
452
|
+
assert_equal(d2.jd, j)
|
453
|
+
assert_equal(d2.ajd, d.ajd)
|
454
|
+
assert_equal(d2.year, d.year)
|
455
|
+
end
|
456
|
+
end
|
457
|
+
|
458
|
+
def period2_iter(from, to)
|
459
|
+
period2_iter2(from, to, Date::GREGORIAN)
|
460
|
+
period2_iter2(from, to, Date::ITALY)
|
461
|
+
period2_iter2(from, to, Date::ENGLAND)
|
462
|
+
period2_iter2(from, to, Date::JULIAN)
|
463
|
+
end
|
464
|
+
|
465
|
+
def test_period2
|
466
|
+
cm_period0 = 71149239
|
467
|
+
cm_period = 0xfffffff.div(cm_period0) * cm_period0
|
468
|
+
period2_iter(-cm_period * (1 << 64) - 3, -cm_period * (1 << 64) + 3)
|
469
|
+
period2_iter(-cm_period - 3, -cm_period + 3)
|
470
|
+
period2_iter(0 - 3, 0 + 3)
|
471
|
+
period2_iter(+cm_period - 3, +cm_period + 3)
|
472
|
+
period2_iter(+cm_period * (1 << 64) - 3, +cm_period * (1 << 64) + 3)
|
473
|
+
end
|
474
|
+
|
475
|
+
def test_marshal
|
476
|
+
s = "\x04\bU:\tDate[\bU:\rRational[\ai\x03\xCF\xD3Ji\ai\x00o:\x13Date::Infinity\x06:\a@di\xFA"
|
477
|
+
d = Marshal.load(s)
|
478
|
+
assert_equal(Rational(4903887,2), d.ajd)
|
479
|
+
assert_equal(Rational(0,24), d.send(:offset))
|
480
|
+
assert_equal(Date::GREGORIAN, d.start)
|
481
|
+
|
482
|
+
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"
|
483
|
+
d = Marshal.load(s)
|
484
|
+
assert_equal(Rational(11769327817,4800), d.ajd)
|
485
|
+
assert_equal(Rational(9,24), d.offset)
|
486
|
+
assert_equal(Date::GREGORIAN, d.start)
|
487
|
+
end
|
488
|
+
|
489
|
+
def test_taint
|
490
|
+
h = Date._strptime('15:43+09:00', '%R%z')
|
491
|
+
assert_equal(false, h[:zone].tainted?)
|
492
|
+
h = Date._strptime('15:43+09:00'.taint, '%R%z')
|
493
|
+
assert_equal(true, h[:zone].tainted?)
|
494
|
+
|
495
|
+
h = Date._strptime('1;1/0', '%d')
|
496
|
+
assert_equal(false, h[:leftover].tainted?)
|
497
|
+
h = Date._strptime('1;1/0'.taint, '%d')
|
498
|
+
assert_equal(true, h[:leftover].tainted?)
|
499
|
+
|
500
|
+
h = Date._parse('15:43+09:00')
|
501
|
+
assert_equal(false, h[:zone].tainted?)
|
502
|
+
h = Date._parse('15:43+09:00'.taint)
|
503
|
+
assert_equal(true, h[:zone].tainted?)
|
504
|
+
|
505
|
+
s = Date.today.strftime('new 105')
|
506
|
+
assert_equal(false, s.tainted?)
|
507
|
+
s = Date.today.strftime('new 105'.taint)
|
508
|
+
assert_equal(true, s.tainted?)
|
509
|
+
s = Date.today.strftime("new \000 105".taint)
|
510
|
+
assert_equal(true, s.tainted?)
|
511
|
+
|
512
|
+
s = DateTime.now.strftime('super $record')
|
513
|
+
assert_equal(false, s.tainted?)
|
514
|
+
s = DateTime.now.strftime('super $record'.taint)
|
515
|
+
assert_equal(true, s.tainted?)
|
516
|
+
end
|
517
|
+
|
518
|
+
def test_enc
|
519
|
+
Date::MONTHNAMES.each do |s|
|
520
|
+
assert_equal(Encoding::US_ASCII, s.encoding) if s
|
521
|
+
end
|
522
|
+
Date::DAYNAMES.each do |s|
|
523
|
+
assert_equal(Encoding::US_ASCII, s.encoding) if s
|
524
|
+
end
|
525
|
+
Date::ABBR_MONTHNAMES.each do |s|
|
526
|
+
assert_equal(Encoding::US_ASCII, s.encoding) if s
|
527
|
+
end
|
528
|
+
Date::ABBR_DAYNAMES.each do |s|
|
529
|
+
assert_equal(Encoding::US_ASCII, s.encoding) if s
|
530
|
+
end
|
531
|
+
|
532
|
+
h = Date._strptime('15:43+09:00'.force_encoding('euc-jp'), '%R%z')
|
533
|
+
assert_equal(Encoding::EUC_JP, h[:zone].encoding)
|
534
|
+
h = Date._strptime('15:43+09:00'.force_encoding('ascii-8bit'), '%R%z')
|
535
|
+
assert_equal(Encoding::ASCII_8BIT, h[:zone].encoding)
|
536
|
+
|
537
|
+
h = Date._strptime('1;1/0'.force_encoding('euc-jp'), '%d')
|
538
|
+
assert_equal(Encoding::EUC_JP, h[:leftover].encoding)
|
539
|
+
h = Date._strptime('1;1/0'.force_encoding('ascii-8bit'), '%d')
|
540
|
+
assert_equal(Encoding::ASCII_8BIT, h[:leftover].encoding)
|
541
|
+
|
542
|
+
h = Date._parse('15:43+09:00'.force_encoding('euc-jp'))
|
543
|
+
assert_equal(Encoding::EUC_JP, h[:zone].encoding)
|
544
|
+
h = Date._parse('15:43+09:00'.force_encoding('ascii-8bit'))
|
545
|
+
assert_equal(Encoding::ASCII_8BIT, h[:zone].encoding)
|
546
|
+
|
547
|
+
s = Date.today.strftime('new 105'.force_encoding('euc-jp'))
|
548
|
+
assert_equal(Encoding::EUC_JP, s.encoding)
|
549
|
+
s = Date.today.strftime('new 105'.force_encoding('ascii-8bit'))
|
550
|
+
assert_equal(Encoding::ASCII_8BIT, s.encoding)
|
551
|
+
|
552
|
+
s = DateTime.now.strftime('super $record'.force_encoding('euc-jp'))
|
553
|
+
assert_equal(Encoding::EUC_JP, s.encoding)
|
554
|
+
s = DateTime.now.strftime('super $record'.force_encoding('ascii-8bit'))
|
555
|
+
assert_equal(Encoding::ASCII_8BIT, s.encoding)
|
556
|
+
end
|
557
|
+
|
558
|
+
def test_dup
|
559
|
+
d = Date.new(2001,2,3)
|
560
|
+
d2 = d.dup
|
561
|
+
assert_not_equal(d.object_id, d2.object_id)
|
562
|
+
assert_kind_of(Date, d2)
|
563
|
+
assert_equal(d, d2)
|
564
|
+
|
565
|
+
d = DateTime.new(2001,2,3)
|
566
|
+
d2 = d.dup
|
567
|
+
assert_not_equal(d.object_id, d2.object_id)
|
568
|
+
assert_kind_of(DateTime, d2)
|
569
|
+
assert_equal(d, d2)
|
570
|
+
end
|
571
|
+
|
572
|
+
def test_base
|
573
|
+
skip unless defined?(Date.test_all)
|
574
|
+
assert_equal(true, Date.test_all)
|
575
|
+
end
|
576
|
+
|
577
|
+
end
|