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,43 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'datetime_spec_helper'))
2
+
3
+ describe "DateTime#<=>" do
4
+
5
+ it "should be able to compare two same DateTimes" do
6
+ (DateTime.civil(2000, 04, 06) <=> DateTime.civil(2000, 04, 06)).should == 0
7
+ (DateTime.civil(2000, 04, 06, 10, 12, 11) <=> DateTime.civil(2000, 04, 06, 10, 12, 11)).should == 0
8
+ end
9
+
10
+ it "should be able to compute the difference between two DateTimes" do
11
+ (DateTime.civil(2000, 04, 05) <=> DateTime.civil(2000, 04, 06)).should == -1
12
+ (DateTime.civil(2001, 04, 05) <=> DateTime.civil(2000, 04, 06)).should == 1
13
+ (DateTime.civil(2000, 04, 05, 10, 12, 13, 1) <=> DateTime.civil(2000, 04, 05, 10, 12, 13, 2)).should == -1
14
+ (DateTime.civil(2001, 04, 05, 10, 12, 13, 1) <=> DateTime.civil(2001, 04, 05, 10, 12, 13, 0)).should == 1
15
+ end
16
+
17
+ it "should be able to compare to another numeric" do
18
+ (DateTime.civil(2000, 04, 05) <=> DateTime.civil(2000, 04, 06).jd).should == -1
19
+ (DateTime.civil(2001, 04, 05) <=> DateTime.civil(2000, 04, 06).jd).should == 1
20
+
21
+ (DateTime.civil(2000, 04, 05).jd <=> 2451640).should == 0
22
+ (DateTime.civil(2000, 04, 05) <=> 2451640.00000001).should == -1
23
+ (DateTime.civil(2000, 04, 05) <=> 2451639.99999999).should == 1
24
+ end
25
+
26
+ end
27
+
28
+ describe "DateTime#between?" do
29
+ it "should be true if the DateTime falls in between the two given DateTimes" do
30
+ (DateTime.civil(2000, 04, 06).between?(DateTime.civil(2000, 04, 05), DateTime.civil(2000, 04, 07))).should == true
31
+ (DateTime.civil(2000, 04, 06).between?(DateTime.civil(2000, 04, 06), DateTime.civil(2000, 04, 07))).should == true
32
+ (DateTime.civil(2000, 04, 06).between?(DateTime.civil(2000, 04, 05), DateTime.civil(2000, 04, 06))).should == true
33
+
34
+ (DateTime.civil(2000, 04, 06, 10, 12, 11, 13).between?(DateTime.civil(2000, 04, 06, 10, 12, 11, 12), DateTime.civil(2000, 04, 06, 10, 12, 11, 14))).should == true
35
+ (DateTime.civil(2000, 04, 06, 10, 12, 11, 12).between?(DateTime.civil(2000, 04, 06, 10, 12, 11, 12), DateTime.civil(2000, 04, 06, 10, 12, 11, 14))).should == true
36
+ (DateTime.civil(2000, 04, 06, 10, 12, 11, 14).between?(DateTime.civil(2000, 04, 06, 10, 12, 11, 12), DateTime.civil(2000, 04, 06, 10, 12, 11, 14))).should == true
37
+ end
38
+
39
+ it "should be false if the DateTime does not fall in between the two given DateTimes" do
40
+ (DateTime.civil(2000, 04, 05).between?(DateTime.civil(2000, 04, 06), DateTime.civil(2000, 04, 07))).should == false
41
+ (DateTime.civil(2000, 04, 06, 10, 12, 11, 14).between?(DateTime.civil(2000, 04, 06, 10, 12, 11, 15), DateTime.civil(2000, 04, 06, 10, 12, 11, 16))).should == false
42
+ end
43
+ end
@@ -0,0 +1,58 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'datetime_spec_helper'))
2
+
3
+ describe "DateTime constructors" do
4
+ it ".civil creates a datetime with arguments" do
5
+ d = DateTime.civil(2000, 3, 5, 6, 7, 8, 9, 10)
6
+ d.year.should == 2000
7
+ d.month.should == 3
8
+ d.day.should == 5
9
+ d.hour.should == 6
10
+ d.min.should == 7
11
+ d.sec.should == 8
12
+ d.usec.should == 9
13
+ d.offset.should == 10
14
+ end
15
+
16
+ it ".commercial creates a datetime with arguments" do
17
+ d = DateTime.commercial(2000, 3, 5, 6, 7, 8, 9, 10)
18
+ d.cwyear.should == 2000
19
+ d.cweek.should == 3
20
+ d.cwday.should == 5
21
+ d.hour.should == 6
22
+ d.min.should == 7
23
+ d.sec.should == 8
24
+ d.usec.should == 9
25
+ d.offset.should == 10
26
+ end
27
+
28
+ it ".jd creates a datetime with arguments" do
29
+ d = DateTime.jd(2000, 6, 7, 8, 9, 10)
30
+ d.jd.should == 2000
31
+ d.hour.should == 6
32
+ d.min.should == 7
33
+ d.sec.should == 8
34
+ d.usec.should == 9
35
+ d.offset.should == 10
36
+ end
37
+
38
+ it ".jd_fract creates a datetime with arguments" do
39
+ d = DateTime.jd_fract(2000, 0.5, 10)
40
+ d.jd.should == 2000
41
+ d.hour.should == 12
42
+ d.min.should == 0
43
+ d.sec.should == 0
44
+ d.usec.should == 0
45
+ d.offset.should == 10
46
+ end
47
+
48
+ it ".ordinal creates a datetime with arguments" do
49
+ d = DateTime.ordinal(2000, 100, 6, 7, 8, 9, 10)
50
+ d.year.should == 2000
51
+ d.yday.should == 100
52
+ d.hour.should == 6
53
+ d.min.should == 7
54
+ d.sec.should == 8
55
+ d.usec.should == 9
56
+ d.offset.should == 10
57
+ end
58
+ end
@@ -0,0 +1,11 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'datetime_spec_helper'))
2
+
3
+ describe "DateTime#eql?" do
4
+ it "should be able determine equality between date objects" do
5
+ DateTime.civil(2007, 10, 11).should eql(DateTime.civil(2007, 10, 11))
6
+ DateTime.civil(2007, 10, 11, 10, 11, 12, 13).should eql(DateTime.civil(2007, 10, 11, 10, 11, 12, 13))
7
+ DateTime.civil(2007, 10, 11, 10, 11, 12, 13).usec.should eql((DateTime.civil(2007, 10, 12, 10, 11, 12, 13) - 1).usec)
8
+ DateTime.civil(2007, 10, 11, 10, 11, 12, 13).should eql(DateTime.civil(2007, 10, 12, 10, 11, 12, 13) - 1)
9
+ DateTime.civil(2007, 10, 11, 10, 11, 12, 13).should_not eql(DateTime.civil(2007, 10, 11, 10, 11, 12, 12))
10
+ end
11
+ end
@@ -0,0 +1,65 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'datetime_spec_helper'))
2
+
3
+ describe "DateTimeTime#-" do
4
+
5
+ it "should substract a number of days from a DateTime" do
6
+ (DateTime.civil(2008, 1, 8) - 315).should == DateTime.civil(2007,2,27)
7
+ (DateTime.commercial(2007, 47, 2) - 315).should == DateTime.commercial(2007,2,2)
8
+ (DateTime.jd(2455097) - 315).should == DateTime.jd(2454782)
9
+ (DateTime.ordinal(2008, 325) - 315).should == DateTime.ordinal(2008, 10)
10
+ end
11
+
12
+ it "should subtract a fractional number of days to a Date" do
13
+ (DateTime.civil(2008, 1, 8, 12) - 315.5).should == DateTime.civil(2007,2,27)
14
+ (DateTime.commercial(2007, 47, 2, 18) - 315.75).should == DateTime.commercial(2007,2,2)
15
+ (DateTime.jd(2455097, 6) - 315.25).should == DateTime.jd(2454782)
16
+ (DateTime.ordinal(2008, 325, 6) - 315.25).should == DateTime.ordinal(2008, 10)
17
+ end
18
+
19
+ it "should substract a negative number of days from a DateTime" do
20
+ d = DateTime.civil(2007, 4, 19).-(-13)
21
+ d.should == DateTime.civil(2007, 5 ,2)
22
+ end
23
+
24
+ it "should subtract a fractional negative number of days to a Date" do
25
+ d = DateTime.civil(2007, 2, 16, 12).-(-10.5)
26
+ d.should == DateTime.civil(2007,2,27)
27
+ end
28
+
29
+ it "should be able to compute the different between two DateTimes" do
30
+ (DateTime.civil(2007,2,27) - DateTime.civil(2007,2,27)).should be_close(0.0, 0.00000001)
31
+ (DateTime.civil(2007,2,27) - DateTime.civil(2007,2,26)).should be_close(1.0, 0.00000001)
32
+ (DateTime.civil(2006,2,27) - DateTime.civil(2007,2,27)).should be_close(-365.0, 0.00000001)
33
+ (DateTime.civil(2008,2,27) - DateTime.civil(2007,2,27)).should be_close(365.0, 0.00000001)
34
+ (DateTime.civil(2009,2,27) - DateTime.civil(2008,2,27)).should be_close(366.0, 0.00000001)
35
+
36
+ (DateTime.civil(2009,2,27) - DateTime.commercial(2008,2,1)).should be_close(417.0, 0.00000001)
37
+ (DateTime.civil(2009,2,27) - DateTime.jd(2454782)).should be_close(108.0, 0.00000001)
38
+ (DateTime.civil(2009,2,27) - DateTime.ordinal(2008, 10)).should be_close(414.0, 0.00000001)
39
+
40
+ (DateTime.commercial(2008,2,1) - DateTime.civil(2008,2,27)).should be_close(-51.0, 0.00000001)
41
+ (DateTime.commercial(2008,2,1) - DateTime.jd(2454782)).should be_close(-309.0, 0.00000001)
42
+ (DateTime.commercial(2008,2,1) - DateTime.ordinal(2008, 10)).should be_close(-3.0, 0.00000001)
43
+
44
+ (DateTime.jd(2454782) - DateTime.commercial(2008,2,1)).should be_close(309.0, 0.00000001)
45
+ (DateTime.jd(2454782) - DateTime.civil(2009,2,27)).should be_close(-108.0, 0.00000001)
46
+ (DateTime.jd(2454782) - DateTime.ordinal(2008, 10)).should be_close(306.0, 0.00000001)
47
+
48
+ (DateTime.ordinal(2008, 10) - DateTime.commercial(2008,2,1)).should be_close(3.0, 0.00000001)
49
+ (DateTime.ordinal(2008, 10) - DateTime.jd(2454782)).should be_close(-306.0, 0.00000001)
50
+ (DateTime.ordinal(2008, 10) - DateTime.civil(2009,2,27)).should be_close(-414.0, 0.00000001)
51
+ end
52
+
53
+ it "should consider the offset when computing the different between two DateTimes" do
54
+ (DateTime.civil(2007,2,27, 0, 0, 0, 0, 43200) - DateTime.civil(2007,2,27,0,0,0,0)).should be_close(0.5, 0.00000001)
55
+ (DateTime.civil(2007,2,27, 0, 0, 0, 0, 43200) - DateTime.civil(2007,2,27,12,0,0,0)).should be_close(0.0, 0.00000001)
56
+ (DateTime.civil(2007,2,27,0,0,0,0,-43200) - DateTime.civil(2007,2,27,0,0,0,0,43200)).should be_close(-1.0, 0.00000001)
57
+ end
58
+
59
+ it "should raise an error on non numeric parameters" do
60
+ lambda { DateTime.civil(2007,2,27) - :hello }.should raise_error(TypeError)
61
+ lambda { DateTime.civil(2007,2,27) - "hello" }.should raise_error(TypeError)
62
+ lambda { DateTime.civil(2007,2,27) - Object.new }.should raise_error(TypeError)
63
+ end
64
+
65
+ end
@@ -0,0 +1,14 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'datetime_spec_helper'))
2
+
3
+ describe "DateTime.now" do
4
+ it "should be right now as a DateTime" do
5
+ t = Time.now
6
+ d = DateTime.now
7
+ d.year.should == t.year
8
+ d.mon.should == t.mon
9
+ d.day.should == t.day
10
+ d.hour.should == t.hour
11
+ d.min.should == t.min
12
+ d.sec.should == t.sec
13
+ end
14
+ end
@@ -0,0 +1,338 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'datetime_spec_helper'))
2
+
3
+ describe "DateTime#parse" do
4
+ it "can't handle a empty string" do
5
+ lambda{ DateTime.parse("") }.should raise_error(ArgumentError)
6
+ end
7
+
8
+ # Specs using numbers
9
+ it "can't handle a single digit" do
10
+ lambda{ DateTime.parse("1") }.should raise_error(ArgumentError)
11
+ end
12
+
13
+ it "can handle many different types of time values" do
14
+ DateTime.parse("01:02:03").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3)
15
+ DateTime.parse("01:02:03a").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3)
16
+ DateTime.parse(" 1:02:03a").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3)
17
+ DateTime.parse("1:02:03a").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3)
18
+ DateTime.parse("01:02:03am").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3)
19
+ DateTime.parse("01:02:03p").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 13, 2, 3)
20
+ DateTime.parse("01:02:03pm").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 13, 2, 3)
21
+ DateTime.parse("12:02:03am").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 0, 2, 3)
22
+ DateTime.parse("12:02:03p").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 12, 2, 3)
23
+ proc{DateTime.parse("13:02:03p")}.should raise_error(ArgumentError)
24
+ proc{DateTime.parse("00:02:03p")}.should raise_error(ArgumentError)
25
+ proc{DateTime.parse("00:02:03r")}.should raise_error(ArgumentError)
26
+ end
27
+
28
+ it "should use the current time offset if no time offset is specified" do
29
+ DateTime.parse("01:02:03").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3, 0, Time.now.utc_offset)
30
+ DateTime.parse("01:02:03Z").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3, 0, 0)
31
+ DateTime.parse("01:02:03+0100").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3, 0, 3600)
32
+ DateTime.parse("01:02:03-01:00").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3, 0, -3600)
33
+ DateTime.parse("01:02:03+01").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3, 0, 3600)
34
+ DateTime.parse("01:02:03-01").should == DateTime.civil(DateTime.today.year, DateTime.today.month, DateTime.today.day, 1, 2, 3, 0, -3600)
35
+ end
36
+
37
+ it "can handle DD as month day number" do
38
+ DateTime.parse("10").should == DateTime.civil(DateTime.today.year, DateTime.today.month, 10)
39
+ DateTime.parse("10 01:02:03").should == DateTime.civil(DateTime.today.year, DateTime.today.month, 10, 1, 2, 3)
40
+ end
41
+
42
+ it "can handle DDD as year day number" do
43
+ DateTime.parse("050").should == DateTime.civil(DateTime.today.year, 2, 19)
44
+ DateTime.parse("050 1:02:03").should == DateTime.civil(DateTime.today.year, 2, 19, 1, 2, 3)
45
+ end
46
+
47
+ it "can handle MMDD as month and day" do
48
+ DateTime.parse("1108").should == DateTime.civil(DateTime.today.year, 11, 8)
49
+ DateTime.parse("1108 10:02:03").should == DateTime.civil(DateTime.today.year, 11, 8, 10, 2, 3)
50
+ end
51
+
52
+ it "can handle YYDDD as year and day number" do
53
+ DateTime.parse("10100").should == DateTime.civil(2010, 4, 10)
54
+ DateTime.parse("10100 23:02:03").should == DateTime.civil(2010, 4, 10, 23, 2, 3)
55
+ end
56
+
57
+ it "can handle YYMMDD as year month and day" do
58
+ DateTime.parse("201023").should == DateTime.civil(2020, 10, 23)
59
+ DateTime.parse("201023 23:02:03 +0800").should == DateTime.civil(2020, 10, 23, 23, 2, 3, 0, 28800)
60
+ end
61
+
62
+ it "can handle YYYYDDD as year and day number" do
63
+ DateTime.parse("1910100").should == DateTime.civil(1910, 4, 10)
64
+ DateTime.parse("1910100 23:02:03 -0101").should == DateTime.civil(1910, 4, 10, 23, 2, 3, 0, -3660)
65
+ end
66
+
67
+ it "can handle YYYYMMDD as year and day number" do
68
+ DateTime.parse("19101101").should == DateTime.civil(1910, 11, 1)
69
+ DateTime.parse("19101101T23:02:03 +0000").should == DateTime.civil(1910, 11, 1, 23, 2, 3)
70
+ end
71
+ end
72
+
73
+ describe :date_parse, :shared => true do
74
+ it "can parse a mmm-YYYY string into a DateTime object" do
75
+ d = DateTime.parse("feb#{@sep}2008")
76
+ d.year.should == 2008
77
+ d.month.should == 2
78
+ d.day.should == 1
79
+
80
+ d = DateTime.parse("feb#{@sep}2008 1:02:03")
81
+ d.year.should == 2008
82
+ d.month.should == 2
83
+ d.day.should == 1
84
+ d.hour.should == 1
85
+ d.min.should == 2
86
+ d.sec.should == 3
87
+ end
88
+
89
+ it "can parse a 'DD mmm YYYY' string into a DateTime object" do
90
+ d = DateTime.parse("23#{@sep}feb#{@sep}2008")
91
+ d.year.should == 2008
92
+ d.month.should == 2
93
+ d.day.should == 23
94
+
95
+ d = DateTime.parse("23#{@sep}feb#{@sep}2008 11:02:03")
96
+ d.year.should == 2008
97
+ d.month.should == 2
98
+ d.day.should == 23
99
+ d.hour.should == 11
100
+ d.min.should == 2
101
+ d.sec.should == 3
102
+ end
103
+
104
+ it "can parse a 'mmm DD YYYY' string into a DateTime object" do
105
+ d = DateTime.parse("feb#{@sep}23#{@sep}2008")
106
+ d.year.should == 2008
107
+ d.month.should == 2
108
+ d.day.should == 23
109
+
110
+ d = DateTime.parse("feb#{@sep}23#{@sep}2008 01:02:03")
111
+ d.year.should == 2008
112
+ d.month.should == 2
113
+ d.day.should == 23
114
+ d.hour.should == 1
115
+ d.min.should == 2
116
+ d.sec.should == 3
117
+ end
118
+
119
+ it "can parse a 'YYYY mmm DD' string into a DateTime object" do
120
+ d = DateTime.parse("2008#{@sep}feb#{@sep}23")
121
+ d.year.should == 2008
122
+ d.month.should == 2
123
+ d.day.should == 23
124
+
125
+ d = DateTime.parse("2008#{@sep}feb#{@sep}23 01:02")
126
+ d.year.should == 2008
127
+ d.month.should == 2
128
+ d.day.should == 23
129
+ d.hour.should == 1
130
+ d.min.should == 2
131
+ d.sec.should == 0
132
+ end
133
+
134
+ it "can parse a month name and day into a Date object" do
135
+ DateTime.parse("november#{@sep}5th").should == DateTime.civil(Date.today.year, 11, 5)
136
+ DateTime.parse("november#{@sep}5th 1:02").should == DateTime.civil(Date.today.year, 11, 5, 1, 2)
137
+ end
138
+
139
+ it "can parse a month name, day and year into a Date object" do
140
+ DateTime.parse("november#{@sep}5th#{@sep}2005").should == DateTime.civil(2005, 11, 5)
141
+ DateTime.parse("november#{@sep}5th#{@sep}2005 1:02").should == DateTime.civil(2005, 11, 5, 1, 2)
142
+ end
143
+
144
+ it "can parse a year, month name and day into a Date object" do
145
+ DateTime.parse("2005#{@sep}november#{@sep}5th").should == DateTime.civil(2005, 11, 5)
146
+ DateTime.parse("2005#{@sep}november#{@sep}5th 01:02 +0100").should == DateTime.civil(2005, 11, 5, 1, 2, 0, 0, 3600)
147
+ end
148
+
149
+ it "can parse a year, day and month name into a Date object" do
150
+ DateTime.parse("5th#{@sep}november#{@sep}2005").should == DateTime.civil(2005, 11, 5)
151
+ DateTime.parse("5th#{@sep}november#{@sep}2005 1:02 -0100").should == DateTime.civil(2005, 11, 5, 1, 2, 0, 0, -3600)
152
+ end
153
+
154
+ it "can handle negative year numbers" do
155
+ DateTime.parse("5th#{@sep}november#{@sep}-2005").should == DateTime.civil(-2005, 11, 5)
156
+ DateTime.parse("5th#{@sep}november#{@sep}-2005 1:02 -0100").should == DateTime.civil(-2005, 11, 5, 1, 2, 0, 0, -3600)
157
+ end
158
+ end
159
+
160
+ describe :date_parse_us, :shared => true do
161
+ it "parses a YYYY#{@sep}MM#{@sep}DD string into a DateTime object" do
162
+ d = DateTime.parse("2007#{@sep}10#{@sep}01")
163
+ d.year.should == 2007
164
+ d.month.should == 10
165
+ d.day.should == 1
166
+
167
+ d = DateTime.parse("2007#{@sep}10#{@sep}01 01:02:03")
168
+ d.year.should == 2007
169
+ d.month.should == 10
170
+ d.day.should == 1
171
+ d.hour.should == 1
172
+ d.min.should == 2
173
+ d.sec.should == 3
174
+ end
175
+
176
+ it "parses a MM#{@sep}DD#{@sep}YYYY string into a DateTime object" do
177
+ d = DateTime.parse("10#{@sep}01#{@sep}2007")
178
+ d.year.should == 2007
179
+ d.month.should == 10
180
+ d.day.should == 1
181
+
182
+
183
+ d = DateTime.parse("10#{@sep}01#{@sep}2007 01:02:03")
184
+ d.year.should == 2007
185
+ d.month.should == 10
186
+ d.day.should == 1
187
+ d.hour.should == 1
188
+ d.min.should == 2
189
+ d.sec.should == 3
190
+ end
191
+
192
+ it "parses a MM#{@sep}DD#{@sep}YY string into a DateTime object using the year digits as 20XX" do
193
+ d = DateTime.parse("10#{@sep}01#{@sep}07")
194
+ d.year.should == 2007
195
+ d.month.should == 10
196
+ d.day.should == 1
197
+
198
+ d = DateTime.parse("10#{@sep}01#{@sep}97 01:02:03 Z")
199
+ d.year.should == 1997
200
+ d.month.should == 10
201
+ d.day.should == 1
202
+ d.hour.should == 1
203
+ d.min.should == 2
204
+ d.sec.should == 3
205
+ end
206
+ end
207
+
208
+ describe :date_parse_eu, :shared => true do
209
+ before do
210
+ DateTime.use_parsers(:iso, :eu)
211
+ end
212
+ after do
213
+ DateTime.reset_parsers!
214
+ end
215
+
216
+ # The - separator let's it work like European format, so it as a different spec
217
+ it "can parse a YYYY-MM-DD string into a DateTime object" do
218
+ d = DateTime.parse("2007#{@sep}10#{@sep}01")
219
+ d.year.should == 2007
220
+ d.month.should == 10
221
+ d.day.should == 1
222
+
223
+ d = DateTime.parse("2007#{@sep}10#{@sep}01 01:02:03Z")
224
+ d.year.should == 2007
225
+ d.month.should == 10
226
+ d.day.should == 1
227
+ d.hour.should == 1
228
+ d.min.should == 2
229
+ d.sec.should == 3
230
+ end
231
+
232
+ it "can parse a DD-MM-YYYY string into a DateTime object" do
233
+ d = DateTime.parse("10#{@sep}01#{@sep}2007")
234
+ d.year.should == 2007
235
+ d.month.should == 1
236
+ d.day.should == 10
237
+
238
+ d = DateTime.parse("10#{@sep}01#{@sep}2007 01:02:03-01:00")
239
+ d.year.should == 2007
240
+ d.month.should == 1
241
+ d.day.should == 10
242
+ d.hour.should == 1
243
+ d.min.should == 2
244
+ d.sec.should == 3
245
+ d.offset.should == -3600
246
+ end
247
+
248
+ it "can parse a YY-MM-DD string into a DateTime object" do
249
+ d = DateTime.parse("10#{@sep}01#{@sep}07")
250
+ d.year.should == 2010
251
+ d.month.should == 1
252
+ d.day.should == 7
253
+
254
+ d = DateTime.parse("97#{@sep}01#{@sep}07 01:02:03+01:00")
255
+ d.year.should == 1997
256
+ d.month.should == 1
257
+ d.day.should == 7
258
+ d.hour.should == 1
259
+ d.min.should == 2
260
+ d.sec.should == 3
261
+ d.offset.should == 3600
262
+ end
263
+ end
264
+
265
+
266
+ describe "DateTime#parse with '.' separator" do
267
+ before :all do
268
+ @sep = '.'
269
+ end
270
+
271
+ it_should_behave_like "date_parse"
272
+ end
273
+
274
+ describe "DateTime#parse with '/' separator" do
275
+ before :all do
276
+ @sep = '/'
277
+ end
278
+
279
+ it_should_behave_like "date_parse"
280
+ end
281
+
282
+ describe "DateTime#parse with ' ' separator" do
283
+ before :all do
284
+ @sep = ' '
285
+ end
286
+
287
+ it_should_behave_like "date_parse"
288
+ end
289
+
290
+ describe "DateTime#parse with '/' separator US-style" do
291
+ before :all do
292
+ @sep = '/'
293
+ end
294
+
295
+ it_should_behave_like "date_parse_us"
296
+ end
297
+
298
+ ruby_version_is "" ... "1.8.7" do
299
+ describe "DateTime#parse with '.' separator US-style" do
300
+ before :all do
301
+ @sep = '.'
302
+ end
303
+
304
+ it_should_behave_like "date_parse_us"
305
+ end
306
+ end
307
+
308
+ describe "DateTime#parse with '-' separator EU-style" do
309
+ before :all do
310
+ @sep = '-'
311
+ end
312
+
313
+ it_should_behave_like "date_parse_eu"
314
+ end
315
+
316
+ describe "DateTime parser modifications" do
317
+ after do
318
+ DateTime.reset_parsers!
319
+ end
320
+
321
+ it "should be able to add a parser to an existing parser type that takes precedence" do
322
+ d = DateTime.now
323
+ proc{DateTime.parse("today")}.should raise_error(ArgumentError)
324
+ DateTime.add_parser(:iso, /\Anow\z/){{:civil=>[d.year, d.mon, d.day], :parts=>[d.hour, d.min, d.sec, d.usec], :offset=>d.offset}}
325
+ DateTime.parse("now").should == d
326
+ end
327
+
328
+ it "should be able to add new parser types" do
329
+ proc{DateTime.parse("today")}.should raise_error(ArgumentError)
330
+ DateTime.add_parser_type(:mine)
331
+ d = DateTime.now
332
+ DateTime.add_parser(:mine, /\Anow\z/){{:civil=>[d.year, d.mon, d.day], :parts=>[d.hour, d.min, d.sec, d.usec], :offset=>d.offset}}
333
+ proc{DateTime.parse("now")}.should raise_error(ArgumentError)
334
+ DateTime.parse("now", :parser_types=>[:mine]).should == d
335
+ DateTime.use_parsers(:mine)
336
+ DateTime.parse("now").should == d
337
+ end
338
+ end
@@ -0,0 +1,102 @@
1
+ require(File.join(File.dirname(__FILE__), '..', 'datetime_spec_helper'))
2
+
3
+ describe "DateTime#strftime" do
4
+
5
+ it "should be able to print the date time" do
6
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime.should == "2000-04-06T10:11:12+00:00"
7
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime.should == DateTime.civil(2000, 4, 6, 10, 11, 12).to_s
8
+ end
9
+
10
+ it "should be able to print the hour in a 24 hour clock with leading zero" do
11
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%H').should == "10"
12
+ DateTime.civil(2000, 4, 6, 13, 11, 12).strftime('%H').should == "13"
13
+ end
14
+
15
+ it "should be able to print the hour in a 12 hour clock with leading zero" do
16
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%I').should == "10"
17
+ DateTime.civil(2000, 4, 6, 13, 11, 12).strftime('%I').should == "01"
18
+ end
19
+
20
+ it "should be able to print the hour in a 24 hour clock with leading space" do
21
+ DateTime.civil(2000, 4, 6, 9, 11, 12).strftime('%k').should == " 9"
22
+ DateTime.civil(2000, 4, 6, 13, 11, 12).strftime('%k').should == "13"
23
+ end
24
+
25
+ it "should be able to print the hour in a 12 hour clock with leading space" do
26
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%l').should == "10"
27
+ DateTime.civil(2000, 4, 6, 13, 11, 12).strftime('%l').should == " 1"
28
+ end
29
+
30
+ it "should be able to print the minute with leading zero" do
31
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%M').should == "11"
32
+ DateTime.civil(2000, 4, 6, 10, 14, 12).strftime('%M').should == "14"
33
+ end
34
+
35
+ it "should be able to print the meridian indicator in lower case" do
36
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%P').should == "am"
37
+ DateTime.civil(2000, 4, 6, 13, 11, 12).strftime('%P').should == "pm"
38
+ end
39
+
40
+ it "should be able to print the meridian indicator in upper case" do
41
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%p').should == "AM"
42
+ DateTime.civil(2000, 4, 6, 13, 11, 12).strftime('%p').should == "PM"
43
+ end
44
+
45
+ it "should be able to print the second with leading zero" do
46
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%S').should == "12"
47
+ DateTime.civil(2000, 4, 6, 10, 11, 13).strftime('%S').should == "13"
48
+ end
49
+
50
+ it "should be able to print the number of seconds since the unix epoch" do
51
+ DateTime.civil(2008, 11, 12, 14, 3, 30, 0, -28800).strftime('%s').should == "1226527410"
52
+ DateTime.civil(2008, 11, 12, 14, 3, 31, 0, -28800).strftime('%s').should == "1226527411"
53
+ end
54
+
55
+ it "should be able to print the time zone offset as a Z if the offset is zero" do
56
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%Z').should == "Z"
57
+ DateTime.civil(2000, 4, 6, 10, 11, 12, 0, -43200).strftime('%Z').should == "-12:00"
58
+ end
59
+
60
+ it "should be able to print the time zone offset as a string of hours and minutes" do
61
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%z').should == "+00:00"
62
+ DateTime.civil(2000, 4, 6, 10, 11, 12, 0, -43200).strftime('%z').should == "-12:00"
63
+ DateTime.civil(2000, 4, 6, 10, 11, 12, 0, 43200).strftime('%z').should == "+12:00"
64
+ DateTime.civil(2000, 4, 6, 10, 11, 12, 0, -3600).strftime('%z').should == "-01:00"
65
+ DateTime.civil(2000, 4, 6, 10, 11, 12, 0, 3600).strftime('%z').should == "+01:00"
66
+ end
67
+
68
+ ############################
69
+ # Specs that combine stuff #
70
+ ############################
71
+
72
+ it "should be able to print the common date" do
73
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime("%c").should == "Thu Apr 6 10:11:12 2000"
74
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime("%c").should == DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%a %b %e %H:%M:%S %Y')
75
+ end
76
+
77
+ it "should be able to print the hour and minute" do
78
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime("%R").should == "10:11"
79
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime("%R").should == DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%H:%M')
80
+ end
81
+
82
+ it "should be able to show the hour, minute, second, and am/pm flag" do
83
+ DateTime.civil(2000, 4, 9, 10, 11, 12).strftime("%r").should == "10:11:12 AM"
84
+ DateTime.civil(2000, 4, 9, 13, 11, 12).strftime("%r").should == "01:11:12 PM"
85
+ DateTime.civil(2000, 4, 9, 10, 11, 12).strftime("%r").should == DateTime.civil(2000, 4, 9, 10, 11, 12).strftime('%I:%M:%S %p')
86
+ end
87
+
88
+ it "should be able to show the hour, minute, and second" do
89
+ DateTime.civil(2000, 4, 9, 10, 11, 12).strftime("%T").should == "10:11:12"
90
+ DateTime.civil(2000, 4, 9, 13, 11, 12).strftime("%T").should == "13:11:12"
91
+ DateTime.civil(2000, 4, 9, 10, 11, 12).strftime("%T").should == DateTime.civil(2000, 4, 9, 10, 11, 12).strftime('%H:%M:%S')
92
+ DateTime.civil(2000, 4, 9, 10, 11, 12).strftime("%X").should == "10:11:12"
93
+ DateTime.civil(2000, 4, 9, 13, 11, 12).strftime("%X").should == "13:11:12"
94
+ DateTime.civil(2000, 4, 9, 10, 11, 12).strftime("%X").should == DateTime.civil(2000, 4, 9, 10, 11, 12).strftime('%H:%M:%S')
95
+ end
96
+
97
+ it "should be able to print the common date and timezone" do
98
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime("%+").should == "Thu Apr 6 10:11:12 +00:00 2000"
99
+ DateTime.civil(2000, 4, 6, 10, 11, 12, 0, 43200).strftime("%+").should == "Thu Apr 6 10:11:12 +12:00 2000"
100
+ DateTime.civil(2000, 4, 6, 10, 11, 12).strftime("%+").should == DateTime.civil(2000, 4, 6, 10, 11, 12).strftime('%a %b %e %H:%M:%S %z %Y')
101
+ end
102
+ end