third_base 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/LICENSE +19 -0
  2. data/README +261 -0
  3. data/benchmark/date.rb +18 -0
  4. data/benchmark/datetime.rb +18 -0
  5. data/bin/third_base +4 -0
  6. data/lib/third_base/compat/date/format.rb +3 -0
  7. data/lib/third_base/compat/date.rb +3 -0
  8. data/lib/third_base/compat.rb +405 -0
  9. data/lib/third_base/date.rb +674 -0
  10. data/lib/third_base/datetime.rb +385 -0
  11. data/lib/third_base.rb +2 -0
  12. data/spec/compat/compat_class_methods_spec.rb +208 -0
  13. data/spec/compat/compat_instance_methods_spec.rb +54 -0
  14. data/spec/compat/date_spec.rb +56 -0
  15. data/spec/compat/datetime_spec.rb +77 -0
  16. data/spec/compat_spec_helper.rb +2 -0
  17. data/spec/date/accessor_spec.rb +134 -0
  18. data/spec/date/add_month_spec.rb +28 -0
  19. data/spec/date/add_spec.rb +24 -0
  20. data/spec/date/boat_spec.rb +31 -0
  21. data/spec/date/civil_spec.rb +47 -0
  22. data/spec/date/commercial_spec.rb +34 -0
  23. data/spec/date/constants_spec.rb +18 -0
  24. data/spec/date/downto_spec.rb +17 -0
  25. data/spec/date/eql_spec.rb +9 -0
  26. data/spec/date/hash_spec.rb +13 -0
  27. data/spec/date/julian_spec.rb +13 -0
  28. data/spec/date/leap_spec.rb +19 -0
  29. data/spec/date/minus_month_spec.rb +26 -0
  30. data/spec/date/minus_spec.rb +47 -0
  31. data/spec/date/ordinal_spec.rb +13 -0
  32. data/spec/date/parse_spec.rb +227 -0
  33. data/spec/date/step_spec.rb +55 -0
  34. data/spec/date/strftime_spec.rb +132 -0
  35. data/spec/date/strptime_spec.rb +118 -0
  36. data/spec/date/succ_spec.rb +16 -0
  37. data/spec/date/today_spec.rb +11 -0
  38. data/spec/date/upto_spec.rb +17 -0
  39. data/spec/date_spec_helper.rb +3 -0
  40. data/spec/datetime/accessor_spec.rb +53 -0
  41. data/spec/datetime/add_spec.rb +36 -0
  42. data/spec/datetime/boat_spec.rb +43 -0
  43. data/spec/datetime/constructor_spec.rb +58 -0
  44. data/spec/datetime/eql_spec.rb +11 -0
  45. data/spec/datetime/minus_spec.rb +65 -0
  46. data/spec/datetime/now_spec.rb +14 -0
  47. data/spec/datetime/parse_spec.rb +338 -0
  48. data/spec/datetime/strftime_spec.rb +102 -0
  49. data/spec/datetime/strptime_spec.rb +84 -0
  50. data/spec/datetime_spec_helper.rb +3 -0
  51. data/spec/spec_helper.rb +54 -0
  52. metadata +107 -0
