smart_month 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 (90) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +33 -0
  3. data/Rakefile +51 -0
  4. data/VERSION +1 -0
  5. data/lib/month.rb +26 -0
  6. data/lib/smart_month.rb +11 -0
  7. data/lib/smart_month/calculations.rb +87 -0
  8. data/lib/smart_month/collection.rb +51 -0
  9. data/lib/smart_month/extensions/date.rb +19 -0
  10. data/lib/smart_month/magic.rb +71 -0
  11. data/lib/smart_month/math.rb +41 -0
  12. data/lib/smart_month/rulesets.rb +151 -0
  13. data/lib/smart_month/util.rb +37 -0
  14. data/pkg/smart_month-1.0.0.gem +0 -0
  15. data/rdoc/classes/Date.html +209 -0
  16. data/rdoc/classes/Month.html +189 -0
  17. data/rdoc/classes/SmartMonth.html +136 -0
  18. data/rdoc/classes/SmartMonth/Calculations.html +383 -0
  19. data/rdoc/classes/SmartMonth/Collection.html +306 -0
  20. data/rdoc/classes/SmartMonth/Magic.html +214 -0
  21. data/rdoc/classes/SmartMonth/Magic/MonthFactory.html +178 -0
  22. data/rdoc/classes/SmartMonth/Math.html +311 -0
  23. data/rdoc/classes/SmartMonth/Rulesets.html +419 -0
  24. data/rdoc/classes/SmartMonth/Util.html +302 -0
  25. data/rdoc/classes/Time.html +152 -0
  26. data/rdoc/created.rid +1 -0
  27. data/rdoc/files/README.html +170 -0
  28. data/rdoc/files/lib/month_rb.html +113 -0
  29. data/rdoc/files/lib/smart_month/calculations_rb.html +101 -0
  30. data/rdoc/files/lib/smart_month/collection_rb.html +101 -0
  31. data/rdoc/files/lib/smart_month/extensions/date_rb.html +108 -0
  32. data/rdoc/files/lib/smart_month/magic_rb.html +101 -0
  33. data/rdoc/files/lib/smart_month/math_rb.html +101 -0
  34. data/rdoc/files/lib/smart_month/rulesets_rb.html +108 -0
  35. data/rdoc/files/lib/smart_month/util_rb.html +101 -0
  36. data/rdoc/files/lib/smart_month_rb.html +108 -0
  37. data/rdoc/fr_class_index.html +37 -0
  38. data/rdoc/fr_file_index.html +36 -0
  39. data/rdoc/fr_method_index.html +74 -0
  40. data/rdoc/index.html +24 -0
  41. data/rdoc/rdoc-style.css +208 -0
  42. data/smart_month.gemspec +179 -0
  43. data/test/spec/date/accessor_spec.rb +91 -0
  44. data/test/spec/date/add_month_spec.rb +24 -0
  45. data/test/spec/date/add_spec.rb +23 -0
  46. data/test/spec/date/boat_spec.rb +20 -0
  47. data/test/spec/date/civil_spec.rb +28 -0
  48. data/test/spec/date/commercial_spec.rb +29 -0
  49. data/test/spec/date/constants_spec.rb +41 -0
  50. data/test/spec/date/conversions_spec.rb +153 -0
  51. data/test/spec/date/downto_spec.rb +18 -0
  52. data/test/spec/date/eql_spec.rb +10 -0
  53. data/test/spec/date/gregorian_spec.rb +28 -0
  54. data/test/spec/date/hash_spec.rb +14 -0
  55. data/test/spec/date/infinity_spec.rb +77 -0
  56. data/test/spec/date/julian_spec.rb +52 -0
  57. data/test/spec/date/minus_month_spec.rb +23 -0
  58. data/test/spec/date/minus_spec.rb +30 -0
  59. data/test/spec/date/new_spec.rb +9 -0
  60. data/test/spec/date/neww_spec.rb +9 -0
  61. data/test/spec/date/ordinal_spec.rb +29 -0
  62. data/test/spec/date/parse_spec.rb +149 -0
  63. data/test/spec/date/relationship_spec.rb +20 -0
  64. data/test/spec/date/shared/civil.rb +66 -0
  65. data/test/spec/date/shared/commercial.rb +39 -0
  66. data/test/spec/date/shared/parse.rb +54 -0
  67. data/test/spec/date/shared/parse_eu.rb +30 -0
  68. data/test/spec/date/shared/parse_us.rb +29 -0
  69. data/test/spec/date/step_spec.rb +56 -0
  70. data/test/spec/date/strftime_spec.rb +205 -0
  71. data/test/spec/date/strptime_spec.rb +143 -0
  72. data/test/spec/date/upto_spec.rb +18 -0
  73. data/test/spec/parsedate/parsedate.rb +95 -0
  74. data/test/spec/time/httpdate_spec.rb +21 -0
  75. data/test/spec/time/iso8601_spec.rb +7 -0
  76. data/test/spec/time/rfc2822_spec.rb +7 -0
  77. data/test/spec/time/rfc822_spec.rb +7 -0
  78. data/test/spec/time/shared/rfc2822.rb +65 -0
  79. data/test/spec/time/shared/xmlschema.rb +53 -0
  80. data/test/spec/time/xmlschema_spec.rb +7 -0
  81. data/test/spec_helper.rb +56 -0
  82. data/test/test_helper.rb +4 -0
  83. data/test/unit/calculations_test.rb +82 -0
  84. data/test/unit/collection_test.rb +42 -0
  85. data/test/unit/magic_test.rb +37 -0
  86. data/test/unit/math_test.rb +30 -0
  87. data/test/unit/rulesets_test.rb +94 -0
  88. data/test/unit/samples/test_ruleset.yml +6 -0
  89. data/test/unit/util_test.rb +29 -0
  90. metadata +196 -0
