date-constructor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ require 'test/unit'
2
+ require 'date'
3
+
4
+ class TestDateCompat < Test::Unit::TestCase
5
+
6
+ def test_compat
7
+ assert_equal(DateTime.new, Date.new)
8
+ assert_equal(DateTime.new(2002,3,19), Date.new(2002,3,19))
9
+ assert_equal(DateTime.new(2002,3,19, 0,0,0), Date.new(2002,3,19))
10
+ assert_equal(DateTime.new(2002,3,19, 0,0,0, 0), Date.new(2002,3,19))
11
+ assert_equal(DateTime.new(2002,3,19, 0,0,0, 0.to_r), Date.new(2002,3,19))
12
+ assert_equal(DateTime.new(2002,3,19, 0,0,0, 0, Date::GREGORIAN), Date.new(2002,3,19, Date::GREGORIAN))
13
+ assert_equal(DateTime.new(2002,3,19, 0,0,0, 0, Date::JULIAN), Date.new(2002,3,19, Date::JULIAN))
14
+
15
+ assert(Date.new(2002,3,19) != DateTime.new(2002,3,19, 12,0,0))
16
+ assert(Date.new(2002,3,19) != DateTime.new(2002,3,19, 0,0,1))
17
+ assert(Date.new(2002,3,19) === DateTime.new(2002,3,19, 12,0,0))
18
+ assert(Date.new(2002,3,19) === DateTime.new(2002,3,19, 0,0,1))
19
+ end
20
+
21
+ end
@@ -0,0 +1,137 @@
1
+ require 'test/unit'
2
+ require 'date'
3
+
4
+ class TestDateConv < Test::Unit::TestCase
5
+
6
+ def test_to_class
7
+ [Time.now, Date.today, DateTime.now].each do |o|
8
+ assert_instance_of(Time, o.to_time)
9
+ assert_instance_of(Date, o.to_date)
10
+ assert_instance_of(DateTime, o.to_datetime)
11
+ end
12
+ end
13
+
14
+ def test_to_time__from_time
15
+ t = Time.mktime(2004, 9, 19, 1, 2, 3, 456789)
16
+ t2 = t.to_time
17
+ assert_equal([2004, 9, 19, 1, 2, 3, 456789],
18
+ [t2.year, t2.mon, t2.mday, t2.hour, t2.min, t2.sec, t2.usec])
19
+
20
+ t = Time.utc(2004, 9, 19, 1, 2, 3, 456789)
21
+ t2 = t.to_time.utc
22
+ assert_equal([2004, 9, 19, 1, 2, 3, 456789],
23
+ [t2.year, t2.mon, t2.mday, t2.hour, t2.min, t2.sec, t2.usec])
24
+ end
25
+
26
+ def test_to_time__from_date
27
+ d = Date.new(2004, 9, 19)
28
+ t = d.to_time
29
+ assert_equal([2004, 9, 19, 0, 0, 0, 0],
30
+ [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec])
31
+ end
32
+
33
+ def test_to_time__from_datetime
34
+ d = DateTime.new(2004, 9, 19, 1, 2, 3, 9.to_r/24) + 456789.to_r/86400000000
35
+ t = d.to_time
36
+ if t.utc_offset == 9*60*60
37
+ assert_equal([2004, 9, 19, 1, 2, 3, 456789],
38
+ [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec])
39
+ end
40
+
41
+ d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789.to_r/86400000000
42
+ t = d.to_time.utc
43
+ assert_equal([2004, 9, 19, 1, 2, 3, 456789],
44
+ [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec])
45
+
46
+ if Time.allocate.respond_to?(:nsec)
47
+ d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123.to_r/86400000000000
48
+ t = d.to_time.utc
49
+ assert_equal([2004, 9, 19, 1, 2, 3, 456789123],
50
+ [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.nsec])
51
+ end
52
+
53
+ if Time.allocate.respond_to?(:subsec)
54
+ d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123456789123.to_r/86400000000000000000000
55
+ t = d.to_time.utc
56
+ assert_equal([2004, 9, 19, 1, 2, 3, Rational(456789123456789123,1000000000000000000)],
57
+ [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.subsec])
58
+ end
59
+ end
60
+
61
+ def test_to_date__from_time
62
+ t = Time.mktime(2004, 9, 19, 1, 2, 3, 456789)
63
+ d = t.to_date
64
+ assert_equal([2004, 9, 19, 0], [d.year, d.mon, d.mday, d.day_fraction])
65
+
66
+ t = Time.utc(2004, 9, 19, 1, 2, 3, 456789)
67
+ d = t.to_date
68
+ assert_equal([2004, 9, 19, 0], [d.year, d.mon, d.mday, d.day_fraction])
69
+ end
70
+
71
+ def test_to_date__from_date
72
+ d = Date.new(2004, 9, 19) + 1.to_r/2
73
+ d2 = d.to_date
74
+ assert_equal([2004, 9, 19, 1.to_r/2],
75
+ [d2.year, d2.mon, d2.mday, d2.day_fraction])
76
+ end
77
+
78
+ def test_to_date__from_datetime
79
+ d = DateTime.new(2004, 9, 19, 1, 2, 3, 9.to_r/24) + 456789.to_r/86400000000
80
+ d2 = d.to_date
81
+ assert_equal([2004, 9, 19, 0], [d2.year, d2.mon, d2.mday, d2.day_fraction])
82
+
83
+ d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789.to_r/86400000000
84
+ d2 = d.to_date
85
+ assert_equal([2004, 9, 19, 0], [d2.year, d2.mon, d2.mday, d2.day_fraction])
86
+ end
87
+
88
+ def test_to_datetime__from_time
89
+ t = Time.mktime(2004, 9, 19, 1, 2, 3, 456789)
90
+ d = t.to_datetime
91
+ assert_equal([2004, 9, 19, 1, 2, 3,
92
+ 456789.to_r/1000000,
93
+ t.utc_offset.to_r/86400],
94
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec,
95
+ d.sec_fraction, d.offset])
96
+
97
+ t = Time.utc(2004, 9, 19, 1, 2, 3, 456789)
98
+ d = t.to_datetime
99
+ assert_equal([2004, 9, 19, 1, 2, 3,
100
+ 456789.to_r/1000000,
101
+ 0],
102
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec,
103
+ d.sec_fraction, d.offset])
104
+
105
+ t = Time.now
106
+ d = t.to_datetime
107
+ require 'time'
108
+ assert_equal(t.iso8601(10), d.iso8601(10))
109
+ end
110
+
111
+ def test_to_datetime__from_date
112
+ d = Date.new(2004, 9, 19) + 1.to_r/2
113
+ d2 = d.to_datetime
114
+ assert_equal([2004, 9, 19, 0, 0, 0, 0, 0],
115
+ [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec,
116
+ d2.sec_fraction, d2.offset])
117
+ end
118
+
119
+ def test_to_datetime__from_datetime
120
+ d = DateTime.new(2004, 9, 19, 1, 2, 3, 9.to_r/24) + 456789.to_r/86400000000
121
+ d2 = d.to_datetime
122
+ assert_equal([2004, 9, 19, 1, 2, 3,
123
+ 456789.to_r/1000000,
124
+ 9.to_r/24],
125
+ [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec,
126
+ d2.sec_fraction, d2.offset])
127
+
128
+ d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789.to_r/86400000000
129
+ d2 = d.to_datetime
130
+ assert_equal([2004, 9, 19, 1, 2, 3,
131
+ 456789.to_r/1000000,
132
+ 0],
133
+ [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec,
134
+ d2.sec_fraction, d2.offset])
135
+ end
136
+
137
+ end
@@ -0,0 +1,29 @@
1
+ require 'test/unit'
2
+ require 'date'
3
+
4
+ class TestDateMarshal < Test::Unit::TestCase
5
+
6
+ def test_marshal
7
+ d = Date.new
8
+ m = Marshal.dump(d)
9
+ d2 = Marshal.load(m)
10
+ assert_equal(d, d2)
11
+ assert_equal(d.start, d2.start)
12
+ assert_instance_of(String, d2.to_s)
13
+
14
+ d = Date.today
15
+ m = Marshal.dump(d)
16
+ d2 = Marshal.load(m)
17
+ assert_equal(d, d2)
18
+ assert_equal(d.start, d2.start)
19
+ assert_instance_of(String, d2.to_s)
20
+
21
+ d = DateTime.now
22
+ m = Marshal.dump(d)
23
+ d2 = Marshal.load(m)
24
+ assert_equal(d, d2)
25
+ assert_equal(d.start, d2.start)
26
+ assert_instance_of(String, d2.to_s)
27
+ end
28
+
29
+ end
@@ -0,0 +1,271 @@
1
+ require 'test/unit'
2
+ require 'date'
3
+
4
+ class 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
+
271
+ end