home_run 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/CHANGELOG +3 -0
  2. data/LICENSE +19 -0
  3. data/README.rdoc +314 -0
  4. data/Rakefile +140 -0
  5. data/bench/cpu_bench.rb +279 -0
  6. data/bench/dt_garbage_bench.rb +11 -0
  7. data/bench/dt_mem_bench.rb +14 -0
  8. data/bench/garbage_bench.rb +11 -0
  9. data/bench/mem_bench.rb +14 -0
  10. data/bin/home_run +87 -0
  11. data/default.mspec +12 -0
  12. data/ext/date/format.rb +842 -0
  13. data/ext/date.rb +7 -0
  14. data/ext/date_ext.c +4548 -0
  15. data/ext/date_parser.c +367 -0
  16. data/ext/date_parser.rl +134 -0
  17. data/ext/datetime.c +2804 -0
  18. data/ext/extconf.rb +6 -0
  19. data/spec/date/accessor_spec.rb +176 -0
  20. data/spec/date/add_month_spec.rb +26 -0
  21. data/spec/date/add_spec.rb +23 -0
  22. data/spec/date/boat_spec.rb +38 -0
  23. data/spec/date/civil_spec.rb +147 -0
  24. data/spec/date/commercial_spec.rb +153 -0
  25. data/spec/date/constants_spec.rb +44 -0
  26. data/spec/date/conversions_spec.rb +246 -0
  27. data/spec/date/day_spec.rb +73 -0
  28. data/spec/date/downto_spec.rb +17 -0
  29. data/spec/date/eql_spec.rb +16 -0
  30. data/spec/date/format_spec.rb +52 -0
  31. data/spec/date/gregorian_spec.rb +52 -0
  32. data/spec/date/hash_spec.rb +11 -0
  33. data/spec/date/julian_spec.rb +129 -0
  34. data/spec/date/leap_spec.rb +19 -0
  35. data/spec/date/minus_month_spec.rb +25 -0
  36. data/spec/date/minus_spec.rb +51 -0
  37. data/spec/date/next_prev_spec.rb +108 -0
  38. data/spec/date/ordinal_spec.rb +83 -0
  39. data/spec/date/parse_spec.rb +442 -0
  40. data/spec/date/parsing_spec.rb +77 -0
  41. data/spec/date/relationship_spec.rb +28 -0
  42. data/spec/date/step_spec.rb +109 -0
  43. data/spec/date/strftime_spec.rb +223 -0
  44. data/spec/date/strptime_spec.rb +201 -0
  45. data/spec/date/succ_spec.rb +20 -0
  46. data/spec/date/today_spec.rb +15 -0
  47. data/spec/date/upto_spec.rb +17 -0
  48. data/spec/datetime/accessor_spec.rb +218 -0
  49. data/spec/datetime/add_month_spec.rb +26 -0
  50. data/spec/datetime/add_spec.rb +36 -0
  51. data/spec/datetime/boat_spec.rb +43 -0
  52. data/spec/datetime/constructor_spec.rb +142 -0
  53. data/spec/datetime/conversions_spec.rb +54 -0
  54. data/spec/datetime/day_spec.rb +73 -0
  55. data/spec/datetime/downto_spec.rb +39 -0
  56. data/spec/datetime/eql_spec.rb +17 -0
  57. data/spec/datetime/format_spec.rb +59 -0
  58. data/spec/datetime/hash_spec.rb +11 -0
  59. data/spec/datetime/leap_spec.rb +19 -0
  60. data/spec/datetime/minus_month_spec.rb +25 -0
  61. data/spec/datetime/minus_spec.rb +77 -0
  62. data/spec/datetime/next_prev_spec.rb +138 -0
  63. data/spec/datetime/now_spec.rb +18 -0
  64. data/spec/datetime/parse_spec.rb +390 -0
  65. data/spec/datetime/parsing_spec.rb +77 -0
  66. data/spec/datetime/relationship_spec.rb +28 -0
  67. data/spec/datetime/step_spec.rb +155 -0
  68. data/spec/datetime/strftime_spec.rb +118 -0
  69. data/spec/datetime/strptime_spec.rb +117 -0
  70. data/spec/datetime/succ_spec.rb +24 -0
  71. data/spec/datetime/upto_spec.rb +39 -0
  72. data/spec/spec_helper.rb +59 -0
  73. metadata +138 -0