@@ -0,0 +1,56 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'compat_spec_helper'))
2
+
3
+ describe Date do
4
+
5
+ it "._strptime should be the same as strptime with a no default date" do
6
+ Date._strptime('2008-10-11').should == Date.strptime('2008-10-11')
7
+ Date._strptime('2008-10-11', '%Y-%m-%d').should == Date.strptime('2008-10-11', '%Y-%m-%d')
8
+ end
9
+
10
+ it ".civil should have defaults and an optional sg value" do
11
+ Date.civil(2008, 1, 1, 1).should == Date.civil(2008, 1, 1)
12
+ Date.civil.should == Date.civil(-4712, 1, 1)
13
+ end
14
+
15
+ it ".commercial should have defaults and an optional sg value" do
16
+ Date.commercial(2008, 1, 1, 1).should == Date.commercial(2008, 1, 1)
17
+ Date.commercial.should == Date.commercial(1582, 41, 5)
18
+ end
19
+
20
+ it ".jd should have defaults and an optional sg value" do
21
+ Date.jd(2008, 1).should == Date.jd(2008)
22
+ Date.jd.should == Date.jd(0)
23
+ end
24
+
25
+ it ".ordinal should have defaults and an optional sg value" do
26
+ Date.ordinal(2008, 1, 1).should == Date.ordinal(2008, 1)
27
+ Date.ordinal.should == Date.ordinal(-4712, 1)
28
+ end
29
+
30
+ it ".parse should have defaults and an optional sg value" do
31
+ Date.parse('2008-10-11').should == Date.civil(2008, 10, 11)
32
+ Date.parse('2008-10-11', true).should == Date.civil(2008, 10, 11)
33
+ Date.parse('2008-10-11', true, 1).should == Date.civil(2008, 10, 11)
34
+ Date.parse.should == Date.civil(-4712, 1, 1)
35
+ end
36
+
37
+ it ".strptime should have defaults and an optional sg value" do
38
+ Date.strptime('2008-10-11').should == Date.civil(2008, 10, 11)
39
+ Date.strptime('2008-10-11', '%Y-%m-%d').should == Date.civil(2008, 10, 11)
40
+ Date.strptime('2008-10-11', '%Y-%m-%d', 1).should == Date.civil(2008, 10, 11)
41
+ Date.strptime.should == Date.civil(-4712, 1, 1)
42
+ end
43
+
44
+ it ".today should have an optional sg value" do
45
+ Date.today(1).should == Date.today
46
+ end
47
+
48
+ it "#asctime and #ctime should be a string with the date formatted" do
49
+ Date.new(2008, 1, 1).asctime.should == 'Tue Jan 1 00:00:00 2008'
50
+ Date.new(2008, 1, 1).ctime.should == 'Tue Jan 1 00:00:00 2008'
51
+ end
52
+
53
+ it "#day_fraction should be 0.0" do
54
+ Date.new(2008, 1, 1).day_fraction.should == 0.0
55
+ end
56
+ end
@@ -0,0 +1,77 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'compat_spec_helper'))
2
+
3
+ describe DateTime do
4
+
5
+ it "._strptime should be the same as strptime with no default date" do
6
+ DateTime._strptime('2008-10-11T01:00:02+0000').should == DateTime.strptime('2008-10-11T01:00:02+0000')
7
+ DateTime._strptime('2008-10-11', '%Y-%m-%d').should == DateTime.strptime('2008-10-11', '%Y-%m-%d')
8
+ end
9
+
10
+ it ".civil should have defaults and an optional sg value" do
11
+ DateTime.civil(2008, 1, 1, 1, 1, 1, 0.5, 1).should == DateTime.civil(2008, 1, 1, 1, 1, 1, 0.5)
12
+ DateTime.civil.should == DateTime.civil(-4712, 1, 1, 0, 0, 0, 0)
13
+ end
14
+
15
+ it ".commercial should have defaults and an optional sg value" do
16
+ DateTime.commercial(2008, 1, 1, 1, 1, 1, 0.5, 1).should == DateTime.commercial(2008, 1, 1, 1, 1, 1, 0.5)
17
+ DateTime.commercial.should == DateTime.commercial(1582, 41, 5, 0, 0, 0, 0)
18
+ end
19
+
20
+ it ".jd should have defaults and an optional sg value" do
21
+ DateTime.jd(2008, 1, 1, 1, 0.5, 1).should == DateTime.jd(2008, 1, 1, 1, 0.5)
22
+ DateTime.jd.should == DateTime.jd(0, 0, 0, 0, 0)
23
+ end
24
+
25
+ it ".ordinal should have defaults and an optional sg value" do
26
+ DateTime.ordinal(2008, 1, 1, 1, 1, 0.5, 1).should == DateTime.ordinal(2008, 1, 1, 1, 1, 0.5)
27
+ DateTime.ordinal.should == DateTime.ordinal(-4712, 1, 0, 0, 0, 0)
28
+ end
29
+
30
+ it ".parse should have defaults and an optional sg value" do
31
+ DateTime.parse('2008-10-11').should == DateTime.civil(2008, 10, 11)
32
+ DateTime.parse('2008-10-11', true).should == DateTime.civil(2008, 10, 11)
33
+ DateTime.parse('2008-10-11', true, 1).should == DateTime.civil(2008, 10, 11)
34
+ DateTime.parse.should == DateTime.civil(-4712, 1, 1)
35
+ end
36
+
37
+ it ".strptime should have defaults and an optional sg value" do
38
+ DateTime.strptime('2008-10-11T00:00:00+00:00').should == DateTime.civil(2008, 10, 11)
39
+ DateTime.strptime('2008-10-11', '%Y-%m-%d').should == DateTime.civil(2008, 10, 11)
40
+ DateTime.strptime('2008-10-11', '%Y-%m-%d', 1).should == DateTime.civil(2008, 10, 11)
41
+ DateTime.strptime.should == DateTime.civil(-4712, 1, 1)
42
+ end
43
+
44
+ it ".now should have an optional sg value" do
45
+ DateTime.now(1).to_s.should == DateTime.now.to_s
46
+ end
47
+
48
+ it "#asctime and #ctime should be a string with the date formatted" do
49
+ DateTime.new(2008, 1, 1).asctime.should == 'Tue Jan 1 00:00:00 2008'
50
+ DateTime.new(2008, 1, 1).ctime.should == 'Tue Jan 1 00:00:00 2008'
51
+ end
52
+
53
+ it "#day_fraction should be the fraction of the day" do
54
+ DateTime.new(2008, 1, 1).day_fraction.should == 0.0
55
+ DateTime.new(2008, 1, 1, 12).day_fraction.should == 0.5
56
+ end
57
+
58
+ it "#new_offset and #newof should be a separate datetime with a modified offset" do
59
+ DateTime.new(2008, 1, 1).new_offset(0.5).should == DateTime.new(2008, 1, 1, 0, 0, 0, 0.5)
60
+ DateTime.new(2008, 1, 1).newof(0.5).should == DateTime.new(2008, 1, 1, 0, 0, 0, 0.5)
61
+ end
62
+
63
+ it "#offset and #of should be the offset as a float fraction of the day" do
64
+ DateTime.parse('2008-01-01 00:00:00+12:00').offset.should == 0.5
65
+ DateTime.parse('2008-01-01 00:00:00+12:00').of.should == 0.5
66
+ end
67
+
68
+ it "#offset_sec offset from UTC in seconds" do
69
+ DateTime.parse('2008-01-01 00:00:00+02:00').offset_sec.should == 7200
70
+ end
71
+
72
+ it "#sec_fraction should be the fraction of a second as a fraction of the day" do
73
+ DateTime.new(2008, 1, 1).sec_fraction.should == 0.0
74
+ DateTime.parse('12:13:15.678900').sec_fraction.should be_close(7.85763888888889e-06, 0.0000000000001)
75
+ end
76
+
77
+ end
@@ -0,0 +1,2 @@
1
+ require 'third_base/compat'
2
+ require(File.join(File.dirname(__FILE__), 'spec_helper'))
@@ -0,0 +1,134 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#cwday" do
4
+ it "should be able to determine the commercial week day for a date" do
5
+ Date.civil(2007, 1, 17).cwday.should == 3
6
+ Date.civil(2008, 10, 26).cwday.should == 7
7
+ Date.commercial(2008, 1, 1).cwday.should == 1
8
+ Date.commercial(2008, 10, 5).cwday.should == 5
9
+ Date.jd(2454782).cwday.should == 2
10
+ Date.jd(2454786).cwday.should == 6
11
+ Date.ordinal(2008, 1).cwday.should == 2
12
+ Date.ordinal(2008, 317).cwday.should == 3
13
+ end
14
+ end
15
+
16
+ describe "Date#cweek" do
17
+ it "should be able to determine the commercial week for a date" do
18
+ Date.civil(2007, 1, 17).cweek.should == 3
19
+ Date.civil(2008, 10, 28).cweek.should == 44
20
+ Date.civil(2007, 12, 31).cweek.should == 1
21
+ Date.civil(2010, 1, 1).cweek.should == 53
22
+ Date.commercial(2008, 1, 1).cweek.should == 1
23
+ Date.commercial(2008, 10, 5).cweek.should == 10
24
+ Date.jd(2454782).cweek.should == 46
25
+ Date.jd(2454789).cweek.should == 47
26
+ Date.ordinal(2008, 1).cweek.should == 1
27
+ Date.ordinal(2008, 359).cweek.should == 52
28
+ Date.ordinal(2008, 366).cweek.should == 1
29
+ end
30
+ end
31
+
32
+ describe "Date#cwyear" do
33
+ it "should be able to determine the commercial year for a date" do
34
+ Date.civil(2007, 1, 17).cwyear.should == 2007
35
+ Date.civil(2008, 10, 28).cwyear.should == 2008
36
+ Date.civil(2007, 12, 31).cwyear.should == 2008
37
+ Date.civil(2010, 1, 1).cwyear.should == 2009
38
+ Date.commercial(2008, 1, 1).cwyear.should == 2008
39
+ Date.commercial(2008, 52, 1).cwyear.should == 2008
40
+ Date.jd(2454782).cwyear.should == 2008
41
+ Date.jd(2454832).cwyear.should == 2009
42
+ Date.ordinal(2008, 1).cwyear.should == 2008
43
+ Date.ordinal(2008, 359).cwyear.should == 2008
44
+ Date.ordinal(2008, 366).cwyear.should == 2009
45
+ end
46
+ end
47
+
48
+ describe "Date#day and #mday" do
49
+ it "should be able to determine the month for a date" do
50
+ Date.civil(2007, 1, 17).day.should == 17
51
+ Date.civil(2008, 10, 28).day.should == 28
52
+ Date.civil(2007, 1, 17).mday.should == 17
53
+ Date.civil(2008, 10, 28).mday.should == 28
54
+ Date.commercial(2008, 1, 1).day.should == 31
55
+ Date.commercial(2008, 52, 1).day.should == 22
56
+ Date.jd(2454782).day.should == 11
57
+ Date.jd(2454832).day.should == 31
58
+ Date.ordinal(2008, 1).day.should == 1
59
+ Date.ordinal(2008, 359).day.should == 24
60
+ Date.ordinal(2008, 366).day.should == 31
61
+ end
62
+ end
63
+
64
+ describe "Date#jd" do
65
+ it "should be able to determine the month for a date" do
66
+ Date.civil(2007, 1, 17).jd.should == 2454118
67
+ Date.civil(2008, 10, 28).jd.should == 2454768
68
+ Date.commercial(2008, 1, 1).jd.should == 2454466
69
+ Date.commercial(2008, 52, 1).jd.should == 2454823
70
+ Date.jd(2454782).jd.should == 2454782
71
+ Date.jd(2454832).jd.should == 2454832
72
+ Date.ordinal(2008, 1).jd.should == 2454467
73
+ Date.ordinal(2008, 359).jd.should == 2454825
74
+ Date.ordinal(2008, 366).jd.should == 2454832
75
+ end
76
+ end
77
+
78
+ describe "Date#mon and #month" do
79
+ it "should be able to determine the month for a date" do
80
+ Date.civil(2007, 1, 17).mon.should == 1
81
+ Date.civil(2008, 10, 28).mon.should == 10
82
+ Date.civil(2007, 1, 17).month.should == 1
83
+ Date.civil(2008, 10, 28).month.should == 10
84
+ Date.commercial(2008, 1, 1).mon.should == 12
85
+ Date.commercial(2008, 52, 1).mon.should == 12
86
+ Date.jd(2454782).mon.should == 11
87
+ Date.jd(2454832).mon.should == 12
88
+ Date.ordinal(2008, 1).mon.should == 1
89
+ Date.ordinal(2008, 170).mon.should == 6
90
+ Date.ordinal(2008, 366).mon.should == 12
91
+ end
92
+ end
93
+
94
+ describe "Date#wday" do
95
+ it "should be able to determine the week day for a date" do
96
+ Date.civil(2007, 1, 17).wday.should == 3
97
+ Date.civil(2008, 10, 26).wday.should == 0
98
+ Date.commercial(2008, 1, 1).wday.should == 1
99
+ Date.commercial(2008, 52, 1).wday.should == 1
100
+ Date.jd(2454782).wday.should == 2
101
+ Date.jd(2454832).wday.should == 3
102
+ Date.ordinal(2008, 1).wday.should == 2
103
+ Date.ordinal(2008, 170).wday.should == 3
104
+ Date.ordinal(2008, 366).wday.should == 3
105
+ end
106
+ end
107
+
108
+ describe "Date#yday" do
109
+ it "should be able to determine the year for a date" do
110
+ Date.civil(2007, 1, 17).yday.should == 17
111
+ Date.civil(2008, 10, 28).yday.should == 302
112
+ Date.commercial(2008, 1, 1).yday.should == 365
113
+ Date.commercial(2008, 52, 1).yday.should == 357
114
+ Date.jd(2454782).yday.should == 316
115
+ Date.jd(2454832).yday.should == 366
116
+ Date.ordinal(2008, 1).yday.should == 1
117
+ Date.ordinal(2008, 170).yday.should == 170
118
+ Date.ordinal(2008, 366).yday.should == 366
119
+ end
120
+ end
121
+
122
+ describe "Date#year" do
123
+ it "should be able to determine the year for a date" do
124
+ Date.civil(2007, 1, 17).year.should == 2007
125
+ Date.civil(2008, 1, 17).year.should == 2008
126
+ Date.commercial(2008, 1, 1).year.should == 2007
127
+ Date.commercial(2008, 52, 1).year.should == 2008
128
+ Date.jd(2454782).year.should == 2008
129
+ Date.jd(2454833).year.should == 2009
130
+ Date.ordinal(2008, 1).year.should == 2008
131
+ Date.ordinal(2008, 170).year.should == 2008
132
+ Date.ordinal(2008, 366).year.should == 2008
133
+ end
134
+ end
@@ -0,0 +1,28 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#>>" do
4
+
5
+ it "should add a number of months to a Date" do
6
+ (Date.civil(2007,2,27) >> 10).should == Date.civil(2007, 12, 27)
7
+ (Date.civil(2007,2,27) >> 10).should == Date.civil(2007, 12, 27)
8
+ (Date.commercial(2007,2,2) >> 10).should == Date.commercial(2007, 45, 5)
9
+ (Date.jd(2454782) >> 10).should == Date.jd(2455086)
10
+ (Date.ordinal(2008, 10) >> 10).should == Date.ordinal(2008, 315)
11
+ (Date.civil(2007,2,27) >> 22).should == Date.civil(2008, 12, 27)
12
+ (Date.civil(2007,2,27) >> -2).should == Date.civil(2006, 12, 27)
13
+ end
14
+
15
+
16
+ it "should result in the last day of a month if the day doesn't exist" do
17
+ d = Date.civil(2008,3,31) >> 1
18
+ d.should == Date.civil(2008, 4, 30)
19
+ end
20
+
21
+ it "should raise an error on non numeric parameters" do
22
+ lambda { Date.civil(2007,2,27) >> :hello }.should raise_error(TypeError)
23
+ lambda { Date.civil(2007,2,27) >> "hello" }.should raise_error(TypeError)
24
+ lambda { Date.civil(2007,2,27) >> Date.new(2007,2,27) }.should raise_error(TypeError)
25
+ lambda { Date.civil(2007,2,27) >> Object.new }.should raise_error(TypeError)
26
+ end
27
+
28
+ end
@@ -0,0 +1,24 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#+" do
4
+
5
+ it "should add a number of days to a Date" do
6
+ (Date.civil(2007,2,27) + 315).should == Date.civil(2008, 1, 8)
7
+ (Date.commercial(2007,2,2) + 315).should == Date.commercial(2007, 47, 2)
8
+ (Date.jd(2454782) + 315).should == Date.jd(2455097)
9
+ (Date.ordinal(2008, 10) + 315).should == Date.ordinal(2008, 325)
10
+ end
11
+
12
+ it "should add a negative number of days to a Date" do
13
+ d = Date.civil(2007,2,27).+(-10)
14
+ d.should == Date.civil(2007, 2, 17)
15
+ end
16
+
17
+ it "should raise an error on non numeric parameters" do
18
+ lambda { Date.civil(2007,2,27) + :hello }.should raise_error(TypeError)
19
+ lambda { Date.civil(2007,2,27) + "hello" }.should raise_error(TypeError)
20
+ lambda { Date.civil(2007,2,27) + Date.new(2007,2,27) }.should raise_error(TypeError)
21
+ lambda { Date.civil(2007,2,27) + Object.new }.should raise_error(TypeError)
22
+ end
23
+
24
+ end
@@ -0,0 +1,31 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#<=>" do
4
+
5
+ it "should be able to compare two same dates" do
6
+ (Date.civil(2000, 04, 06) <=> Date.civil(2000, 04, 06)).should == 0
7
+ end
8
+
9
+ it "should be able to compute the difference between two dates" do
10
+ (Date.civil(2000, 04, 05) <=> Date.civil(2000, 04, 06)).should == -1
11
+ (Date.civil(2001, 04, 05) <=> Date.civil(2000, 04, 06)).should == 1
12
+ end
13
+
14
+ it "should be able to compare to another numeric" do
15
+ (Date.civil(2000, 04, 05) <=> Date.civil(2000, 04, 06).jd).should == -1
16
+ (Date.civil(2001, 04, 05) <=> Date.civil(2000, 04, 06).jd).should == 1
17
+ end
18
+
19
+ end
20
+
21
+ describe "Date#between?" do
22
+ it "should be true if the date falls in between the two given dates" do
23
+ (Date.civil(2000, 04, 06).between?(Date.civil(2000, 04, 05), Date.civil(2000, 04, 07))).should == true
24
+ (Date.civil(2000, 04, 06).between?(Date.civil(2000, 04, 06), Date.civil(2000, 04, 07))).should == true
25
+ (Date.civil(2000, 04, 06).between?(Date.civil(2000, 04, 05), Date.civil(2000, 04, 06))).should == true
26
+ end
27
+
28
+ it "should be false if the date does not fall in between the two given dates" do
29
+ (Date.civil(2000, 04, 05).between?(Date.civil(2000, 04, 06), Date.civil(2000, 04, 07))).should == false
30
+ end
31
+ end
@@ -0,0 +1,47 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe :date_civil, :shared => true do
4
+ it "creates a date with arguments" do
5
+ d = Date.send(@method, 2000, 3, 5)
6
+ d.year.should == 2000
7
+ d.month.should == 3
8
+ d.day.should == 5
9
+ d.jd.should == 2451609
10
+
11
+ # Should also work with years far in the past and future
12
+
13
+ d = Date.send(@method, -9000, 7, 5)
14
+ d.year.should == -9000
15
+ d.month.should == 7
16
+ d.day.should == 5
17
+ d.jd.should == -1565937
18
+
19
+ d = Date.send(@method, 9000, 10, 14)
20
+ d.year.should == 9000
21
+ d.month.should == 10
22
+ d.day.should == 14
23
+ d.jd.should == 5008529
24
+
25
+ end
26
+
27
+ it "doesn't create dates for invalid arguments" do
28
+ lambda { Date.send(@method, 2000, 13, 31) }.should raise_error(ArgumentError)
29
+ lambda { Date.send(@method, 2000, 12, 32) }.should raise_error(ArgumentError)
30
+ lambda { Date.send(@method, 2000, 2, 30) }.should raise_error(ArgumentError)
31
+ lambda { Date.send(@method, 1900, 2, 29) }.should raise_error(ArgumentError)
32
+ lambda { Date.send(@method, 2000, 2, 29) }.should_not raise_error(ArgumentError)
33
+ end
34
+ end
35
+
36
+
37
+ describe "Date#civil" do
38
+
39
+ it_behaves_like(:date_civil, :civil)
40
+
41
+ end
42
+
43
+ describe "Date#new" do
44
+
45
+ it_behaves_like(:date_civil, :new)
46
+
47
+ end
@@ -0,0 +1,34 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#commercial" do
4
+ it "Creates a Date for the friday in the year and week given" do
5
+ d = Date.commercial(2000, 1)
6
+ d.year.should == 2000
7
+ d.month.should == 1
8
+ d.day.should == 7
9
+ d.cwday.should == 5
10
+ end
11
+
12
+ it "Creates a Date for the correct day given the year, week and day number" do
13
+ d = Date.commercial(2004, 1, 1)
14
+ d.year.should == 2003
15
+ d.month.should == 12
16
+ d.day.should == 29
17
+ d.cwday.should == 1
18
+ d.cweek.should == 1
19
+ d.cwyear.should == 2004
20
+ end
21
+
22
+ it "creates only Date objects for valid weeks" do
23
+ lambda { Date.commercial(2004, 53, 1) }.should_not raise_error(ArgumentError)
24
+ lambda { Date.commercial(2004, 53, 0) }.should raise_error(ArgumentError)
25
+ lambda { Date.commercial(2004, 53, 8) }.should raise_error(ArgumentError)
26
+ lambda { Date.commercial(2004, 54, 1) }.should raise_error(ArgumentError)
27
+ lambda { Date.commercial(2004, 0, 1) }.should raise_error(ArgumentError)
28
+
29
+ lambda { Date.commercial(2003, 52, 1) }.should_not raise_error(ArgumentError)
30
+ lambda { Date.commercial(2003, 53, 1) }.should raise_error(ArgumentError)
31
+ lambda { Date.commercial(2003, 52, 0) }.should raise_error(ArgumentError)
32
+ lambda { Date.commercial(2003, 52, 8) }.should raise_error(ArgumentError)
33
+ end
34
+ end
@@ -0,0 +1,18 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date constants" do
4
+
5
+ it "should define MONTHNAMES" do
6
+ Date::MONTHNAMES.should == [nil] + %w(January February March April May June July
7
+ August September October November December)
8
+ end
9
+
10
+ it "should define DAYNAMES" do
11
+ Date::DAYNAMES.should == %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
12
+ end
13
+
14
+ it "should define ABBR_MONTHNAMES" do
15
+ Date::ABBR_DAYNAMES.should == %w(Sun Mon Tue Wed Thu Fri Sat)
16
+ end
17
+
18
+ end
@@ -0,0 +1,17 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#downto" do
4
+
5
+ it "should be able to step backward in time" do
6
+ ds = Date.civil(2000, 4, 14)
7
+ de = Date.civil(2000, 3, 29)
8
+ count = 0
9
+ ds.step(de, -1) do |d|
10
+ d.should <= ds
11
+ d.should >= de
12
+ count += 1
13
+ end
14
+ count.should == 17
15
+ end
16
+
17
+ end
@@ -0,0 +1,9 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#eql?" do
4
+ it "should be able determine equality between date objects" do
5
+ Date.civil(2007, 10, 11).should eql(Date.civil(2007, 10, 11))
6
+ Date.civil(2007, 10, 11).should eql(Date.civil(2007, 10, 12) - 1)
7
+ Date.civil(2007, 10, 11).should_not eql(Date.civil(2007, 10, 12))
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#hash" do
4
+
5
+ it "should be able determine the hash value for a date" do
6
+ Date.civil(2004, 7, 12).respond_to?(:hash).should == true
7
+ end
8
+
9
+ it "should be the case that the same date results in the same hash" do
10
+ Date.civil(2004, 7, 12).hash.should == Date.civil(2004, 7, 12).hash
11
+ end
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#jd" do
4
+
5
+ it "should be able to construct a Date object based on the Julian day" do
6
+ Date.jd(2454482).should == Date.civil(2008, 1, 16)
7
+ end
8
+
9
+ it "should be able to determine the Julian day for a Date object" do
10
+ Date.civil(2008, 1, 16).jd.should == 2454482
11
+ end
12
+
13
+ end
@@ -0,0 +1,19 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#leap?" do
4
+ it "should be true if the current date is a leap year and no argument is given" do
5
+ Date.civil(2000, 10, 11).leap?.should == true
6
+ Date.civil(2004, 10, 11).leap?.should == true
7
+ Date.civil(2008, 10, 11).leap?.should == true
8
+ Date.civil(1996, 10, 11).leap?.should == true
9
+ Date.civil(1600, 10, 11).leap?.should == true
10
+ end
11
+
12
+ it "should be false if the current date is not a leap year and no argument is given" do
13
+ Date.civil(1700, 10, 11).leap?.should == false
14
+ Date.civil(1800, 10, 11).leap?.should == false
15
+ Date.civil(1900, 10, 11).leap?.should == false
16
+ Date.civil(1999, 10, 11).leap?.should == false
17
+ Date.civil(2001, 10, 11).leap?.should == false
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#<<" do
4
+
5
+ it "should substract a number of months from a date" do
6
+ (Date.civil(2007, 12, 27) << 10).should == Date.civil(2007,2,27)
7
+ (Date.commercial(2007, 45, 5) << 10).should == Date.commercial(2007,2,2)
8
+ (Date.jd(2455086) << 10).should == Date.jd(2454782)
9
+ (Date.ordinal(2008, 315) << 10).should == Date.ordinal(2008, 10)
10
+ (Date.civil(2007, 12, 27) << 12).should == Date.civil(2006,12,27)
11
+ (Date.civil(2007, 12, 27) << -12).should == Date.civil(2008,12,27)
12
+ end
13
+
14
+ it "should result in the last day of a month if the day doesn't exist" do
15
+ d = Date.civil(2008,3,31) << 1
16
+ d.should == Date.civil(2008, 2, 29)
17
+ end
18
+
19
+ it "should raise an error on non numeric parameters" do
20
+ lambda { Date.civil(2007,2,27) << :hello }.should raise_error(NoMethodError)
21
+ lambda { Date.civil(2007,2,27) << "hello" }.should raise_error(NoMethodError)
22
+ lambda { Date.civil(2007,2,27) << Date.new(2007,10,27) }.should raise_error(NoMethodError)
23
+ lambda { Date.civil(2007,2,27) << Object.new }.should raise_error(NoMethodError)
24
+ end
25
+
26
+ end
@@ -0,0 +1,47 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#-" do
4
+
5
+ it "should substract a number of days from a Date" do
6
+ (Date.civil(2008, 1, 8) - 315).should == Date.civil(2007,2,27)
7
+ (Date.commercial(2007, 47, 2) - 315).should == Date.commercial(2007,2,2)
8
+ (Date.jd(2455097) - 315).should == Date.jd(2454782)
9
+ (Date.ordinal(2008, 325) - 315).should == Date.ordinal(2008, 10)
10
+ end
11
+
12
+ it "should substract a negative number of days from a Date" do
13
+ d = Date.civil(2007, 4, 19).-(-13)
14
+ d.should == Date.civil(2007, 5 ,2)
15
+ end
16
+
17
+ it "should be able to compute the different between two dates" do
18
+ (Date.civil(2007,2,27) - Date.civil(2007,2,27)).should == 0
19
+ (Date.civil(2007,2,27) - Date.civil(2007,2,26)).should == 1
20
+ (Date.civil(2006,2,27) - Date.civil(2007,2,27)).should == -365
21
+ (Date.civil(2008,2,27) - Date.civil(2007,2,27)).should == 365
22
+ (Date.civil(2009,2,27) - Date.civil(2008,2,27)).should == 366
23
+
24
+ (Date.civil(2009,2,27) - Date.commercial(2008,2,1)).should == 417
25
+ (Date.civil(2009,2,27) - Date.jd(2454782)).should == 108
26
+ (Date.civil(2009,2,27) - Date.ordinal(2008, 10)).should == 414
27
+
28
+ (Date.commercial(2008,2,1) - Date.civil(2008,2,27)).should == -51
29
+ (Date.commercial(2008,2,1) - Date.jd(2454782)).should == -309
30
+ (Date.commercial(2008,2,1) - Date.ordinal(2008, 10)).should == -3
31
+
32
+ (Date.jd(2454782) - Date.commercial(2008,2,1)).should == 309
33
+ (Date.jd(2454782) - Date.civil(2009,2,27)).should == -108
34
+ (Date.jd(2454782) - Date.ordinal(2008, 10)).should == 306
35
+
36
+ (Date.ordinal(2008, 10) - Date.commercial(2008,2,1)).should == 3
37
+ (Date.ordinal(2008, 10) - Date.jd(2454782)).should == -306
38
+ (Date.ordinal(2008, 10) - Date.civil(2009,2,27)).should == -414
39
+ end
40
+
41
+ it "should raise an error on non numeric parameters" do
42
+ lambda { Date.civil(2007,2,27) - :hello }.should raise_error(TypeError)
43
+ lambda { Date.civil(2007,2,27) - "hello" }.should raise_error(TypeError)
44
+ lambda { Date.civil(2007,2,27) - Object.new }.should raise_error(TypeError)
45
+ end
46
+
47
+ end
@@ -0,0 +1,13 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#ordinal" do
4
+
5
+ it "should be able to construct a Date object from an ordinal date" do
6
+ lambda { Date.ordinal(1900, 366) }.should raise_error(ArgumentError)
7
+ lambda { Date.ordinal(1900, 365) }.should_not raise_error(ArgumentError)
8
+ lambda { Date.ordinal(2000, 366) }.should_not raise_error(ArgumentError)
9
+ lambda { Date.ordinal(2000, 367) }.should raise_error(ArgumentError)
10
+ Date.ordinal(2000, 288).should == Date.civil(2000, 10, 14)
11
+ end
12
+
13
+ end