chronos 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +27 -0
- data/HISTORY.rdoc +4 -0
- data/LICENSE.txt +52 -0
- data/MANIFEST.txt +51 -0
- data/NOTES.rdoc +85 -0
- data/README.rdoc +125 -0
- data/Rakefile +34 -0
- data/TODO.rdoc +63 -0
- data/bench/completebench.rb +24 -0
- data/ext/cchronos/extconf.rb +5 -0
- data/ext/chronos_core/extconf.rb +5 -0
- data/lib/chronos.rb +208 -0
- data/lib/chronos/calendar.rb +16 -0
- data/lib/chronos/calendar/gregorian.rb +94 -0
- data/lib/chronos/data/zones.tab +424 -0
- data/lib/chronos/datetime.rb +299 -0
- data/lib/chronos/datetime/gregorian.rb +698 -0
- data/lib/chronos/duration.rb +141 -0
- data/lib/chronos/duration/gregorian.rb +261 -0
- data/lib/chronos/durationtotext.rb +42 -0
- data/lib/chronos/exceptions.rb +16 -0
- data/lib/chronos/gregorian.rb +27 -0
- data/lib/chronos/interval.rb +132 -0
- data/lib/chronos/interval/gregorian.rb +80 -0
- data/lib/chronos/locale/parsers/de_CH.rb +50 -0
- data/lib/chronos/locale/parsers/en_US.rb +1 -0
- data/lib/chronos/locale/parsers/generic.rb +21 -0
- data/lib/chronos/locale/strings/de_DE.yaml +76 -0
- data/lib/chronos/locale/strings/en_US.yaml +76 -0
- data/lib/chronos/minimalistic.rb +37 -0
- data/lib/chronos/numeric/gregorian.rb +100 -0
- data/lib/chronos/ruby.rb +6 -0
- data/lib/chronos/version.rb +21 -0
- data/lib/chronos/zone.rb +212 -0
- data/rake/initialize.rb +116 -0
- data/rake/lib/assesscode.rb +59 -0
- data/rake/lib/bonesplitter.rb +245 -0
- data/rake/lib/projectclass.rb +69 -0
- data/rake/tasks/copyright.rake +24 -0
- data/rake/tasks/gem.rake +119 -0
- data/rake/tasks/git.rake +40 -0
- data/rake/tasks/loc.rake +33 -0
- data/rake/tasks/manifest.rake +63 -0
- data/rake/tasks/meta.rake +16 -0
- data/rake/tasks/notes.rake +36 -0
- data/rake/tasks/post_load.rake +18 -0
- data/rake/tasks/rdoc.rake +73 -0
- data/rake/tasks/rubyforge.rake +67 -0
- data/rake/tasks/spec.rake +55 -0
- data/spec/bacon_helper.rb +43 -0
- data/spec/lib/chronos/datetime/gregorian_spec.rb +314 -0
- data/spec/lib/chronos/datetime_spec.rb +219 -0
- data/spec/lib/chronos_spec.rb +91 -0
- metadata +111 -0
@@ -0,0 +1,314 @@
|
|
1
|
+
unless $LOADED_FEATURES.include?('bacon_helper.rb')
|
2
|
+
load(File.expand_path("#{__FILE__}/../../../../bacon_helper.rb"))
|
3
|
+
$LOADED_FEATURES << 'bacon_helper.rb'
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'time'
|
7
|
+
require 'date'
|
8
|
+
require 'chronos/datetime/gregorian'
|
9
|
+
|
10
|
+
GDT = Chronos::Datetime::Gregorian
|
11
|
+
|
12
|
+
describe 'Chronos::Datetime::Gregorian' do
|
13
|
+
describe 'creating valid dates' do
|
14
|
+
it 'should be able to create a civil date' do
|
15
|
+
proc {
|
16
|
+
GDT.civil(2008, 10, 20)
|
17
|
+
}.should.not.raise
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should be able to create an ordinal date' do
|
21
|
+
proc {
|
22
|
+
GDT.ordinal(2008, 200)
|
23
|
+
}.should.not.raise
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should be able to create a commercial date' do
|
27
|
+
proc {
|
28
|
+
GDT.commercial(2008, 5, 11)
|
29
|
+
}.should.not.raise
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should be able to create a civil date with time'
|
33
|
+
it 'should be able to create an ordinal date with time'
|
34
|
+
it 'should be able to create a commercial date with time'
|
35
|
+
|
36
|
+
describe '::iso_8601' do
|
37
|
+
it 'Should parse YYYY-MM-DD>T<HH:MM:SS±HH:SS correctly' do
|
38
|
+
result = nil
|
39
|
+
proc {
|
40
|
+
result = GDT.iso_8601('2001-12-31T09:41:29+02:00')
|
41
|
+
}.should.not.raise
|
42
|
+
result.year.should.equal 2001
|
43
|
+
result.month.should.equal 12
|
44
|
+
result.day_of_month.should.equal 31
|
45
|
+
result.hour.should.equal 9
|
46
|
+
result.minute.should.equal 41
|
47
|
+
result.second.should.equal 29
|
48
|
+
result.offset.seconds.should.equal 7200
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'creating invalid dates' do
|
54
|
+
it 'should raise with an invalid month'
|
55
|
+
it 'should raise with an invalid day of month'
|
56
|
+
it 'should raise with an invalid week'
|
57
|
+
it 'should raise with an invalid day of week'
|
58
|
+
it 'should raise with an invalid day of year'
|
59
|
+
it 'should raise with a non integral value for year'
|
60
|
+
it 'should raise with a non integral value for month'
|
61
|
+
it 'should raise with a non integral value for week'
|
62
|
+
it 'should raise with a non integral value for day of year'
|
63
|
+
it 'should raise with a non integral value for day of month'
|
64
|
+
it 'should raise with a non integral value for day of week'
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'a created datetimes attributes' do
|
68
|
+
it 'should have a year attribute, returning an Integer'
|
69
|
+
it 'should have a month attribute, returning an Integer'
|
70
|
+
it 'should have a week attribute, returning an Integer'
|
71
|
+
it 'should have a day (day of month) attribute, returning an Integer'
|
72
|
+
it 'should have a day_of_year attribute, returning an Integer'
|
73
|
+
it 'should have a day_of_month attribute, returning an Integer'
|
74
|
+
it 'should have a day_of_week attribute, returning an Integer'
|
75
|
+
it 'should have an hour attribute, returning an Integer'
|
76
|
+
it 'should have a minute attribute, returning an Integer'
|
77
|
+
it 'should have a second attribute, returning an Integer'
|
78
|
+
it 'should have a millisecond attribute, returning an Integer'
|
79
|
+
it 'should have a microsecond attribute, returning an Integer'
|
80
|
+
it 'should have a nanosecond attribute, returning an Integer'
|
81
|
+
it 'should have a picosecond attribute, returning an Integer'
|
82
|
+
it 'should have a month_name attribute, returning a String'
|
83
|
+
it 'should have a day_name attribute, returning a String'
|
84
|
+
it 'should have an offset attribute, returning a Duration::Gregorian'
|
85
|
+
end
|
86
|
+
|
87
|
+
describe 'format' do
|
88
|
+
it 'should format localized month-names with %%A'
|
89
|
+
it 'should format localized day-names %%B'
|
90
|
+
it 'should format 4-digit years with %%4y and %%y'
|
91
|
+
it 'should format 2-digit years with %%2y'
|
92
|
+
it 'should format months with %%m'
|
93
|
+
it 'should format day of month with %%d'
|
94
|
+
it 'should format week (iso 8601) with %%w'
|
95
|
+
it 'should format day of week with %%'
|
96
|
+
it 'should format 2-digit months zero padded with %%0m'
|
97
|
+
it 'should format 2-digit months space padded with %%0m'
|
98
|
+
end
|
99
|
+
|
100
|
+
describe 'succeeding method' do
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
=begin
|
105
|
+
describe 'consistency in advanding, creation and values'
|
106
|
+
describe 'should be consistent over a full cycle (402 years)' do
|
107
|
+
it '???' do
|
108
|
+
days = [nil, 31,28,31,30,31,30,31,31,30,31,30,31]
|
109
|
+
init = Chronos::Datetime::Gregorian.civil(1999,1,1)
|
110
|
+
day_of_month = nil
|
111
|
+
day_of_week = init.day_of_week
|
112
|
+
day_number = init.day_number
|
113
|
+
1999.upto(2401) { |year| # test a bit more than 1 full cycle, luckily weekdays have a full cycle over 400 years too
|
114
|
+
puts "year: #{year}" if year%100 == 0
|
115
|
+
day_of_year = 1
|
116
|
+
1.upto(12) { |month|
|
117
|
+
ldim = days[month] + ((month == 2 && Chronos::Datetime::Gregorian.leap_year?(year)) ? 1 : 0)
|
118
|
+
1.upto(ldim) { |day_of_month|
|
119
|
+
date1 = Chronos::Datetime::Gregorian.civil(year, month, day_of_month)
|
120
|
+
date2 = Chronos::Datetime::Gregorian.ordinal(year, day_of_year)
|
121
|
+
date3 = Chronos::Datetime::Gregorian.commercial(date1.commercial_year, date1.week, day_of_week)
|
122
|
+
date4 = Date.civil(year, month, day_of_month)
|
123
|
+
|
124
|
+
# test if consistent with Time
|
125
|
+
date4.year.should.be.equal(date1.year)
|
126
|
+
date4.cwyear.should.be.equal(date1.commercial_year)
|
127
|
+
date4.month.should.be.equal(date1.month)
|
128
|
+
date4.strftime("%V").to_i.should.be.equal(date1.week)
|
129
|
+
date4.yday.should.be.equal(date1.day_of_year)
|
130
|
+
date4.day.should.be.equal(date1.day_of_month)
|
131
|
+
((date4.wday+6)%7).should.be.equal(date1.day_of_week)
|
132
|
+
|
133
|
+
# test if different constructors yield the same
|
134
|
+
date1.should.be.equal(date2)
|
135
|
+
date1.should.be.equal(date3)
|
136
|
+
|
137
|
+
# test incremental integrity
|
138
|
+
day_number.should.be.equal(date1.day_number)
|
139
|
+
year.should.be.equal(date1.year)
|
140
|
+
day_of_year.should.be.equal(date1.day_of_year)
|
141
|
+
month.should.be.equal(date1.month)
|
142
|
+
day_of_month.should.be.equal(date1.day_of_month)
|
143
|
+
day_of_week.should.be.equal(date1.day_of_week)
|
144
|
+
|
145
|
+
day_number += 1
|
146
|
+
day_of_year += 1
|
147
|
+
day_of_week = (day_of_week+1)%7
|
148
|
+
}
|
149
|
+
proc {
|
150
|
+
Chronos::Datetime::Gregorian.civil(year, month, day_of_month+1)
|
151
|
+
}.should.raise
|
152
|
+
}
|
153
|
+
}
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end # describe consistency
|
157
|
+
=end
|
158
|
+
end # describe Chronos::Datetime::Gregorian
|
159
|
+
|
160
|
+
__END__
|
161
|
+
class TestDatetime < Test::Unit::TestCase
|
162
|
+
Dt = Chronos::Datetime
|
163
|
+
|
164
|
+
def test_creation
|
165
|
+
[
|
166
|
+
[2006,12,31],
|
167
|
+
].each { |(y,m,d)|
|
168
|
+
dt = Dt.civil(y,m,d)
|
169
|
+
assert_equal(y, dt.year)
|
170
|
+
assert_equal(m, dt.month)
|
171
|
+
assert_equal(d, dt.day_of_month)
|
172
|
+
}
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_leap
|
176
|
+
assert Dt.leap?(2000)
|
177
|
+
assert !(Dt.leap?(2001))
|
178
|
+
assert (Dt.leap?(1996))
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_today
|
182
|
+
now = Dt.now
|
183
|
+
now2 = Time.now
|
184
|
+
today = Dt.today
|
185
|
+
assert now
|
186
|
+
assert today
|
187
|
+
assert_equal now.year, now2.year
|
188
|
+
assert_equal now.month, now2.month
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_ordinal
|
192
|
+
now = Time.now
|
193
|
+
data = Dt.today
|
194
|
+
assert data
|
195
|
+
end
|
196
|
+
|
197
|
+
# brute force test of integrity of datetime object
|
198
|
+
# simply tests for continuity of the constructor (day_number)
|
199
|
+
def test_integrity
|
200
|
+
days = [nil, 31,28,31,30,31,30,31,31,30,31,30,31]
|
201
|
+
init = Dt.civil(1999,1,1)
|
202
|
+
day_of_month = nil
|
203
|
+
day_of_week = init.day_of_week
|
204
|
+
day_number = init.day_number
|
205
|
+
1999.upto(2401) { |year| # test a bit more than 1 full cycle, luckily weekdays have a full cycle over 400 years too
|
206
|
+
puts "year: #{year}" if year%100 == 0
|
207
|
+
day_of_year = 1
|
208
|
+
1.upto(12) { |month|
|
209
|
+
ldim = days[month] + ((month == 2 && Dt.leap?(year)) ? 1 : 0)
|
210
|
+
1.upto(ldim) { |day_of_month|
|
211
|
+
date1 = Dt.civil(year, month, day_of_month)
|
212
|
+
date2 = Dt.ordinal(year, day_of_year)
|
213
|
+
date3 = Dt.commercial(date1.commercial_year, date1.week, day_of_week)
|
214
|
+
date4 = Date.civil(year, month, day_of_month)
|
215
|
+
|
216
|
+
# test if consistent with Time
|
217
|
+
assert_equal(date4.year, date1.year, "Time and Chronos::Datetime differ in year #{[year, month, day_of_month].inspect}")
|
218
|
+
assert_equal(date4.cwyear, date1.commercial_year, "Time and Chronos::Datetime differ in cwyear #{[year, month, day_of_month].inspect}")
|
219
|
+
assert_equal(date4.month, date1.month, "Time and Chronos::Datetime differ in month #{[year, month, day_of_month].inspect}")
|
220
|
+
assert_equal(date4.strftime("%V").to_i, date1.week, "Time and Chronos::Datetime differ in week #{[year, month, day_of_month].inspect}")
|
221
|
+
assert_equal(date4.yday, date1.day_of_year, "Time and Chronos::Datetime differ in day of year #{[year, month, day_of_month].inspect}")
|
222
|
+
assert_equal(date4.day, date1.day_of_month, "Time and Chronos::Datetime differ in day of year #{[year, month, day_of_month].inspect}")
|
223
|
+
assert_equal((date4.wday+6)%7, date1.day_of_week, "Time and Chronos::Datetime differ in day of year #{[year, month, day_of_month].inspect}")
|
224
|
+
|
225
|
+
# test if different constructors yield the same
|
226
|
+
assert_equal(date1, date2, "date constructed via civil and ordinal not the same #{[year, month, day_of_month].inspect}")
|
227
|
+
assert_equal(date1, date3, "date constructed via civil and commercial not the same #{[year, month, day_of_month].inspect}")
|
228
|
+
|
229
|
+
# test incremental integrity
|
230
|
+
assert_equal(day_number, date1.day_number, "day number #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
231
|
+
assert_equal(year, date1.year, "year #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
232
|
+
assert_equal(day_of_year, date1.day_of_year, "day of year #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
233
|
+
assert_equal(month, date1.month, "month #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
234
|
+
assert_equal(day_of_month, date1.day_of_month, "day of month #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
235
|
+
assert_equal(day_of_week, date1.day_of_week, "day of week #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
236
|
+
|
237
|
+
day_number += 1
|
238
|
+
day_of_year += 1
|
239
|
+
day_of_week = (day_of_week+1)%7
|
240
|
+
}
|
241
|
+
assert_raise(ArgumentError) { Dt.civil(year, month, day_of_month+1) }
|
242
|
+
}
|
243
|
+
}
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_day_of_week
|
247
|
+
assert_equal(6, Dt.civil(1982,5,23).day_of_week)
|
248
|
+
assert_equal(5, Dt.civil(2000,1,1).day_of_week)
|
249
|
+
assert_equal(6, Dt.civil(2006,12,24).day_of_week)
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_commercial
|
253
|
+
assert_equal(2,Dt.commercial(2006,1,2).day_of_week)
|
254
|
+
# if this fails our tests are broken
|
255
|
+
#assert_equal 1,Dt.commercial(2006,1,2).day_of_month
|
256
|
+
|
257
|
+
month = Dt.commercial(2006,1,2).month_and_day_of_month[0]
|
258
|
+
assert_equal 1, month
|
259
|
+
|
260
|
+
# this return value varies from ISO8601 standards,since 1st Jan 2006 was sunday, it would taken as 52/53 week of previous year
|
261
|
+
# hence 2nd day of 1 st week, would be Tuesday not wednesday
|
262
|
+
# correct me, if i am wrong
|
263
|
+
#assert_equal 'Tuesday', Dt.commercial(2006,1,2).day_name
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_time
|
267
|
+
dt_time = Dt.time(10)
|
268
|
+
assert dt_time
|
269
|
+
assert_equal dt_time.second, 0
|
270
|
+
assert_equal dt_time.usec, 0
|
271
|
+
assert_equal dt_time.minute, 0
|
272
|
+
assert_equal dt_time.hour, 10
|
273
|
+
assert_raise(Chronos::NoDatePart) {dt_time.month}
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_epoch
|
277
|
+
dt_time = Dt.epoch(Time.at(0))
|
278
|
+
assert dt_time
|
279
|
+
# this depends upon, when you executed your test case
|
280
|
+
assert_equal dt_time.year, 1970
|
281
|
+
assert_equal dt_time.month, 1
|
282
|
+
assert_equal dt_time.second, 0
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_at_time
|
286
|
+
dt_time = Dt.time(10)
|
287
|
+
dt_time = dt_time.at(20)
|
288
|
+
assert_equal dt_time.hour, 20
|
289
|
+
assert_equal dt_time.timezone, nil
|
290
|
+
assert_raise(Chronos::NoDatePart) { dt_time.month}
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_date_and_time
|
294
|
+
dt_time = Dt.now
|
295
|
+
assert dt_time.month
|
296
|
+
assert dt_time.second
|
297
|
+
dt_date = dt_time.strip_time
|
298
|
+
dt_only_time = dt_time.strip_date
|
299
|
+
assert_raise(Chronos::NoTimePart) { dt_date.hour}
|
300
|
+
assert_raise(Chronos::NoDatePart) { dt_only_time.month}
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_succeeding
|
304
|
+
dt_civil = Dt.civil(2000,3,31)
|
305
|
+
assert_raise(ArgumentError) { dt_civil.succeeding(:month) }
|
306
|
+
dt_civil = Dt.civil(2004,2,29)
|
307
|
+
assert_raise(ArgumentError) { dt_civil.succeeding(:year) }
|
308
|
+
# FIXME put some more special cases here
|
309
|
+
dt_time = Dt.now
|
310
|
+
dt_time_next = dt_time.succeeding(:hour)
|
311
|
+
assert_equal (dt_time.hour+1), dt_time_next.hour
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
@@ -0,0 +1,219 @@
|
|
1
|
+
unless $LOADED_FEATURES.include?('bacon_helper.rb')
|
2
|
+
load(File.expand_path("#{__FILE__}/../../../bacon_helper.rb"))
|
3
|
+
$LOADED_FEATURES << 'bacon_helper.rb'
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'time'
|
7
|
+
require 'date'
|
8
|
+
require 'chronos/datetime'
|
9
|
+
|
10
|
+
Chronos.timezone = 'UTC'
|
11
|
+
Chronos.language = 'en_US'
|
12
|
+
|
13
|
+
describe 'Chronos::Datetime' do
|
14
|
+
describe 'Chronos::Datetime::today' do
|
15
|
+
it 'should create an instance representing current date' do
|
16
|
+
proc { Chronos::Datetime.today }.should.not.raise
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'Chronos::Datetime::now' do
|
21
|
+
it 'should create an instance representing current date and time' do
|
22
|
+
proc { Chronos::Datetime.now }.should.not.raise
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'Chronos::Datetime::import' do
|
27
|
+
it 'should import Datetime instances' do
|
28
|
+
proc { Chronos::Datetime.import(Chronos::Datetime.now) }.should.not.raise
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should import Time instances' do
|
32
|
+
proc { Chronos::Datetime.import(Time.now) }.should.not.raise
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should import Date instances' do
|
36
|
+
proc { Chronos::Datetime.import(Date.today) }.should.not.raise
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should import DateTime instances' do
|
40
|
+
proc { Chronos::Datetime.import(DateTime.now) }.should.not.raise
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'Chronos::Datetime#+' do
|
45
|
+
it 'should accept a duration' do
|
46
|
+
proc { Chronos::Datetime.now+Chronos::Duration.new(3,0) }.should.not.raise
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should advance by 3 days if a duration of 3 days is added' do
|
50
|
+
dt = Chronos::Datetime.new(733000,nil,nil,nil)
|
51
|
+
dr = Chronos::Duration.new(3,0)
|
52
|
+
rs = dt+dr
|
53
|
+
rs.day_number.should == 733003
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should retreat by 3 days if a duration of 3 days is subtracted' do
|
57
|
+
dt = Chronos::Datetime.new(733000,nil,nil,nil)
|
58
|
+
dr = Chronos::Duration.new(3,0)
|
59
|
+
rs = dt-dr
|
60
|
+
rs.day_number.should == 732997
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
__END__
|
66
|
+
class TestDatetime < Test::Unit::TestCase
|
67
|
+
Dt = Chronos::Datetime
|
68
|
+
|
69
|
+
def test_creation
|
70
|
+
[
|
71
|
+
[2006,12,31],
|
72
|
+
].each { |(y,m,d)|
|
73
|
+
dt = Dt.civil(y,m,d)
|
74
|
+
assert_equal(y, dt.year)
|
75
|
+
assert_equal(m, dt.month)
|
76
|
+
assert_equal(d, dt.day_of_month)
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_leap
|
81
|
+
assert Dt.leap?(2000)
|
82
|
+
assert !(Dt.leap?(2001))
|
83
|
+
assert (Dt.leap?(1996))
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_today
|
87
|
+
now = Dt.now
|
88
|
+
now2 = Time.now
|
89
|
+
today = Dt.today
|
90
|
+
assert now
|
91
|
+
assert today
|
92
|
+
assert_equal now.year, now2.year
|
93
|
+
assert_equal now.month, now2.month
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_ordinal
|
97
|
+
now = Time.now
|
98
|
+
data = Dt.today
|
99
|
+
assert data
|
100
|
+
end
|
101
|
+
|
102
|
+
# brute force test of integrity of datetime object
|
103
|
+
# simply tests for continuity of the constructor (day_number)
|
104
|
+
def test_integrity
|
105
|
+
days = [nil, 31,28,31,30,31,30,31,31,30,31,30,31]
|
106
|
+
init = Dt.civil(1999,1,1)
|
107
|
+
day_of_month = nil
|
108
|
+
day_of_week = init.day_of_week
|
109
|
+
day_number = init.day_number
|
110
|
+
1999.upto(2401) { |year| # test a bit more than 1 full cycle, luckily weekdays have a full cycle over 400 years too
|
111
|
+
puts "year: #{year}" if year%100 == 0
|
112
|
+
day_of_year = 1
|
113
|
+
1.upto(12) { |month|
|
114
|
+
ldim = days[month] + ((month == 2 && Dt.leap?(year)) ? 1 : 0)
|
115
|
+
1.upto(ldim) { |day_of_month|
|
116
|
+
date1 = Dt.civil(year, month, day_of_month)
|
117
|
+
date2 = Dt.ordinal(year, day_of_year)
|
118
|
+
date3 = Dt.commercial(date1.commercial_year, date1.week, day_of_week)
|
119
|
+
date4 = Date.civil(year, month, day_of_month)
|
120
|
+
|
121
|
+
# test if consistent with Time
|
122
|
+
assert_equal(date4.year, date1.year, "Time and Chronos::Datetime differ in year #{[year, month, day_of_month].inspect}")
|
123
|
+
assert_equal(date4.cwyear, date1.commercial_year, "Time and Chronos::Datetime differ in cwyear #{[year, month, day_of_month].inspect}")
|
124
|
+
assert_equal(date4.month, date1.month, "Time and Chronos::Datetime differ in month #{[year, month, day_of_month].inspect}")
|
125
|
+
assert_equal(date4.strftime("%V").to_i, date1.week, "Time and Chronos::Datetime differ in week #{[year, month, day_of_month].inspect}")
|
126
|
+
assert_equal(date4.yday, date1.day_of_year, "Time and Chronos::Datetime differ in day of year #{[year, month, day_of_month].inspect}")
|
127
|
+
assert_equal(date4.day, date1.day_of_month, "Time and Chronos::Datetime differ in day of year #{[year, month, day_of_month].inspect}")
|
128
|
+
assert_equal((date4.wday+6)%7, date1.day_of_week, "Time and Chronos::Datetime differ in day of year #{[year, month, day_of_month].inspect}")
|
129
|
+
|
130
|
+
# test if different constructors yield the same
|
131
|
+
assert_equal(date1, date2, "date constructed via civil and ordinal not the same #{[year, month, day_of_month].inspect}")
|
132
|
+
assert_equal(date1, date3, "date constructed via civil and commercial not the same #{[year, month, day_of_month].inspect}")
|
133
|
+
|
134
|
+
# test incremental integrity
|
135
|
+
assert_equal(day_number, date1.day_number, "day number #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
136
|
+
assert_equal(year, date1.year, "year #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
137
|
+
assert_equal(day_of_year, date1.day_of_year, "day of year #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
138
|
+
assert_equal(month, date1.month, "month #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
139
|
+
assert_equal(day_of_month, date1.day_of_month, "day of month #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
140
|
+
assert_equal(day_of_week, date1.day_of_week, "day of week #{[year, month, day_of_month].inspect} #{date1.inspect}")
|
141
|
+
|
142
|
+
day_number += 1
|
143
|
+
day_of_year += 1
|
144
|
+
day_of_week = (day_of_week+1)%7
|
145
|
+
}
|
146
|
+
assert_raise(ArgumentError) { Dt.civil(year, month, day_of_month+1) }
|
147
|
+
}
|
148
|
+
}
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_day_of_week
|
152
|
+
assert_equal(6, Dt.civil(1982,5,23).day_of_week)
|
153
|
+
assert_equal(5, Dt.civil(2000,1,1).day_of_week)
|
154
|
+
assert_equal(6, Dt.civil(2006,12,24).day_of_week)
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_commercial
|
158
|
+
assert_equal(2,Dt.commercial(2006,1,2).day_of_week)
|
159
|
+
# if this fails our tests are broken
|
160
|
+
#assert_equal 1,Dt.commercial(2006,1,2).day_of_month
|
161
|
+
|
162
|
+
month = Dt.commercial(2006,1,2).month_and_day_of_month[0]
|
163
|
+
assert_equal 1, month
|
164
|
+
|
165
|
+
# this return value varies from ISO8601 standards,since 1st Jan 2006 was sunday, it would taken as 52/53 week of previous year
|
166
|
+
# hence 2nd day of 1 st week, would be Tuesday not wednesday
|
167
|
+
# correct me, if i am wrong
|
168
|
+
#assert_equal 'Tuesday', Dt.commercial(2006,1,2).day_name
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_time
|
172
|
+
dt_time = Dt.time(10)
|
173
|
+
assert dt_time
|
174
|
+
assert_equal dt_time.second, 0
|
175
|
+
assert_equal dt_time.usec, 0
|
176
|
+
assert_equal dt_time.minute, 0
|
177
|
+
assert_equal dt_time.hour, 10
|
178
|
+
assert_raise(Chronos::NoDatePart) {dt_time.month}
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_epoch
|
182
|
+
dt_time = Dt.epoch(Time.at(0))
|
183
|
+
assert dt_time
|
184
|
+
# this depends upon, when you executed your test case
|
185
|
+
assert_equal dt_time.year, 1970
|
186
|
+
assert_equal dt_time.month, 1
|
187
|
+
assert_equal dt_time.second, 0
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_at_time
|
191
|
+
dt_time = Dt.time(10)
|
192
|
+
dt_time = dt_time.at(20)
|
193
|
+
assert_equal dt_time.hour, 20
|
194
|
+
assert_equal dt_time.timezone, nil
|
195
|
+
assert_raise(Chronos::NoDatePart) { dt_time.month}
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_date_and_time
|
199
|
+
dt_time = Dt.now
|
200
|
+
assert dt_time.month
|
201
|
+
assert dt_time.second
|
202
|
+
dt_date = dt_time.strip_time
|
203
|
+
dt_only_time = dt_time.strip_date
|
204
|
+
assert_raise(Chronos::NoTimePart) { dt_date.hour}
|
205
|
+
assert_raise(Chronos::NoDatePart) { dt_only_time.month}
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_succeeding
|
209
|
+
dt_civil = Dt.civil(2000,3,31)
|
210
|
+
assert_raise(ArgumentError) { dt_civil.succeeding(:month) }
|
211
|
+
dt_civil = Dt.civil(2004,2,29)
|
212
|
+
assert_raise(ArgumentError) { dt_civil.succeeding(:year) }
|
213
|
+
# FIXME put some more special cases here
|
214
|
+
dt_time = Dt.now
|
215
|
+
dt_time_next = dt_time.succeeding(:hour)
|
216
|
+
assert_equal (dt_time.hour+1), dt_time_next.hour
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|