third_base 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,227 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#parse" do
4
+ it "can't handle a empty string" do
5
+ lambda{ Date.parse("") }.should raise_error(ArgumentError)
6
+ end
7
+
8
+ # Specs using numbers
9
+ it "can't handle a single digit" do
10
+ lambda{ Date.parse("1") }.should raise_error(ArgumentError)
11
+ end
12
+
13
+ it "can handle DD as month day number" do
14
+ Date.parse("10").should == Date.civil(Date.today.year, Date.today.month, 10)
15
+ end
16
+
17
+ it "can handle DDD as year day number" do
18
+ Date.parse("050").should == Date.civil(Date.today.year, 2, 19)
19
+ end
20
+
21
+ it "can handle MMDD as month and day" do
22
+ Date.parse("1108").should == Date.civil(Date.today.year, 11, 8)
23
+ end
24
+
25
+ it "can handle YYDDD as year and day number" do
26
+ Date.parse("10100").should == Date.civil(2010, 4, 10)
27
+ end
28
+
29
+ it "can handle YYMMDD as year month and day" do
30
+ Date.parse("201023").should == Date.civil(2020, 10, 23)
31
+ end
32
+
33
+ it "can handle YYYYDDD as year and day number" do
34
+ Date.parse("1910100").should == Date.civil(1910, 4, 10)
35
+ end
36
+
37
+ it "can handle YYYYMMDD as year and day number" do
38
+ Date.parse("19101101").should == Date.civil(1910, 11, 1)
39
+ end
40
+ end
41
+
42
+ describe :date_parse, :shared => true do
43
+ it "can parse a mmm-YYYY string into a Date object" do
44
+ d = Date.parse("feb#{@sep}2008")
45
+ d.year.should == 2008
46
+ d.month.should == 2
47
+ d.day.should == 1
48
+ end
49
+
50
+ it "can parse a 'DD mmm YYYY' string into a Date object" do
51
+ d = Date.parse("23#{@sep}feb#{@sep}2008")
52
+ d.year.should == 2008
53
+ d.month.should == 2
54
+ d.day.should == 23
55
+ end
56
+
57
+ it "can parse a 'mmm DD YYYY' string into a Date object" do
58
+ d = Date.parse("feb#{@sep}23#{@sep}2008")
59
+ d.year.should == 2008
60
+ d.month.should == 2
61
+ d.day.should == 23
62
+
63
+ d = Date.parse("feb#{@sep}23,#{@sep}2008")
64
+ d.year.should == 2008
65
+ d.month.should == 2
66
+ d.day.should == 23
67
+
68
+ d = Date.parse("feb#{@sep}23,#{@sep}08")
69
+ d.year.should == 2008
70
+ d.month.should == 2
71
+ d.day.should == 23
72
+ end
73
+
74
+ it "can parse a 'YYYY mmm DD' string into a Date object" do
75
+ d = Date.parse("2008#{@sep}feb#{@sep}23")
76
+ d.year.should == 2008
77
+ d.month.should == 2
78
+ d.day.should == 23
79
+ end
80
+
81
+ it "can parse a month name and day into a Date object" do
82
+ Date.parse("november#{@sep}5th").should == Date.civil(Date.today.year, 11, 5)
83
+ end
84
+
85
+ it "can parse a month name, day and year into a Date object" do
86
+ Date.parse("november#{@sep}5th#{@sep}2005").should == Date.civil(2005, 11, 5)
87
+ end
88
+
89
+ it "can parse a year, month name and day into a Date object" do
90
+ Date.parse("2005#{@sep}november#{@sep}5th").should == Date.civil(2005, 11, 5)
91
+ end
92
+
93
+ it "can parse a year, day and month name into a Date object" do
94
+ Date.parse("5th#{@sep}november#{@sep}2005").should == Date.civil(2005, 11, 5)
95
+ end
96
+
97
+ it "can handle negative year numbers" do
98
+ Date.parse("5th#{@sep}november#{@sep}-2005").should == Date.civil(-2005, 11, 5)
99
+ end
100
+ end
101
+
102
+ describe :date_parse_us, :shared => true do
103
+ it "parses a YYYY#{@sep}MM#{@sep}DD string into a Date object" do
104
+ d = Date.parse("2007#{@sep}10#{@sep}01")
105
+ d.year.should == 2007
106
+ d.month.should == 10
107
+ d.day.should == 1
108
+ end
109
+
110
+ it "parses a MM#{@sep}DD#{@sep}YYYY string into a Date object" do
111
+ d = Date.parse("10#{@sep}01#{@sep}2007")
112
+ d.year.should == 2007
113
+ d.month.should == 10
114
+ d.day.should == 1
115
+ end
116
+
117
+ it "parses a MM#{@sep}DD#{@sep}YY string into a Date object using the year digits as 20XX" do
118
+ d = Date.parse("10#{@sep}01#{@sep}07")
119
+ d.year.should == 2007
120
+ d.month.should == 10
121
+ d.day.should == 1
122
+ end
123
+ end
124
+
125
+ describe :date_parse_eu, :shared => true do
126
+ before do
127
+ Date.use_parsers(:iso, :eu)
128
+ end
129
+ after do
130
+ Date.reset_parsers!
131
+ end
132
+
133
+ # The - separator let's it work like European format, so it as a different spec
134
+ it "can parse a YYYY-MM-DD string into a Date object" do
135
+ d = Date.parse("2007#{@sep}10#{@sep}01")
136
+ d.year.should == 2007
137
+ d.month.should == 10
138
+ d.day.should == 1
139
+ end
140
+
141
+ it "can parse a DD-MM-YYYY string into a Date object" do
142
+ d = Date.parse("10#{@sep}01#{@sep}2007")
143
+ d.year.should == 2007
144
+ d.month.should == 1
145
+ d.day.should == 10
146
+ end
147
+
148
+ it "can parse a YY-MM-DD string into a Date object" do
149
+ d = Date.parse("10#{@sep}01#{@sep}07")
150
+ d.year.should == 2010
151
+ d.month.should == 1
152
+ d.day.should == 7
153
+ end
154
+ end
155
+
156
+
157
+ describe "Date#parse with '.' separator" do
158
+ before :all do
159
+ @sep = '.'
160
+ end
161
+
162
+ it_should_behave_like "date_parse"
163
+ end
164
+
165
+ describe "Date#parse with '/' separator" do
166
+ before :all do
167
+ @sep = '/'
168
+ end
169
+
170
+ it_should_behave_like "date_parse"
171
+ end
172
+
173
+ describe "Date#parse with ' ' separator" do
174
+ before :all do
175
+ @sep = ' '
176
+ end
177
+
178
+ it_should_behave_like "date_parse"
179
+ end
180
+
181
+ describe "Date#parse with '/' separator US-style" do
182
+ before :all do
183
+ @sep = '/'
184
+ end
185
+
186
+ it_should_behave_like "date_parse_us"
187
+ end
188
+
189
+ ruby_version_is "" ... "1.8.7" do
190
+ describe "Date#parse with '.' separator US-style" do
191
+ before :all do
192
+ @sep = '.'
193
+ end
194
+
195
+ it_should_behave_like "date_parse_us"
196
+ end
197
+ end
198
+
199
+ describe "Date#parse with '-' separator EU-style" do
200
+ before :all do
201
+ @sep = '-'
202
+ end
203
+
204
+ it_should_behave_like "date_parse_eu"
205
+ end
206
+
207
+ describe "Date parser modifications" do
208
+ after do
209
+ Date.reset_parsers!
210
+ end
211
+
212
+ it "should be able to add a parser to an existing parser type that takes precedence" do
213
+ proc{Date.parse("today")}.should raise_error(ArgumentError)
214
+ Date.add_parser(:iso, /today/){t = Time.now; {:civil=>[t.year, t.mon, t.day]}}
215
+ Date.parse("today").should == Date.today
216
+ end
217
+
218
+ it "should be able to add new parser types" do
219
+ proc{Date.parse("today")}.should raise_error(ArgumentError)
220
+ Date.add_parser_type(:mine)
221
+ Date.add_parser(:mine, /today/){t = Time.now; {:civil=>[t.year, t.mon, t.day]}}
222
+ proc{Date.parse("today")}.should raise_error(ArgumentError)
223
+ Date.parse("today", :parser_types=>[:mine]).should == Date.today
224
+ Date.use_parsers(:mine)
225
+ Date.parse("today").should == Date.today
226
+ end
227
+ end
@@ -0,0 +1,55 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#step" do
4
+
5
+ it "should be able to step forward in time" do
6
+ ds = Date.civil(2008, 10, 11)
7
+ de = Date.civil(2008, 9, 29)
8
+ count = 0
9
+ de.step(ds) do |d|
10
+ d.should <= ds
11
+ d.should >= de
12
+ count += 1
13
+ end
14
+ count.should == 13
15
+
16
+ count = 0
17
+ de.step(ds, 5) do |d|
18
+ d.should <= ds
19
+ d.should >= de
20
+ count += 1
21
+ end
22
+ count.should == 3
23
+
24
+ count = 0
25
+ ds.step(de) do |d|; count += 1; end
26
+ count.should == 0
27
+
28
+ end
29
+
30
+ it "should be able to step backward in time" do
31
+ ds = Date.civil(2000, 4, 14)
32
+ de = Date.civil(2000, 3, 29)
33
+ count = 0
34
+ ds.step(de, -1) do |d|
35
+ d.should <= ds
36
+ d.should >= de
37
+ count += 1
38
+ end
39
+ count.should == 17
40
+
41
+ count = 0
42
+ ds.step(de, -5) do |d|
43
+ d.should <= ds
44
+ d.should >= de
45
+ count += 1
46
+ end
47
+ count.should == 4
48
+
49
+ count = 0
50
+ de.step(ds, -1) do |d|; count += 1; end
51
+ count.should == 0
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,132 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#strftime" do
4
+
5
+ it "should be able to print the date" do
6
+ Date.civil(2000, 4, 6).strftime.should == "2000-04-06"
7
+ Date.civil(2000, 4, 6).strftime.should == Date.civil(2000, 4, 6).to_s
8
+ end
9
+
10
+ it "should be able to print the full day name" do
11
+ Date.civil(2000, 4, 6).strftime("%A").should == "Thursday"
12
+ end
13
+
14
+ it "should be able to print the short day name" do
15
+ Date.civil(2000, 4, 6).strftime("%a").should == "Thu"
16
+ end
17
+
18
+ it "should be able to print the full month name" do
19
+ Date.civil(2000, 4, 6).strftime("%B").should == "April"
20
+ end
21
+
22
+ it "should be able to print the short month name" do
23
+ Date.civil(2000, 4, 6).strftime("%b").should == "Apr"
24
+ Date.civil(2000, 4, 6).strftime("%h").should == "Apr"
25
+ Date.civil(2000, 4, 6).strftime("%b").should == Date.civil(2000, 4, 6).strftime("%h")
26
+ end
27
+
28
+ it "should be able to print the century" do
29
+ Date.civil(2000, 4, 6).strftime("%C").should == "20"
30
+ end
31
+
32
+ it "should be able to print the month day with leading zeroes" do
33
+ Date.civil(2000, 4, 6).strftime("%d").should == "06"
34
+ end
35
+
36
+ it "should be able to print the month day with leading spaces" do
37
+ Date.civil(2000, 4, 6).strftime("%e").should == " 6"
38
+ end
39
+
40
+ it "should be able to print the commercial year with leading zeroes" do
41
+ Date.civil(2000, 4, 6).strftime("%G").should == "2000"
42
+ Date.civil( 200, 4, 6).strftime("%G").should == "0200"
43
+ end
44
+
45
+ it "should be able to print the commercial year with only two digits" do
46
+ Date.civil(2000, 4, 6).strftime("%g").should == "00"
47
+ Date.civil( 200, 4, 6).strftime("%g").should == "00"
48
+ end
49
+
50
+ it "should be able to print the year day with leading zeroes" do
51
+ Date.civil(2000, 4, 6).strftime("%j").should == "097"
52
+ end
53
+
54
+ it "should be able to print the month with leading zeroes" do
55
+ Date.civil(2000, 4, 6).strftime("%m").should == "04"
56
+ end
57
+
58
+ it "should be able to add a newline" do
59
+ Date.civil(2000, 4, 6).strftime("%n").should == "\n"
60
+ end
61
+
62
+ it "should be able to add a tab" do
63
+ Date.civil(2000, 4, 6).strftime("%t").should == "\t"
64
+ end
65
+
66
+ it "should be able to show the week number with the week starting on sunday and monday" do
67
+ Date.civil(2000, 4, 6).strftime("%U").should == "14"
68
+ Date.civil(2000, 4, 6).strftime("%W").should == "14"
69
+ Date.civil(2000, 4, 6).strftime("%U").should == Date.civil(2000, 4, 6).strftime("%W")
70
+ Date.civil(2000, 4, 9).strftime("%U").should == "15"
71
+ Date.civil(2000, 4, 9).strftime("%W").should == "14"
72
+ Date.civil(2000, 4, 9).strftime("%U").should_not == Date.civil(2000, 4, 9).strftime("%W")
73
+ end
74
+
75
+ it "should be able to show the commercial week day" do
76
+ Date.civil(2000, 4, 9).strftime("%u").should == "7"
77
+ Date.civil(2000, 4, 10).strftime("%u").should == "1"
78
+ end
79
+
80
+ it "should be able to show the commercial week" do
81
+ Date.civil(2000, 4, 9).strftime("%V").should == "14"
82
+ Date.civil(2000, 4, 10).strftime("%V").should == "15"
83
+ end
84
+
85
+ it "should be able to show the week day" do
86
+ Date.civil(2000, 4, 9).strftime("%w").should == "0"
87
+ Date.civil(2000, 4, 10).strftime("%w").should == "1"
88
+ end
89
+
90
+ it "should be able to show the year in YYYY format" do
91
+ Date.civil(2000, 4, 9).strftime("%Y").should == "2000"
92
+ end
93
+
94
+ it "should be able to show the year in YY format" do
95
+ Date.civil(2000, 4, 9).strftime("%y").should == "00"
96
+ end
97
+
98
+ it "should be able to escape the % character" do
99
+ Date.civil(2000, 4, 9).strftime("%%").should == "%"
100
+ end
101
+
102
+ it "should leave %X sequences alone if it doesn't have a conversion" do
103
+ Date.civil(2000, 4, 9).strftime("%1").should == "%1"
104
+ Date.civil(2000, 4, 9).strftime("%P").should == "%P"
105
+ end
106
+
107
+ ############################
108
+ # Specs that combine stuff #
109
+ ############################
110
+
111
+ it "should be able to print the date with slashes" do
112
+ Date.civil(2000, 4, 6).strftime("%D").should == "04/06/00"
113
+ Date.civil(2000, 4, 6).strftime("%D").should == Date.civil(2000, 4, 6).strftime('%m/%d/%y')
114
+ end
115
+
116
+ it "should be able to print the date as YYYY-MM-DD" do
117
+ Date.civil(2000, 4, 6).strftime("%F").should == "2000-04-06"
118
+ Date.civil(2000, 4, 6).strftime("%F").should == Date.civil(2000, 4, 6).strftime('%Y-%m-%d')
119
+ end
120
+
121
+ it "should be able to show the commercial week" do
122
+ Date.civil(2000, 4, 9).strftime("%v").should == " 9-Apr-2000"
123
+ Date.civil(2000, 4, 9).strftime("%v").should == Date.civil(2000, 4, 9).strftime('%e-%b-%Y')
124
+ end
125
+
126
+ it "should be able to show MM/DD/YY" do
127
+ Date.civil(2000, 4, 6).strftime("%x").should == "04/06/00"
128
+ Date.civil(2000, 4, 6).strftime("%x").should == Date.civil(2000, 4, 6).strftime('%m/%d/%y')
129
+ end
130
+
131
+
132
+ end
@@ -0,0 +1,118 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#stpftime" do
4
+ it "should be able to parse the default date format" do
5
+ Date.strptime("2000-04-06").should == Date.civil(2000, 4, 6)
6
+ Date.civil(2000, 4, 6).strftime.should == Date.civil(2000, 4, 6).to_s
7
+ end
8
+
9
+ it "should be able to parse the full day name" do
10
+ d = Date.today
11
+ # strptime assumed week that start on sunday, not monday
12
+ week = d.cweek
13
+ week += 1 if d.cwday == 7
14
+ Date.strptime("Thursday", "%A").should == Date.commercial(d.cwyear, week, 4)
15
+ end
16
+
17
+ it "should be able to parse the short day name" do
18
+ d = Date.today
19
+ # strptime assumed week that start on sunday, not monday
20
+ week = d.cweek
21
+ week += 1 if d.cwday == 7
22
+ Date.strptime("Thu", "%a").should == Date.commercial(d.cwyear, week, 4)
23
+ end
24
+
25
+ it "should be able to parse the full month name" do
26
+ d = Date.today
27
+ Date.strptime("April", "%B").should == Date.civil(d.year, 4, 1)
28
+ end
29
+
30
+ it "should be able to parse the short month name" do
31
+ d = Date.today
32
+ Date.strptime("Apr", "%b").should == Date.civil(d.year, 4, 1)
33
+ Date.strptime("Apr", "%h").should == Date.civil(d.year, 4, 1)
34
+ end
35
+
36
+ it "should be able to parse the century" do
37
+ Date.strptime("06 20", "%y %C").should == Date.civil(2006, 1, 1)
38
+ end
39
+
40
+ it "should be able to parse the month day with leading zeroes" do
41
+ d = Date.today
42
+ Date.strptime("06", "%d").should == Date.civil(d.year, d.month, 6)
43
+ end
44
+
45
+ it "should be able to parse the month day with leading spaces" do
46
+ d = Date.today
47
+ Date.strptime(" 6", "%e").should == Date.civil(d.year, d.month, 6)
48
+ end
49
+
50
+ it "should be able to parse the commercial year with leading zeroes" do
51
+ Date.strptime("2000", "%G").should == Date.civil(2000, 1, 3)
52
+ Date.strptime("2002", "%G").should == Date.civil(2001, 12, 31)
53
+ end
54
+
55
+ it "should be able to parse the commercial year with only two digits" do
56
+ Date.strptime("68", "%g").should == Date.civil(2068, 1, 2)
57
+ Date.strptime("69", "%g").should == Date.civil(1968, 12, 30)
58
+ end
59
+
60
+ it "should be able to parse the year day with leading zeroes" do
61
+ d = Date.today
62
+ Date.strptime("050", "%j").should == Date.civil(2008, 2, 19)
63
+ end
64
+
65
+ it "should be able to parse the month with leading zeroes" do
66
+ d = Date.today
67
+ Date.strptime("04", "%m").should == Date.civil(d.year, 4, 1)
68
+ end
69
+
70
+ it "should be able to show the commercial day" do
71
+ Date.strptime("1", "%u").should == Date.commercial(Date.today.year, Date.today.cweek, 1)
72
+ Date.strptime("15 3", "%V %u").should == Date.commercial(Date.today.year, 15, 3)
73
+ end
74
+
75
+ it "should be able to show the commercial week" do
76
+ d = Date.commercial(Date.today.year,1,1)
77
+ Date.strptime("1", "%V").should == d
78
+ Date.strptime("15", "%V").should == Date.commercial(d.cwyear, 15, 1)
79
+ end
80
+
81
+ it "should be able to show the year in YYYY format" do
82
+ Date.strptime("2007", "%Y").should == Date.civil(2007, 1, 1)
83
+ end
84
+
85
+ it "should be able to show the year in YY format" do
86
+ Date.strptime("00", "%y").should == Date.civil(2000, 1, 1)
87
+ end
88
+
89
+ it "should be able to parse escapes" do
90
+ Date.strptime("00 % \n \t %1", "%y %% %n %t %1").should == Date.civil(2000, 1, 1)
91
+ end
92
+
93
+ ############################
94
+ # Specs that combine stuff #
95
+ ############################
96
+
97
+
98
+ it "should be able to parse the date with slashes" do
99
+ Date.strptime("04/06/00", "%D").should == Date.civil(2000, 4, 6)
100
+ Date.strptime("04/06/00", "%m/%d/%y").should == Date.civil(2000, 4, 6)
101
+ end
102
+
103
+ it "should be able to parse the date as YYYY-MM-DD" do
104
+ Date.strptime("2000-04-06", "%F").should == Date.civil(2000, 4, 6)
105
+ Date.strptime("2000-04-06", "%Y-%m-%d").should == Date.civil(2000, 4, 6)
106
+ end
107
+
108
+ it "should be able to show the commercial week" do
109
+ Date.strptime(" 9-Apr-2000", "%v").should == Date.civil(2000, 4, 9)
110
+ Date.strptime(" 9-Apr-2000", "%e-%b-%Y").should == Date.civil(2000, 4, 9)
111
+ end
112
+
113
+ it "should be able to show MM/DD/YY" do
114
+ Date.strptime("04/06/00", "%x").should == Date.civil(2000, 4, 6)
115
+ Date.strptime("04/06/00", "%m/%d/%y").should == Date.civil(2000, 4, 6)
116
+ end
117
+
118
+ end
@@ -0,0 +1,16 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#succ" do
4
+ it "should be the next day" do
5
+ ds = Date.civil(2008, 10, 11)
6
+ ds.succ.should == Date.civil(2008, 10, 12)
7
+ ds = Date.civil(2008, 10, 31)
8
+ ds.succ.should == Date.civil(2008, 11, 1)
9
+ ds = Date.commercial(2008, 2, 7)
10
+ ds.succ.should == Date.commercial(2008, 3, 1)
11
+ ds = Date.jd(2008)
12
+ ds.succ.should == Date.jd(2009)
13
+ ds = Date.ordinal(2008, 366)
14
+ ds.succ.should == Date.ordinal(2009, 1)
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date.today" do
4
+ it "should be today's date" do
5
+ t = Time.now
6
+ d = Date.today
7
+ d.year.should == t.year
8
+ d.mon.should == t.mon
9
+ d.day.should == t.day
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'date_spec_helper'))
2
+
3
+ describe "Date#upto" do
4
+
5
+ it "should be able to step forward in time" do
6
+ ds = Date.civil(2008, 10, 11)
7
+ de = Date.civil(2008, 9, 29)
8
+ count = 0
9
+ de.upto(ds) do |d|
10
+ d.should <= ds
11
+ d.should >= de
12
+ count += 1
13
+ end
14
+ count.should == 13
15
+ end
16
+
17
+ end
@@ -0,0 +1,3 @@
1
+ require 'third_base/date'
2
+ include ThirdBase
3
+ require(File.join(File.dirname(__FILE__), 'spec_helper'))
@@ -0,0 +1,53 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'datetime_spec_helper'))
2
+
3
+ describe "DateTime#fract" do
4
+ it "should be able to determine the fraction of a day" do
5
+ DateTime.jd_fract(2007).fract.should be_close(0.0, 0.000000001)
6
+ DateTime.jd_fract(2007, 0.5).fract.should be_close(0.5, 0.000000001)
7
+ DateTime.jd(2007, 12).fract.should be_close(0.5, 0.000000001)
8
+ end
9
+ end
10
+
11
+ describe "DateTime#hour" do
12
+ it "should be able to determine the hour of the day" do
13
+ DateTime.jd(2007, 1).hour.should == 1
14
+ DateTime.jd_fract(2007, 0.5).hour.should == 12
15
+ end
16
+ end
17
+
18
+ describe "DateTime#min" do
19
+ it "should be able to determine the minute of the day" do
20
+ DateTime.jd(2007, 1, 2).min.should == 2
21
+ DateTime.jd_fract(2007, 0.021).min.should == 30
22
+ end
23
+ end
24
+
25
+ describe "DateTime#offset and #utc_offset" do
26
+ it "should be able to determine the offset of the day from UTC" do
27
+ DateTime.jd(2007, 1, 2, 3, 4, 6).offset.should == 6
28
+ DateTime.jd_fract(2007, 1.15740740740741e-006, 10).offset.should == 10
29
+ DateTime.jd(2007, 1, 2, 3, 4, 6).utc_offset.should == 6
30
+ DateTime.jd_fract(2007, 1.15740740740741e-006, 10).utc_offset.should == 10
31
+ end
32
+ end
33
+
34
+ describe "DateTime#sec" do
35
+ it "should be able to determine the second of the day" do
36
+ DateTime.jd(2007, 1, 2, 3).sec.should == 3
37
+ DateTime.jd_fract(2007, 0.00035).sec.should == 30
38
+ end
39
+ end
40
+
41
+ describe "DateTime#usec" do
42
+ it "should be able to determine the millisecond of the day" do
43
+ DateTime.jd(2007, 1, 2, 3, 4).usec.should == 4
44
+ DateTime.jd_fract(2007, 0.000001158).usec.should == 100051
45
+ end
46
+ end
47
+
48
+ describe "DateTime#zone" do
49
+ it "should give the offset as a string" do
50
+ DateTime.jd(0).zone.should == '+00:00'
51
+ DateTime.jd(2007, 0, 0, 0, 0, -3600).zone.should == '-01:00'
52
+ end
53
+ end
@@ -0,0 +1,36 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'datetime_spec_helper'))
2
+
3
+ describe "DateTime#+" do
4
+
5
+ it "should add a number of days to a Date" do
6
+ (DateTime.civil(2007,2,27) + 315).should == DateTime.civil(2008, 1, 8)
7
+ (DateTime.commercial(2007,2,2) + 315).should == DateTime.commercial(2007, 47, 2)
8
+ (DateTime.jd(2454782) + 315).should == DateTime.jd(2455097)
9
+ (DateTime.ordinal(2008, 10) + 315).should == DateTime.ordinal(2008, 325)
10
+ end
11
+
12
+ it "should add a fractional number of days to a Date" do
13
+ (DateTime.civil(2007,2,27) + 315.5).should == DateTime.civil(2008, 1, 8, 12)
14
+ (DateTime.commercial(2007,2,2) + 315.75).should == DateTime.commercial(2007, 47, 2, 18)
15
+ (DateTime.jd(2454782) + 315.25).should == DateTime.jd(2455097, 6)
16
+ (DateTime.ordinal(2008, 10) + 315.25).should == DateTime.ordinal(2008, 325, 6)
17
+ end
18
+
19
+ it "should add a negative number of days to a Date" do
20
+ d = DateTime.civil(2007,2,27).+(-10)
21
+ d.should == DateTime.civil(2007, 2, 17)
22
+ end
23
+
24
+ it "should add a fractional negative number of days to a Date" do
25
+ d = DateTime.civil(2007,2,27).+(-10.5)
26
+ d.should == DateTime.civil(2007, 2, 16, 12)
27
+ end
28
+
29
+ it "should raise an error on non numeric parameters" do
30
+ lambda { DateTime.civil(2007,2,27) + :hello }.should raise_error(TypeError)
31
+ lambda { DateTime.civil(2007,2,27) + "hello" }.should raise_error(TypeError)
32
+ lambda { DateTime.civil(2007,2,27) + DateTime.new(2007,2,27) }.should raise_error(TypeError)
33
+ lambda { DateTime.civil(2007,2,27) + Object.new }.should raise_error(TypeError)
34
+ end
35
+
36
+ end