@@ -0,0 +1,51 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
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 be able to compute the difference between a Date and a DateTime" do
42
+ (Date.civil(2007, 4, 19) - DateTime.civil(2007, 4, 18, 12)).should == 0.5
43
+ end
44
+
45
+ it "should raise an error on non numeric parameters" do
46
+ lambda { Date.civil(2007,2,27) - :hello }.should raise_error(TypeError)
47
+ lambda { Date.civil(2007,2,27) - "hello" }.should raise_error(TypeError)
48
+ lambda { Date.civil(2007,2,27) - Object.new }.should raise_error(TypeError)
49
+ end
50
+
51
+ end
@@ -0,0 +1,108 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Date#next_day" do
5
+ it "should add a single day to a Date if no arguments" do
6
+ (Date.civil(2007,2,27).next_day).should == Date.civil(2007, 2, 28)
7
+ end
8
+
9
+ it "should add the given number of days to a Date if an argument" do
10
+ (Date.civil(2007,2,27).next_day(2)).should == Date.civil(2007, 3, 1)
11
+ end
12
+
13
+ it "should handle a negative argument by subtracting days" do
14
+ (Date.civil(2007,2,27).next_day(-2)).should == Date.civil(2007, 2, 25)
15
+ end
16
+ end
17
+
18
+ describe "Date#prev_day" do
19
+ it "should subtract a single day to a Date if no arguments" do
20
+ (Date.civil(2007,2,27).prev_day).should == Date.civil(2007, 2, 26)
21
+ end
22
+
23
+ it "should subtract the given number of days to a Date if an argument" do
24
+ (Date.civil(2007,2,27).prev_day(2)).should == Date.civil(2007, 2, 25)
25
+ end
26
+
27
+ it "should handle a negative argument by adding days" do
28
+ (Date.civil(2007,2,27).prev_day(-2)).should == Date.civil(2007, 3, 1)
29
+ end
30
+ end
31
+
32
+ describe "Date#next_month" do
33
+ it "should add a single month to a Date if no arguments" do
34
+ (Date.civil(2007,2,27).next_month).should == Date.civil(2007, 3, 27)
35
+ end
36
+
37
+ it "should add the given number of months to a Date if an argument" do
38
+ (Date.civil(2007,2,27).next_month(2)).should == Date.civil(2007, 4, 27)
39
+ end
40
+
41
+ it "should handle a negative argument by subtracting months" do
42
+ (Date.civil(2007,2,27).next_month(-2)).should == Date.civil(2006, 12, 27)
43
+ end
44
+
45
+ it "should handle adding a month where the new date is not a valid date" do
46
+ (Date.civil(2007,1,31).next_month).should == Date.civil(2007, 2, 28)
47
+ (Date.civil(2008,1,31).next_month).should == Date.civil(2008, 2, 29)
48
+ (Date.civil(2007,1,31).next_month(3)).should == Date.civil(2007, 4, 30)
49
+ end
50
+ end
51
+
52
+ describe "Date#prev_month" do
53
+ it "should subtract a single month to a Date if no arguments" do
54
+ (Date.civil(2007,2,27).prev_month).should == Date.civil(2007, 1, 27)
55
+ end
56
+
57
+ it "should subtract the given number of months to a Date if an argument" do
58
+ (Date.civil(2007,2,27).prev_month(2)).should == Date.civil(2006, 12, 27)
59
+ end
60
+
61
+ it "should handle a negative argument by adding months" do
62
+ (Date.civil(2007,2,27).prev_month(-2)).should == Date.civil(2007, 4, 27)
63
+ end
64
+
65
+ it "should handle subtracting a month where the new date is not a valid date" do
66
+ (Date.civil(2007,3,31).prev_month).should == Date.civil(2007, 2, 28)
67
+ (Date.civil(2008,3,31).prev_month).should == Date.civil(2008, 2, 29)
68
+ (Date.civil(2007,3,31).prev_month(4)).should == Date.civil(2006, 11, 30)
69
+ end
70
+ end
71
+
72
+ describe "Date#next_year" do
73
+ it "should add a single year to a Date if no arguments" do
74
+ (Date.civil(2007,2,27).next_year).should == Date.civil(2008, 2, 27)
75
+ end
76
+
77
+ it "should add the given number of years to a Date if an argument" do
78
+ (Date.civil(2007,2,27).next_year(2)).should == Date.civil(2009, 2, 27)
79
+ end
80
+
81
+ it "should handle a negative argument by subtracting years" do
82
+ (Date.civil(2007,2,27).next_year(-2)).should == Date.civil(2005, 2, 27)
83
+ end
84
+
85
+ it "should handle adding a year where the new date is not a valid date" do
86
+ (Date.civil(2008,2,29).next_year).should == Date.civil(2009, 2, 28)
87
+ end
88
+ end
89
+
90
+ describe "Date#prev_year" do
91
+ it "should add a single year to a Date if no arguments" do
92
+ (Date.civil(2007,2,27).prev_year).should == Date.civil(2006, 2, 27)
93
+ end
94
+
95
+ it "should add the given number of years to a Date if an argument" do
96
+ (Date.civil(2007,2,27).prev_year(2)).should == Date.civil(2005, 2, 27)
97
+ end
98
+
99
+ it "should handle a negative argument by subtracting years" do
100
+ (Date.civil(2007,2,27).prev_year(-2)).should == Date.civil(2009, 2, 27)
101
+ end
102
+
103
+ it "should handle adding a year where the new date is not a valid date" do
104
+ (Date.civil(2008,2,29).prev_year).should == Date.civil(2007, 2, 28)
105
+ end
106
+ end
107
+
108
+ end
@@ -0,0 +1,83 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Date.ordinal" do
4
+
5
+ it "should be able to construct a Date object from an ordinal date" do
6
+ Date.ordinal(1582, 274).should == Date.civil(1582, 10, 1)
7
+ Date.ordinal(1582, 277).should == Date.civil(1582, 10, 4)
8
+ Date.ordinal(1582, 288).should == Date.civil(1582, 10, 15)
9
+ Date.ordinal(1582, 287, Date::ENGLAND).should == Date.civil(1582, 10, 14, Date::ENGLAND)
10
+ end
11
+
12
+ it "should have defaults and an optional sg value" do
13
+ Date.ordinal.should == Date.jd
14
+ Date.ordinal(2008).should == Date.ordinal(2008, 1)
15
+ Date.ordinal(2008, 1, 1).should == Date.ordinal(2008, 1)
16
+ end
17
+
18
+ it "should not accept more than 3 arguments" do
19
+ proc{Date.ordinal(2008, 1, 1, 1)}.should raise_error(ArgumentError)
20
+ end
21
+
22
+ it "raises errors for invalid dates" do
23
+ lambda { Date.ordinal(2007, 366) }.should raise_error(ArgumentError)
24
+ lambda { Date.ordinal(2008, 366) }.should_not raise_error(ArgumentError)
25
+ lambda { Date.ordinal(2008, 367) }.should raise_error(ArgumentError)
26
+ end
27
+
28
+ ruby_version_is "" ... "1.9" do
29
+ it ".new2 should be the same as ordinal" do
30
+ Date.new2(2008, 10).should == Date.ordinal(2008, 10)
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "Date.valid_ordinal?" do
36
+
37
+ ruby_version_is "" ... "1.9" do
38
+ it "should be able to determine if the date is a valid ordinal date" do
39
+ Date.valid_ordinal?(1582, 277).should == Date.civil(1582, 10, 4).jd
40
+ Date.valid_ordinal?(1582, 278).should == Date.civil(1582, 10, 5).jd
41
+ Date.valid_ordinal?(1582, 287).should == Date.civil(1582, 10, 14).jd
42
+ Date.valid_ordinal?(1582, 288).should == Date.civil(1582, 10, 15).jd
43
+ Date.valid_ordinal?(1582, 287, Date::ENGLAND).should_not == nil
44
+ Date.valid_ordinal?(1582, 287, Date::ENGLAND).should == Date.civil(1582, 10, 14, Date::ENGLAND).jd
45
+
46
+ Date.valid_ordinal?(2007, 55).should == 2454156
47
+ Date.valid_ordinal?(2007, 55, 1).should == 2454156
48
+ Date.valid_ordinal?(2007, 367, 1).should == nil
49
+ end
50
+
51
+ it "#exist2? should be the same as valid_ordinal?" do
52
+ Date.exist2?(2007, 55, 1).should == Date.valid_ordinal?(2007, 55)
53
+ end
54
+
55
+ it "should be able to handle negative day numbers" do
56
+ Date.valid_ordinal?(1582, -89).should == Date.civil(1582, 10, 4).jd
57
+ Date.valid_ordinal?(1582, -88).should == Date.civil(1582, 10, 5).jd
58
+ Date.valid_ordinal?(1582, -79).should == Date.civil(1582, 10, 14).jd
59
+ Date.valid_ordinal?(1582, -78).should == Date.civil(1582, 10, 15).jd
60
+ Date.valid_ordinal?(2007, -100).should == Date.valid_ordinal?(2007, 266)
61
+ end
62
+ end
63
+
64
+ ruby_version_is "1.9" do
65
+ it "should be able to determine if the date is a valid ordinal date" do
66
+ Date.valid_ordinal?(1582, 277).should == true
67
+ Date.valid_ordinal?(1582, 278).should == true
68
+ Date.valid_ordinal?(1582, 287).should == true
69
+ Date.valid_ordinal?(1582, 288).should == true
70
+
71
+ Date.valid_ordinal?(2007, 55).should == true
72
+ Date.valid_ordinal?(2007, 55, 1).should == true
73
+ Date.valid_ordinal?(2007, 367, 1).should == false
74
+ end
75
+
76
+ it "should be able to handle negative day numbers" do
77
+ Date.valid_ordinal?(1582, -79).should == true
78
+ Date.valid_ordinal?(1582, -78).should == true
79
+ Date.valid_ordinal?(2007, -100).should == true
80
+ end
81
+ end
82
+
83
+ end
@@ -0,0 +1,442 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ ruby_version_is "" ... "1.9" do
4
+ describe "Date.zone_to_diff" do
5
+ it "should return the offset for the given time zone name" do
6
+ Date.zone_to_diff('PST').should == -28800
7
+ Date.zone_to_diff('Tokyo').should == 32400
8
+ end
9
+
10
+ it "should return the offset for the given numeric time zone with colons" do
11
+ Date.zone_to_diff('+08:00').should == 28800
12
+ Date.zone_to_diff('+08:30').should == 30600
13
+ Date.zone_to_diff('+08:30:04').should == 30604
14
+ end
15
+
16
+ it "should return the offset for the given numeric time zone with . or ," do
17
+ Date.zone_to_diff('+08,30').should == 29880
18
+ Date.zone_to_diff('+08.30').should == 29880
19
+ end
20
+
21
+ it "should return the offset for the given numeric time with all digits" do
22
+ Date.zone_to_diff('-1').should == -3600
23
+ Date.zone_to_diff('-10').should == -36000
24
+ Date.zone_to_diff('-101').should == -3660
25
+ Date.zone_to_diff('-1010').should == -36600
26
+ Date.zone_to_diff('-10101').should == -36601
27
+ Date.zone_to_diff('-101010').should == -36610
28
+ Date.zone_to_diff('-1010109').should == -36610
29
+ end
30
+
31
+ it "should return 0 if the time zone is not recognized" do
32
+ Date.zone_to_diff('blah').should == 0
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "Date.parse" do
38
+ it "._parse should parse the date string and return a hash" do
39
+ Date._parse('2008-10-20 11:12:13.0-08:00').should == {:mon=>10, :zone=>'-08:00', :sec=>13, :sec_fraction=>0.0, :year=>2008, :hour=>11, :offset=>-28800, :mday=>20, :min=>12}
40
+ Date._parse('2008-10-20 11:12:13.0-08:00', true).should == {:mon=>10, :zone=>'-08:00', :sec=>13, :sec_fraction=>0.0, :year=>2008, :hour=>11, :offset=>-28800, :mday=>20, :min=>12}
41
+ end
42
+
43
+ it "._parse should not contain fields in the hash that were guessed" do
44
+ Date._parse('2008-10-20').should == {:year=>2008, :mday=>20, :mon=>10}
45
+ Date._parse('11:12:13').should == {:sec=>13, :hour=>11, :min=>12}
46
+ end
47
+
48
+ it "should have defaults and an optional sg value" do
49
+ Date.parse.should == Date.jd
50
+ Date.parse('2008-10-11').should == Date.civil(2008, 10, 11)
51
+ Date.parse('2008-10-11', true).should == Date.civil(2008, 10, 11)
52
+ Date.parse('2008-10-11', true, 1).should == Date.civil(2008, 10, 11)
53
+ end
54
+
55
+ it "raises errors for invalid dates" do
56
+ lambda { Date.parse("") }.should raise_error(ArgumentError)
57
+ lambda { Date.parse("2009-02-29") }.should raise_error(ArgumentError)
58
+ end
59
+
60
+ # The space separator is also different, doesn't work for only numbers
61
+ it "can parse a day name into a Date object" do
62
+ d = Date.parse("friday")
63
+ d.should == Date.commercial(d.cwyear, d.cweek, 5)
64
+ end
65
+
66
+ it "can parse a month name into a Date object" do
67
+ d = Date.parse("october")
68
+ d.should == Date.civil(Date.today.year, 10)
69
+ end
70
+
71
+ it "can parse a month day into a Date object" do
72
+ d = Date.parse("5th")
73
+ d.should == Date.civil(Date.today.year, Date.today.month, 5)
74
+ end
75
+
76
+ # Specs using numbers
77
+ it "can't handle a single digit" do
78
+ lambda{ Date.parse("1") }.should raise_error(ArgumentError)
79
+ end
80
+
81
+ it "can handle DD as month day number" do
82
+ d = Date.parse("10")
83
+ d.should == Date.civil(Date.today.year, Date.today.month, 10)
84
+ end
85
+
86
+ it "can handle DDD as year day number" do
87
+ d = Date.parse("100")
88
+ if Date.gregorian_leap?(Date.today.year)
89
+ d.should == Date.civil(Date.today.year, 4, 9)
90
+ else
91
+ d.should == Date.civil(Date.today.year, 4, 10)
92
+ end
93
+ end
94
+
95
+ it "can handle MMDD as month and day" do
96
+ d = Date.parse("1108")
97
+ d.should == Date.civil(Date.today.year, 11, 8)
98
+ end
99
+
100
+ it "can handle YYDDD as year and day number in 1969--2068" do
101
+ d = Date.parse("10100")
102
+ d.should == Date.civil(2010, 4, 10)
103
+ end
104
+
105
+ it "can handle YYMMDD as year month and day in 1969--2068" do
106
+ d = Date.parse("201023")
107
+ d.should == Date.civil(2020, 10, 23)
108
+ end
109
+
110
+ it "can handle YYDDD as a 2 digit year and day number" do
111
+ d = Date.parse("10100", false)
112
+ d.should == Date.civil(10, 4, 10)
113
+ end
114
+
115
+ it "can handle YYMMDD as 2 digit year month and day" do
116
+ d = Date.parse("201023", false)
117
+ d.should == Date.civil(20, 10, 23)
118
+ end
119
+
120
+ it "can handle YYYYDDD as year and day number" do
121
+ d = Date.parse("1910100")
122
+ d.should == Date.civil(1910, 4, 10)
123
+ end
124
+
125
+ it "can handle YYYYMMDD as year and day number" do
126
+ d = Date.parse("19101101")
127
+ d.should == Date.civil(1910, 11, 1)
128
+ end
129
+ end
130
+
131
+ describe :date_parse, :shared => true do
132
+ it "can parse a mmm-YYYY string into a Date object" do
133
+ d = Date.parse("feb#{@sep}2008")
134
+ d.year.should == 2008
135
+ d.month.should == 2
136
+ d.day.should == 1
137
+ end
138
+
139
+ it "can parse a 'DD mmm YYYY' string into a Date object" do
140
+ d = Date.parse("23#{@sep}feb#{@sep}2008")
141
+ d.year.should == 2008
142
+ d.month.should == 2
143
+ d.day.should == 23
144
+ end
145
+
146
+ it "can parse a 'mmm DD YYYY' string into a Date object" do
147
+ d = Date.parse("feb#{@sep}23#{@sep}2008")
148
+ d.year.should == 2008
149
+ d.month.should == 2
150
+ d.day.should == 23
151
+
152
+ d = Date.parse("feb#{@sep}23,#{@sep}2008")
153
+ d.year.should == 2008
154
+ d.month.should == 2
155
+ d.day.should == 23
156
+
157
+ d = Date.parse("feb#{@sep}23,#{@sep}08")
158
+ d.year.should == 2008
159
+ d.month.should == 2
160
+ d.day.should == 23
161
+ end
162
+
163
+ it "can parse a 'YYYY mmm DD' string into a Date object" do
164
+ d = Date.parse("2008#{@sep}feb#{@sep}23")
165
+ d.year.should == 2008
166
+ d.month.should == 2
167
+ d.day.should == 23
168
+ end
169
+
170
+ it "can parse a month name and day into a Date object" do
171
+ Date.parse("november#{@sep}5th").should == Date.civil(Date.today.year, 11, 5)
172
+ end
173
+
174
+ it "can parse a weekday, month, day, and year into a Date object" do
175
+ Date.parse("Mon Aug 10 2009").should == Date.civil(2009, 8, 10)
176
+ end
177
+
178
+ it "can parse a month name, day and year into a Date object" do
179
+ Date.parse("november#{@sep}5th#{@sep}2005").should == Date.civil(2005, 11, 5)
180
+ end
181
+
182
+ it "can parse a year, month name and day into a Date object" do
183
+ Date.parse("2005#{@sep}november#{@sep}5th").should == Date.civil(2005, 11, 5)
184
+ end
185
+
186
+ it "can parse a year, day and month name into a Date object" do
187
+ Date.parse("5th#{@sep}november#{@sep}2005").should == Date.civil(2005, 11, 5)
188
+ end
189
+
190
+ it "can handle negative year numbers" do
191
+ Date.parse("5th#{@sep}november#{@sep}-2005").should == Date.civil(-2005, 11, 5)
192
+ end
193
+ end
194
+
195
+ describe :date_parse_us, :shared => true do
196
+ it "parses a YYYY#{@sep}MM#{@sep}DD string into a Date object" do
197
+ d = Date.parse("2007#{@sep}10#{@sep}01")
198
+ d.year.should == 2007
199
+ d.month.should == 10
200
+ d.day.should == 1
201
+ end
202
+
203
+ ruby_version_is "" ... "1.9" do
204
+ it "parses a MM#{@sep}DD#{@sep}YYYY string into a Date object" do
205
+ d = Date.parse("10#{@sep}01#{@sep}2007")
206
+ d.year.should == 2007
207
+ d.month.should == 10
208
+ d.day.should == 1
209
+ end
210
+
211
+ it "parses a MM#{@sep}DD#{@sep}YY string into a Date object using the year digits as 20XX" do
212
+ d = Date.parse("10#{@sep}01#{@sep}07")
213
+ d.year.should == 2007
214
+ d.month.should == 10
215
+ d.day.should == 1
216
+ end
217
+
218
+ it "parses a MM#{@sep}DD#{@sep}YY string into a Date object using the year digits as 20XX" do
219
+ d = Date.parse("10#{@sep}01#{@sep}07", true)
220
+ d.year.should == 2007
221
+ d.month.should == 10
222
+ d.day.should == 1
223
+ end
224
+
225
+ it "parses a MM#{@sep}DD#{@sep}YY string into a Date object NOT using the year digits as 20XX" do
226
+ d = Date.parse("10#{@sep}01#{@sep}07", false)
227
+ d.year.should == 7
228
+ d.month.should == 10
229
+ d.day.should == 1
230
+ end
231
+ end
232
+
233
+ ruby_version_is "1.9" do
234
+ it "parses a DD#{@sep}MM#{@sep}YYYY string into a Date object" do
235
+ d = Date.parse("10#{@sep}01#{@sep}2007")
236
+ d.year.should == 2007
237
+ d.month.should == 1
238
+ d.day.should == 10
239
+ end
240
+
241
+ it "parses a YY#{@sep}MM#{@sep}DD string into a Date object using the year digits as 20XX" do
242
+ d = Date.parse("10#{@sep}01#{@sep}07")
243
+ d.year.should == 2010
244
+ d.month.should == 1
245
+ d.day.should == 7
246
+ end
247
+
248
+ it "parses a YY#{@sep}MM#{@sep}DD string into a Date object NOT using the year digits as 20XX" do
249
+ d = Date.parse("10#{@sep}01#{@sep}07", false)
250
+ d.year.should == 10
251
+ d.month.should == 1
252
+ d.day.should == 7
253
+ end
254
+
255
+ it "parses a YY#{@sep}MM#{@sep}DD string into a Date object using the year digits as 20XX" do
256
+ d = Date.parse("10#{@sep}01#{@sep}07", true)
257
+ d.year.should == 2010
258
+ d.month.should == 1
259
+ d.day.should == 7
260
+ end
261
+ end
262
+ end
263
+
264
+ describe "Date#parse with '.' separator" do
265
+ before :all do
266
+ @sep = '.'
267
+ end
268
+
269
+ it_should_behave_like "date_parse"
270
+ end
271
+
272
+ describe "Date#parse with '/' separator" do
273
+ before :all do
274
+ @sep = '/'
275
+ end
276
+
277
+ it_should_behave_like "date_parse"
278
+ end
279
+
280
+ describe "Date#parse with ' ' separator" do
281
+ before :all do
282
+ @sep = ' '
283
+ end
284
+
285
+ it_should_behave_like "date_parse"
286
+ end
287
+
288
+ describe "Date#parse with '/' separator US-style" do
289
+ before :all do
290
+ @sep = '/'
291
+ end
292
+
293
+ it_should_behave_like "date_parse_us"
294
+ end
295
+
296
+ ruby_version_is "" ... "1.8.7" do
297
+ describe "Date#parse with '.' separator US-style" do
298
+ before :all do
299
+ @sep = '.'
300
+ end
301
+
302
+ it_should_behave_like "date_parse_us"
303
+ end
304
+ end
305
+
306
+ describe "Date#parse with '-' separator EU-style" do
307
+ # The - separator let's it work like European format, so it as a different spec
308
+ it "can parse a YYYY-MM-DD string into a Date object" do
309
+ d = Date.parse("2007-10-01")
310
+ d.year.should == 2007
311
+ d.month.should == 10
312
+ d.day.should == 1
313
+ end
314
+
315
+ it "can parse a DD-MM-YYYY string into a Date object" do
316
+ d = Date.parse("10-01-2007")
317
+ d.year.should == 2007
318
+ d.month.should == 1
319
+ d.day.should == 10
320
+ end
321
+
322
+ it "can parse a YY-MM-DD string into a Date object" do
323
+ d = Date.parse("10-01-07")
324
+ d.year.should == 2010
325
+ d.month.should == 1
326
+ d.day.should == 7
327
+ end
328
+
329
+ it "can parse a YY-DD-MM string into a Date object" do
330
+ d = Date.parse("10-01-07")
331
+ d.year.should == 2010
332
+ d.month.should == 1
333
+ d.day.should == 7
334
+ end
335
+
336
+ it "can parse a MM-DD-YY string into a Date object NOT using the year digits as 20XX" do
337
+ d = Date.parse("10-01-07", false)
338
+ d.year.should == 10
339
+ d.month.should == 1
340
+ d.day.should == 7
341
+ end
342
+
343
+ it "can parse a YY-MM-DD string into a Date object using the year digits as 20XX" do
344
+ d = Date.parse("10-01-07", true)
345
+ d.year.should == 2010
346
+ d.month.should == 1
347
+ d.day.should == 7
348
+ end
349
+ end
350
+
351
+ ruby_version_is "1.8.7" do
352
+ describe "Date#parse(.)" do
353
+ it "parses a YYYY.MM.DD string into a Date object" do
354
+ d = Date.parse("2007.10.01")
355
+ d.year.should == 2007
356
+ d.month.should == 10
357
+ d.day.should == 1
358
+ end
359
+
360
+ it "parses a DD.MM.YYYY string into a Date object" do
361
+ d = Date.parse("10.01.2007")
362
+ d.year.should == 2007
363
+ d.month.should == 1
364
+ d.day.should == 10
365
+ end
366
+
367
+ ruby_version_is "" ... "1.9" do
368
+ it "parses a YY.MM.DD string into a Date object" do
369
+ d = Date.parse("10.01.07")
370
+ d.year.should == 2010
371
+ d.month.should == 1
372
+ d.day.should == 7
373
+ end
374
+ end
375
+
376
+ ruby_version_is "1.9" do
377
+ it "parses a YY.MM.DD string into a Date object" do
378
+ d = Date.parse("10.01.07")
379
+ d.year.should == 2010
380
+ d.month.should == 1
381
+ d.day.should == 7
382
+ end
383
+ end
384
+
385
+ it "parses a YY.MM.DD string into a Date object using the year digits as 20XX" do
386
+ d = Date.parse("10.01.07", true)
387
+ d.year.should == 2010
388
+ d.month.should == 1
389
+ d.day.should == 7
390
+ end
391
+ end
392
+ end
393
+
394
+ describe "Date::Format::STYLE" do
395
+ before do
396
+ @style = Date::Format::STYLE.dup
397
+ end
398
+ after do
399
+ Date::Format::STYLE.replace(@style)
400
+ end
401
+
402
+ it "Can change DD/DD/DD parsing by modifying Format::STYLE[:slash]" do
403
+ Date::Format::STYLE[:slash] = :ymd
404
+ d = Date.parse("10/01/07")
405
+ d.year.should == 2010
406
+ d.month.should == 1
407
+ d.day.should == 7
408
+
409
+ Date::Format::STYLE[:slash] = :mdy
410
+ d = Date.parse("10/01/07")
411
+ d.year.should == 2007
412
+ d.month.should == 10
413
+ d.day.should == 1
414
+
415
+ Date::Format::STYLE[:slash] = :dmy
416
+ d = Date.parse("10/01/07")
417
+ d.year.should == 2007
418
+ d.month.should == 1
419
+ d.day.should == 10
420
+ end
421
+
422
+ it "Can change DD.DD.DD parsing by modifying Format::STYLE[:dot]" do
423
+ Date::Format::STYLE[:dot] = :ymd
424
+ d = Date.parse("10.01.07")
425
+ d.year.should == 2010
426
+ d.month.should == 1
427
+ d.day.should == 7
428
+
429
+ Date::Format::STYLE[:dot] = :mdy
430
+ d = Date.parse("10.01.07")
431
+ d.year.should == 2007
432
+ d.month.should == 10
433
+ d.day.should == 1
434
+
435
+ Date::Format::STYLE[:dot] = :dmy
436
+ d = Date.parse("10.01.07")
437
+ d.year.should == 2007
438
+ d.month.should == 1
439
+ d.day.should == 10
440
+ end
441
+ end
442
+