@@ -0,0 +1,18 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+
4
+ describe "Date#downto" do
5
+
6
+ it "should be able to step backward in time" do
7
+ ds = Date.civil(2000, 4, 14)
8
+ de = Date.civil(2000, 3, 29)
9
+ count = 0
10
+ ds.step(de, -1) do |d|
11
+ d.should <= ds
12
+ d.should >= de
13
+ count += 1
14
+ end
15
+ count.should == 17
16
+ end
17
+
18
+ end
@@ -0,0 +1,10 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+
4
+ describe "Date#eql?" do
5
+ it "should be able determine equality between date objects" do
6
+ Date.civil(2007, 10, 11).should eql(Date.civil(2007, 10, 11))
7
+ Date.civil(2007, 10, 11).should eql(Date.civil(2007, 10, 12) - 1)
8
+ Date.civil(2007, 10, 11).should_not eql(Date.civil(2007, 10, 12))
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+
4
+ describe "Date#gregorian?" do
5
+
6
+ it "should mark a day before the calendar reform as Julian" do
7
+ Date.civil(1007, 2, 27).gregorian?.should == false
8
+ Date.civil(1907, 2, 27, Date.civil(2000, 1, 1).jd).gregorian?.should == false
9
+ end
10
+
11
+ it "should mark a day after the calendar reform as Julian" do
12
+ Date.civil(2007, 2, 27).gregorian?.should == true
13
+ Date.civil(1007, 2, 27, Date.civil(1000, 1, 1).jd).gregorian?.should == true
14
+ end
15
+
16
+ end
17
+
18
+ describe "Date#gregorian_leap?" do
19
+
20
+ it "should be able to determine whether a year is a leap year in the Gregorian calendar" do
21
+ Date.gregorian_leap?(1900).should == false
22
+ Date.gregorian_leap?(1999).should == false
23
+ Date.gregorian_leap?(2000).should == true
24
+ Date.gregorian_leap?(2002).should == false
25
+ Date.gregorian_leap?(2004).should == true
26
+ end
27
+
28
+ end
@@ -0,0 +1,14 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+
4
+ describe "Date#hash" do
5
+
6
+ it "should be able determine the hash value for a date" do
7
+ Date.civil(2004, 7, 12).respond_to?(:hash).should == true
8
+ end
9
+
10
+ it "should be the case that the same date results in the same hash" do
11
+ Date.civil(2004, 7, 12).hash.should == Date.civil(2004, 7, 12).hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,77 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+
4
+ describe "Date::Infinity" do
5
+
6
+ it "should be able to check whether Infinity is zero" do
7
+ i = Date::Infinity.new
8
+ i.zero?.should == false
9
+ end
10
+
11
+ it "should be able to check whether Infinity is finite" do
12
+ i1 = Date::Infinity.new
13
+ i1.finite?.should == false
14
+ i2 = Date::Infinity.new(-1)
15
+ i2.finite?.should == false
16
+ i3 = Date::Infinity.new(0)
17
+ i3.finite?.should == false
18
+ end
19
+
20
+ it "should be able to check whether Infinity is infinite" do
21
+ i1 = Date::Infinity.new
22
+ i1.infinite?.should == 1
23
+ i2 = Date::Infinity.new(-1)
24
+ i2.infinite?.should == -1
25
+ i3 = Date::Infinity.new(0)
26
+ i3.infinite?.should == nil
27
+ end
28
+
29
+ it "should be able to check whether Infinity is not a number" do
30
+ i1 = Date::Infinity.new
31
+ i1.nan?.should == false
32
+ i2 = Date::Infinity.new(-1)
33
+ i2.nan?.should == false
34
+ i3 = Date::Infinity.new(0)
35
+ i3.nan?.should == true
36
+ end
37
+
38
+ # These checks fail on MRI because of a bug in Date::Infinity#<=>
39
+ # Fixed in 1.8.7
40
+ ruby_bug "#", "1.8.6" do
41
+ it "should be able to compare Infinity objects" do
42
+ i1 = Date::Infinity.new
43
+ i2 = Date::Infinity.new(-1)
44
+ i3 = Date::Infinity.new(0)
45
+ i4 = Date::Infinity.new
46
+ (i4 <=> i1).should == 0
47
+ (i3 <=> i1).should == -1
48
+ (i2 <=> i1).should == -1
49
+ (i3 <=> i2).should == 1
50
+ end
51
+ end
52
+
53
+ # Also fails because of the same bug as the previous spec
54
+ # Fixed in 1.8.7
55
+ ruby_bug "#", "1.8.6" do
56
+ it "should be able to return plus Infinity for abs" do
57
+ i1 = Date::Infinity.new
58
+ i2 = Date::Infinity.new(-1)
59
+ i3 = Date::Infinity.new(0)
60
+ (i2.abs <=> i1).should == 0
61
+ (i3.abs <=> i1).should == 0
62
+ end
63
+ end
64
+
65
+ ruby_bug "#222", "1.8.6" do
66
+ it "should be able to use -@ and +@ for Date::Infinity" do
67
+ (Date::Infinity.new <=> +Date::Infinity.new).should == 0
68
+ (Date::Infinity.new(-1) <=> -Date::Infinity.new).should == 0
69
+ end
70
+ end
71
+
72
+ it "should be able to coerce a Date::Infinity object" do
73
+ Date::Infinity.new.coerce(1).should == [-1, 1]
74
+ Date::Infinity.new(0).coerce(2).should == [0, 0]
75
+ Date::Infinity.new(-1).coerce(1.5).should == [1, -1]
76
+ end
77
+ end
@@ -0,0 +1,52 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+
4
+ describe "Date#jd" do
5
+
6
+ it "should be able to construct a Date object based on the Julian day" do
7
+ Date.jd(2454482).should == Date.civil(2008, 1, 16)
8
+ end
9
+
10
+ it "should be able to determine the Julian day for a Date object" do
11
+ Date.civil(2008, 1, 16).jd.should == 2454482
12
+ end
13
+
14
+ end
15
+
16
+ describe "Date#julian?" do
17
+
18
+ it "should mark a day before the calendar reform as Julian" do
19
+ Date.civil(1007, 2, 27).julian?.should == true
20
+ Date.civil(1907, 2, 27, Date.civil(2000, 1, 1).jd).julian?.should == true
21
+ end
22
+
23
+ it "should mark a day after the calendar reform as Julian" do
24
+ Date.civil(2007, 2, 27).julian?.should == false
25
+ Date.civil(1007, 2, 27, Date.civil(1000, 1, 1).jd).julian?.should == false
26
+ end
27
+
28
+ end
29
+
30
+ describe "Date#julian_leap?" do
31
+
32
+ it "should be able to determine whether a year is a leap year in the Julian calendar" do
33
+ Date.julian_leap?(1900).should == true
34
+ Date.julian_leap?(1999).should == false
35
+ Date.julian_leap?(2000).should == true
36
+ Date.julian_leap?(2002).should == false
37
+ Date.julian_leap?(2004).should == true
38
+ end
39
+
40
+ end
41
+
42
+ describe "Date#valid_jd?" do
43
+
44
+ it "should be able to determine if a day number is a valid Julian day number, true for all numbers" do
45
+ # This might need to check the type of the jd parameter. Date.valid_jd?(:number) is of course
46
+ # bogus but returns itself with the current implementation
47
+ Date.valid_jd?(-100).should == -100
48
+ Date.valid_jd?(0).should == 0
49
+ Date.valid_jd?(100).should == 100
50
+ end
51
+
52
+ end
@@ -0,0 +1,23 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+
4
+ describe "Date#<<" do
5
+
6
+ it "should substract a number of months from a date" do
7
+ d = Date.civil(2007,2,27) << 10
8
+ d.should == Date.civil(2006, 4, 27)
9
+ end
10
+
11
+ it "should result in the last day of a month if the day doesn't exist" do
12
+ d = Date.civil(2008,3,31) << 1
13
+ d.should == Date.civil(2008, 2, 29)
14
+ end
15
+
16
+ it "should raise an error on non numeric parameters" do
17
+ lambda { Date.civil(2007,2,27) << :hello }.should raise_error(NoMethodError)
18
+ lambda { Date.civil(2007,2,27) << "hello" }.should raise_error(NoMethodError)
19
+ lambda { Date.civil(2007,2,27) << Date.new }.should raise_error(NoMethodError)
20
+ lambda { Date.civil(2007,2,27) << Object.new }.should raise_error(NoMethodError)
21
+ end
22
+
23
+ end
@@ -0,0 +1,30 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+
4
+ describe "Date#-" do
5
+
6
+ it "should substract a number of days from a Date" do
7
+ d = Date.civil(2007, 5 ,2) - 13
8
+ d.should == Date.civil(2007, 4, 19)
9
+ end
10
+
11
+ it "should substract a negative number of days from a Date" do
12
+ d = Date.civil(2007, 4, 19).-(-13)
13
+ d.should == Date.civil(2007, 5 ,2)
14
+ end
15
+
16
+ it "should be able to compute the different between two dates" do
17
+ (Date.civil(2007,2,27) - Date.civil(2007,2,27)).should == 0
18
+ (Date.civil(2007,2,27) - Date.civil(2007,2,26)).should == 1
19
+ (Date.civil(2006,2,27) - Date.civil(2007,2,27)).should == -365
20
+ (Date.civil(2008,2,27) - Date.civil(2007,2,27)).should == 365
21
+
22
+ end
23
+
24
+ it "should raise an error on non numeric parameters" do
25
+ lambda { Date.civil(2007,2,27) - :hello }.should raise_error(TypeError)
26
+ lambda { Date.civil(2007,2,27) - "hello" }.should raise_error(TypeError)
27
+ lambda { Date.civil(2007,2,27) - Object.new }.should raise_error(TypeError)
28
+ end
29
+
30
+ end
@@ -0,0 +1,9 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+ require File.dirname(__FILE__) + '/shared/civil'
4
+
5
+ describe "Date#new" do
6
+
7
+ it_behaves_like(:date_civil, :new)
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+ require File.dirname(__FILE__) + '/shared/commercial'
4
+
5
+ describe "Date#neww" do
6
+
7
+ it_behaves_like(:date_commercial, :neww)
8
+
9
+ end
@@ -0,0 +1,29 @@
1
+ require 'date'
2
+ require File.dirname(__FILE__) + '/../../spec_helper'
3
+ require File.dirname(__FILE__) + '/shared/commercial'
4
+
5
+ describe "Date#ordinal" do
6
+
7
+ it "should be able to construct a Date object from an ordinal date" do
8
+ lambda { Date.ordinal(1582, 287) }.should raise_error(ArgumentError)
9
+ Date.ordinal(1582, 288).should == Date.civil(1582, 10, 15)
10
+ Date.ordinal(1582, 287, Date::ENGLAND).should == Date.civil(1582, 10, 14, Date::ENGLAND)
11
+ end
12
+
13
+ end
14
+
15
+ describe "Date#valid_ordinal?" do
16
+
17
+ it "should be able to determine if the date is a valid ordinal date" do
18
+ Date.valid_ordinal?(1582, 287).should == nil
19
+ Date.valid_ordinal?(1582, 288).should == Date.civil(1582, 10, 15).jd
20
+ Date.valid_ordinal?(1582, 287, Date::ENGLAND).should_not == nil
21
+ Date.valid_ordinal?(1582, 287, Date::ENGLAND).should == Date.civil(1582, 10, 14, Date::ENGLAND).jd
22
+ end
23
+
24
+ it "should be able to handle negative day numbers" do
25
+ Date.valid_ordinal?(1582, -79).should == nil
26
+ Date.valid_ordinal?(2007, -100).should == Date.valid_ordinal?(2007, 266)
27
+ end
28
+
29
+ end
@@ -0,0 +1,149 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+ require File.dirname(__FILE__) + '/shared/parse'
3
+ require File.dirname(__FILE__) + '/shared/parse_us'
4
+ require File.dirname(__FILE__) + '/shared/parse_eu'
5
+ require 'date'
6
+
7
+ describe "Date#parse" do
8
+ # The space separator is also different, doesn't work for only numbers
9
+ it "can parse a day name into a Date object" do
10
+ d = Date.parse("friday")
11
+ d.should == Date.commercial(d.cwyear, d.cweek, 5)
12
+ end
13
+
14
+ it "can parse a month name into a Date object" do
15
+ d = Date.parse("october")
16
+ d.should == Date.civil(Date.today.year, 10)
17
+ end
18
+
19
+ it "can parse a month day into a Date object" do
20
+ d = Date.parse("5th")
21
+ d.should == Date.civil(Date.today.year, Date.today.month, 5)
22
+ end
23
+
24
+ # Specs using numbers
25
+ it "can't handle a single digit" do
26
+ lambda{ Date.parse("1") }.should raise_error(ArgumentError)
27
+ end
28
+
29
+ it "can handle DD as month day number" do
30
+ d = Date.parse("10")
31
+ d.should == Date.civil(Date.today.year, Date.today.month, 10)
32
+ end
33
+
34
+ it "can handle DDD as year day number" do
35
+ d = Date.parse("100")
36
+ if Date.gregorian_leap?(Date.today.year)
37
+ d.should == Date.civil(Date.today.year, 4, 9)
38
+ else
39
+ d.should == Date.civil(Date.today.year, 4, 10)
40
+ end
41
+ end
42
+
43
+ it "can handle MMDD as month and day" do
44
+ d = Date.parse("1108")
45
+ d.should == Date.civil(Date.today.year, 11, 8)
46
+ end
47
+
48
+ it "can handle YYDDD as year and day number" do
49
+ d = Date.parse("10100")
50
+ d.should == Date.civil(10, 4, 10)
51
+ end
52
+
53
+ it "can handle YYMMDD as year month and day" do
54
+ d = Date.parse("201023")
55
+ d.should == Date.civil(20, 10, 23)
56
+ end
57
+
58
+ it "can handle YYYYDDD as year and day number" do
59
+ d = Date.parse("1910100")
60
+ d.should == Date.civil(1910, 4, 10)
61
+ end
62
+
63
+ it "can handle YYYYMMDD as year and day number" do
64
+ d = Date.parse("19101101")
65
+ d.should == Date.civil(1910, 11, 1)
66
+ end
67
+ end
68
+
69
+ describe "Date#parse with '.' separator" do
70
+ before :all do
71
+ @sep = '.'
72
+ end
73
+
74
+ it_should_behave_like "date_parse"
75
+ end
76
+
77
+ describe "Date#parse with '/' separator" do
78
+ before :all do
79
+ @sep = '/'
80
+ end
81
+
82
+ it_should_behave_like "date_parse"
83
+ end
84
+
85
+ describe "Date#parse with ' ' separator" do
86
+ before :all do
87
+ @sep = ' '
88
+ end
89
+
90
+ it_should_behave_like "date_parse"
91
+ end
92
+
93
+ describe "Date#parse with '/' separator US-style" do
94
+ before :all do
95
+ @sep = '/'
96
+ end
97
+
98
+ it_should_behave_like "date_parse_us"
99
+ end
100
+
101
+ ruby_version_is "" ... "1.8.7" do
102
+ describe "Date#parse with '.' separator US-style" do
103
+ before :all do
104
+ @sep = '.'
105
+ end
106
+
107
+ it_should_behave_like "date_parse_us"
108
+ end
109
+ end
110
+
111
+ describe "Date#parse with '-' separator EU-style" do
112
+ before :all do
113
+ @sep = '-'
114
+ end
115
+
116
+ it_should_behave_like "date_parse_eu"
117
+ end
118
+
119
+ ruby_version_is "1.8.7" do
120
+ describe "Date#parse(.)" do
121
+ it "parses a YYYY.MM.DD string into a Date object" do
122
+ d = Date.parse("2007.10.01")
123
+ d.year.should == 2007
124
+ d.month.should == 10
125
+ d.day.should == 1
126
+ end
127
+
128
+ it "parses a DD.MM.YYYY string into a Date object" do
129
+ d = Date.parse("10.01.2007")
130
+ d.year.should == 2007
131
+ d.month.should == 1
132
+ d.day.should == 10
133
+ end
134
+
135
+ it "parses a YY.MM.DD string into a Date object" do
136
+ d = Date.parse("10.01.07")
137
+ d.year.should == 10
138
+ d.month.should == 1
139
+ d.day.should == 7
140
+ end
141
+
142
+ it "parses a YY.MM.DD string into a Date object using the year digits as 20XX" do
143
+ d = Date.parse("10.01.07", true)
144
+ d.year.should == 2010
145
+ d.month.should == 1
146
+ d.day.should == 7
147
+ end
148
+ end
149
+ end