chronic-mmlac 0.6.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/.gitignore +5 -0
  4. data/.yardopts +3 -0
  5. data/HISTORY.md +174 -0
  6. data/LICENSE +21 -0
  7. data/README.md +177 -0
  8. data/Rakefile +46 -0
  9. data/chronic.gemspec +17 -0
  10. data/lib/chronic/chronic.rb +325 -0
  11. data/lib/chronic/grabber.rb +31 -0
  12. data/lib/chronic/handler.rb +90 -0
  13. data/lib/chronic/handlers.rb +465 -0
  14. data/lib/chronic/mini_date.rb +38 -0
  15. data/lib/chronic/numerizer.rb +121 -0
  16. data/lib/chronic/ordinal.rb +44 -0
  17. data/lib/chronic/pointer.rb +30 -0
  18. data/lib/chronic/repeater.rb +135 -0
  19. data/lib/chronic/repeaters/repeater_day.rb +53 -0
  20. data/lib/chronic/repeaters/repeater_day_name.rb +52 -0
  21. data/lib/chronic/repeaters/repeater_day_portion.rb +94 -0
  22. data/lib/chronic/repeaters/repeater_fortnight.rb +71 -0
  23. data/lib/chronic/repeaters/repeater_hour.rb +58 -0
  24. data/lib/chronic/repeaters/repeater_minute.rb +58 -0
  25. data/lib/chronic/repeaters/repeater_month.rb +79 -0
  26. data/lib/chronic/repeaters/repeater_month_name.rb +94 -0
  27. data/lib/chronic/repeaters/repeater_season.rb +109 -0
  28. data/lib/chronic/repeaters/repeater_season_name.rb +43 -0
  29. data/lib/chronic/repeaters/repeater_second.rb +42 -0
  30. data/lib/chronic/repeaters/repeater_time.rb +128 -0
  31. data/lib/chronic/repeaters/repeater_week.rb +74 -0
  32. data/lib/chronic/repeaters/repeater_weekday.rb +85 -0
  33. data/lib/chronic/repeaters/repeater_weekend.rb +66 -0
  34. data/lib/chronic/repeaters/repeater_year.rb +77 -0
  35. data/lib/chronic/scalar.rb +109 -0
  36. data/lib/chronic/season.rb +37 -0
  37. data/lib/chronic/separator.rb +88 -0
  38. data/lib/chronic/span.rb +31 -0
  39. data/lib/chronic/tag.rb +42 -0
  40. data/lib/chronic/time_zone.rb +30 -0
  41. data/lib/chronic/token.rb +45 -0
  42. data/lib/chronic.rb +118 -0
  43. data/test/helper.rb +6 -0
  44. data/test/test_Chronic.rb +148 -0
  45. data/test/test_DaylightSavings.rb +118 -0
  46. data/test/test_Handler.rb +104 -0
  47. data/test/test_MiniDate.rb +32 -0
  48. data/test/test_Numerizer.rb +72 -0
  49. data/test/test_RepeaterDayName.rb +51 -0
  50. data/test/test_RepeaterFortnight.rb +62 -0
  51. data/test/test_RepeaterHour.rb +68 -0
  52. data/test/test_RepeaterMinute.rb +34 -0
  53. data/test/test_RepeaterMonth.rb +50 -0
  54. data/test/test_RepeaterMonthName.rb +56 -0
  55. data/test/test_RepeaterSeason.rb +40 -0
  56. data/test/test_RepeaterTime.rb +70 -0
  57. data/test/test_RepeaterWeek.rb +62 -0
  58. data/test/test_RepeaterWeekday.rb +55 -0
  59. data/test/test_RepeaterWeekend.rb +74 -0
  60. data/test/test_RepeaterYear.rb +69 -0
  61. data/test/test_Span.rb +23 -0
  62. data/test/test_Token.rb +25 -0
  63. data/test/test_parsing.rb +886 -0
  64. metadata +132 -0
@@ -0,0 +1,135 @@
1
+ module Chronic
2
+ class Repeater < Tag
3
+
4
+ # Scan an Array of {Token}s and apply any necessary Repeater tags to
5
+ # each token
6
+ #
7
+ # @param [Array<Token>] tokens Array of tokens to scan
8
+ # @param [Hash] options Options specified in {Chronic.parse}
9
+ # @return [Array] list of tokens
10
+ def self.scan(tokens, options)
11
+ tokens.each do |token|
12
+ if t = scan_for_season_names(token) then token.tag(t); next end
13
+ if t = scan_for_month_names(token) then token.tag(t); next end
14
+ if t = scan_for_day_names(token) then token.tag(t); next end
15
+ if t = scan_for_day_portions(token) then token.tag(t); next end
16
+ if t = scan_for_times(token) then token.tag(t); next end
17
+ if t = scan_for_units(token) then token.tag(t); next end
18
+ end
19
+ end
20
+
21
+ # @param [Token] token
22
+ # @return [RepeaterSeasonName, nil]
23
+ def self.scan_for_season_names(token)
24
+ scan_for token, RepeaterSeasonName,
25
+ {
26
+ /^springs?$/ => :spring,
27
+ /^summers?$/ => :summer,
28
+ /^(autumn)|(fall)s?$/ => :autumn,
29
+ /^winters?$/ => :winter
30
+ }
31
+ end
32
+
33
+ # @param [Token] token
34
+ # @return [RepeaterMonthName, nil]
35
+ def self.scan_for_month_names(token)
36
+ scan_for token, RepeaterMonthName,
37
+ {
38
+ /^jan\.?(uary)?$/ => :january,
39
+ /^feb\.?(ruary)?$/ => :february,
40
+ /^mar\.?(ch)?$/ => :march,
41
+ /^apr\.?(il)?$/ => :april,
42
+ /^may$/ => :may,
43
+ /^jun\.?e?$/ => :june,
44
+ /^jul\.?y?$/ => :july,
45
+ /^aug\.?(ust)?$/ => :august,
46
+ /^sep\.?(t\.?|tember)?$/ => :september,
47
+ /^oct\.?(ober)?$/ => :october,
48
+ /^nov\.?(ember)?$/ => :november,
49
+ /^dec\.?(ember)?$/ => :december
50
+ }
51
+ end
52
+
53
+ # @param [Token] token
54
+ # @return [RepeaterDayName, nil]
55
+ def self.scan_for_day_names(token)
56
+ scan_for token, RepeaterDayName,
57
+ {
58
+ /^m[ou]n(day)?$/ => :monday,
59
+ /^t(ue|eu|oo|u|)s?(day)?$/ => :tuesday,
60
+ /^we(d|dnes|nds|nns)(day)?$/ => :wednesday,
61
+ /^th(u(?:rs)?|ers)(day)?$/ => :thursday,
62
+ /^fr[iy](day)?$/ => :friday,
63
+ /^sat(t?[ue]rday)?$/ => :saturday,
64
+ /^su[nm](day)?$/ => :sunday
65
+ }
66
+ end
67
+
68
+ # @param [Token] token
69
+ # @return [RepeaterDayPortion, nil]
70
+ def self.scan_for_day_portions(token)
71
+ scan_for token, RepeaterDayPortion,
72
+ {
73
+ /^ams?$/ => :am,
74
+ /^pms?$/ => :pm,
75
+ /^mornings?$/ => :morning,
76
+ /^afternoons?$/ => :afternoon,
77
+ /^evenings?$/ => :evening,
78
+ /^(night|nite)s?$/ => :night
79
+ }
80
+ end
81
+
82
+ # @param [Token] token
83
+ # @return [RepeaterTime, nil]
84
+ def self.scan_for_times(token)
85
+ scan_for token, RepeaterTime, /^\d{1,2}(:?\d{2})?([\.:]?\d{2})?$/
86
+ end
87
+
88
+ # @param [Token] token
89
+ # @return [Repeater] A new instance of a subclass of Repeater
90
+ def self.scan_for_units(token)
91
+ {
92
+ /^years?$/ => :year,
93
+ /^seasons?$/ => :season,
94
+ /^months?$/ => :month,
95
+ /^fortnights?$/ => :fortnight,
96
+ /^weeks?$/ => :week,
97
+ /^weekends?$/ => :weekend,
98
+ /^(week|business)days?$/ => :weekday,
99
+ /^days?$/ => :day,
100
+ /^hours?$/ => :hour,
101
+ /^minutes?$/ => :minute,
102
+ /^seconds?$/ => :second
103
+ }.each do |item, symbol|
104
+ if item =~ token.word
105
+ klass_name = 'Repeater' + symbol.to_s.capitalize
106
+ klass = Chronic.const_get(klass_name)
107
+ return klass.new(symbol)
108
+ end
109
+ end
110
+ return nil
111
+ end
112
+
113
+ def <=>(other)
114
+ width <=> other.width
115
+ end
116
+
117
+ # returns the width (in seconds or months) of this repeatable.
118
+ def width
119
+ raise("Repeater#width must be overridden in subclasses")
120
+ end
121
+
122
+ # returns the next occurance of this repeatable.
123
+ def next(pointer)
124
+ raise("Start point must be set before calling #next") unless @now
125
+ end
126
+
127
+ def this(pointer)
128
+ raise("Start point must be set before calling #this") unless @now
129
+ end
130
+
131
+ def to_s
132
+ 'repeater'
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,53 @@
1
+ module Chronic
2
+ class RepeaterDay < Repeater #:nodoc:
3
+ DAY_SECONDS = 86_400 # (24 * 60 * 60)
4
+
5
+ def initialize(type)
6
+ super
7
+ end
8
+
9
+ def next(pointer)
10
+ super
11
+
12
+ if !@current_day_start
13
+ @current_day_start = Chronic.time_class.local(@now.year, @now.month, @now.day)
14
+ end
15
+
16
+ direction = pointer == :future ? 1 : -1
17
+ @current_day_start += direction * DAY_SECONDS
18
+
19
+ Span.new(@current_day_start, @current_day_start + DAY_SECONDS)
20
+ end
21
+
22
+ def this(pointer = :future)
23
+ super
24
+
25
+ case pointer
26
+ when :future
27
+ day_begin = Chronic.construct(@now.year, @now.month, @now.day, @now.hour)
28
+ day_end = Chronic.construct(@now.year, @now.month, @now.day) + DAY_SECONDS
29
+ when :past
30
+ day_begin = Chronic.construct(@now.year, @now.month, @now.day)
31
+ day_end = Chronic.construct(@now.year, @now.month, @now.day, @now.hour)
32
+ when :none
33
+ day_begin = Chronic.construct(@now.year, @now.month, @now.day)
34
+ day_end = Chronic.construct(@now.year, @now.month, @now.day) + DAY_SECONDS
35
+ end
36
+
37
+ Span.new(day_begin, day_end)
38
+ end
39
+
40
+ def offset(span, amount, pointer)
41
+ direction = pointer == :future ? 1 : -1
42
+ span + direction * amount * DAY_SECONDS
43
+ end
44
+
45
+ def width
46
+ DAY_SECONDS
47
+ end
48
+
49
+ def to_s
50
+ super << '-day'
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,52 @@
1
+ module Chronic
2
+ class RepeaterDayName < Repeater #:nodoc:
3
+ DAY_SECONDS = 86400 # (24 * 60 * 60)
4
+
5
+ def initialize(type)
6
+ super
7
+ end
8
+
9
+ def next(pointer)
10
+ super
11
+
12
+ direction = pointer == :future ? 1 : -1
13
+
14
+ if !@current_date
15
+ @current_date = Date.new(@now.year, @now.month, @now.day)
16
+ @current_date += direction
17
+
18
+ day_num = symbol_to_number(@type)
19
+
20
+ while @current_date.wday != day_num
21
+ @current_date += direction
22
+ end
23
+ else
24
+ @current_date += direction * 7
25
+ end
26
+ next_date = @current_date.succ
27
+ Span.new(Chronic.construct(@current_date.year, @current_date.month, @current_date.day), Chronic.construct(next_date.year, next_date.month, next_date.day))
28
+ end
29
+
30
+ def this(pointer = :future)
31
+ super
32
+
33
+ pointer = :future if pointer == :none
34
+ self.next(pointer)
35
+ end
36
+
37
+ def width
38
+ DAY_SECONDS
39
+ end
40
+
41
+ def to_s
42
+ super << '-dayname-' << @type.to_s
43
+ end
44
+
45
+ private
46
+
47
+ def symbol_to_number(sym)
48
+ lookup = {:sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3, :thursday => 4, :friday => 5, :saturday => 6}
49
+ lookup[sym] || raise("Invalid symbol specified")
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,94 @@
1
+ module Chronic
2
+ class RepeaterDayPortion < Repeater #:nodoc:
3
+ PORTIONS = {
4
+ :am => 0..(12 * 60 * 60 - 1),
5
+ :pm => (12 * 60 * 60)..(24 * 60 * 60 - 1),
6
+ :morning => (6 * 60 * 60)..(12 * 60 * 60), # 6am-12am,
7
+ :afternoon => (13 * 60 * 60)..(17 * 60 * 60), # 1pm-5pm,
8
+ :evening => (17 * 60 * 60)..(20 * 60 * 60), # 5pm-8pm,
9
+ :night => (20 * 60 * 60)..(24 * 60 * 60), # 8pm-12pm
10
+ }
11
+
12
+ def initialize(type)
13
+ super
14
+
15
+ if type.kind_of? Integer
16
+ @range = (@type * 60 * 60)..((@type + 12) * 60 * 60)
17
+ else
18
+ @range = PORTIONS[type]
19
+ @range || raise("Invalid type '#{type}' for RepeaterDayPortion")
20
+ end
21
+
22
+ @range || raise("Range should have been set by now")
23
+ end
24
+
25
+ def next(pointer)
26
+ super
27
+
28
+ full_day = 60 * 60 * 24
29
+
30
+ if !@current_span
31
+ now_seconds = @now - Chronic.construct(@now.year, @now.month, @now.day)
32
+ if now_seconds < @range.begin
33
+ case pointer
34
+ when :future
35
+ range_start = Chronic.construct(@now.year, @now.month, @now.day) + @range.begin
36
+ when :past
37
+ range_start = Chronic.construct(@now.year, @now.month, @now.day) - full_day + @range.begin
38
+ end
39
+ elsif now_seconds > @range.end
40
+ case pointer
41
+ when :future
42
+ range_start = Chronic.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
43
+ when :past
44
+ range_start = Chronic.construct(@now.year, @now.month, @now.day) + @range.begin
45
+ end
46
+ else
47
+ case pointer
48
+ when :future
49
+ range_start = Chronic.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
50
+ when :past
51
+ range_start = Chronic.construct(@now.year, @now.month, @now.day) - full_day + @range.begin
52
+ end
53
+ end
54
+
55
+ @current_span = Span.new(range_start, range_start + (@range.end - @range.begin))
56
+ else
57
+ case pointer
58
+ when :future
59
+ @current_span += full_day
60
+ when :past
61
+ @current_span -= full_day
62
+ end
63
+ end
64
+ end
65
+
66
+ def this(context = :future)
67
+ super
68
+
69
+ range_start = Chronic.construct(@now.year, @now.month, @now.day) + @range.begin
70
+ @current_span = Span.new(range_start, range_start + (@range.end - @range.begin))
71
+ end
72
+
73
+ def offset(span, amount, pointer)
74
+ @now = span.begin
75
+ portion_span = self.next(pointer)
76
+ direction = pointer == :future ? 1 : -1
77
+ portion_span + (direction * (amount - 1) * RepeaterDay::DAY_SECONDS)
78
+ end
79
+
80
+ def width
81
+ @range || raise("Range has not been set")
82
+ return @current_span.width if @current_span
83
+ if @type.kind_of? Integer
84
+ return (12 * 60 * 60)
85
+ else
86
+ @range.end - @range.begin
87
+ end
88
+ end
89
+
90
+ def to_s
91
+ super << '-dayportion-' << @type.to_s
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,71 @@
1
+ module Chronic
2
+ class RepeaterFortnight < Repeater #:nodoc:
3
+ FORTNIGHT_SECONDS = 1_209_600 # (14 * 24 * 60 * 60)
4
+
5
+ def initialize(type)
6
+ super
7
+ end
8
+
9
+ def next(pointer)
10
+ super
11
+
12
+ if !@current_fortnight_start
13
+ case pointer
14
+ when :future
15
+ sunday_repeater = RepeaterDayName.new(:sunday)
16
+ sunday_repeater.start = @now
17
+ next_sunday_span = sunday_repeater.next(:future)
18
+ @current_fortnight_start = next_sunday_span.begin
19
+ when :past
20
+ sunday_repeater = RepeaterDayName.new(:sunday)
21
+ sunday_repeater.start = (@now + RepeaterDay::DAY_SECONDS)
22
+ 2.times { sunday_repeater.next(:past) }
23
+ last_sunday_span = sunday_repeater.next(:past)
24
+ @current_fortnight_start = last_sunday_span.begin
25
+ end
26
+ else
27
+ direction = pointer == :future ? 1 : -1
28
+ @current_fortnight_start += direction * FORTNIGHT_SECONDS
29
+ end
30
+
31
+ Span.new(@current_fortnight_start, @current_fortnight_start + FORTNIGHT_SECONDS)
32
+ end
33
+
34
+ def this(pointer = :future)
35
+ super
36
+
37
+ pointer = :future if pointer == :none
38
+
39
+ case pointer
40
+ when :future
41
+ this_fortnight_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour) + RepeaterHour::HOUR_SECONDS
42
+ sunday_repeater = RepeaterDayName.new(:sunday)
43
+ sunday_repeater.start = @now
44
+ sunday_repeater.this(:future)
45
+ this_sunday_span = sunday_repeater.this(:future)
46
+ this_fortnight_end = this_sunday_span.begin
47
+ Span.new(this_fortnight_start, this_fortnight_end)
48
+ when :past
49
+ this_fortnight_end = Chronic.construct(@now.year, @now.month, @now.day, @now.hour)
50
+ sunday_repeater = RepeaterDayName.new(:sunday)
51
+ sunday_repeater.start = @now
52
+ last_sunday_span = sunday_repeater.next(:past)
53
+ this_fortnight_start = last_sunday_span.begin
54
+ Span.new(this_fortnight_start, this_fortnight_end)
55
+ end
56
+ end
57
+
58
+ def offset(span, amount, pointer)
59
+ direction = pointer == :future ? 1 : -1
60
+ span + direction * amount * FORTNIGHT_SECONDS
61
+ end
62
+
63
+ def width
64
+ FORTNIGHT_SECONDS
65
+ end
66
+
67
+ def to_s
68
+ super << '-fortnight'
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,58 @@
1
+ module Chronic
2
+ class RepeaterHour < Repeater #:nodoc:
3
+ HOUR_SECONDS = 3600 # 60 * 60
4
+
5
+ def initialize(type)
6
+ super
7
+ end
8
+
9
+ def next(pointer)
10
+ super
11
+
12
+ if !@current_hour_start
13
+ case pointer
14
+ when :future
15
+ @current_hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour + 1)
16
+ when :past
17
+ @current_hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour - 1)
18
+ end
19
+ else
20
+ direction = pointer == :future ? 1 : -1
21
+ @current_hour_start += direction * HOUR_SECONDS
22
+ end
23
+
24
+ Span.new(@current_hour_start, @current_hour_start + HOUR_SECONDS)
25
+ end
26
+
27
+ def this(pointer = :future)
28
+ super
29
+
30
+ case pointer
31
+ when :future
32
+ hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
33
+ hour_end = Chronic.construct(@now.year, @now.month, @now.day, @now.hour + 1)
34
+ when :past
35
+ hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour)
36
+ hour_end = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
37
+ when :none
38
+ hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour)
39
+ hour_end = hour_start + HOUR_SECONDS
40
+ end
41
+
42
+ Span.new(hour_start, hour_end)
43
+ end
44
+
45
+ def offset(span, amount, pointer)
46
+ direction = pointer == :future ? 1 : -1
47
+ span + direction * amount * HOUR_SECONDS
48
+ end
49
+
50
+ def width
51
+ HOUR_SECONDS
52
+ end
53
+
54
+ def to_s
55
+ super << '-hour'
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,58 @@
1
+ module Chronic
2
+ class RepeaterMinute < Repeater #:nodoc:
3
+ MINUTE_SECONDS = 60
4
+
5
+ def initialize(type)
6
+ super
7
+ end
8
+
9
+ def next(pointer = :future)
10
+ super
11
+
12
+ if !@current_minute_start
13
+ case pointer
14
+ when :future
15
+ @current_minute_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
16
+ when :past
17
+ @current_minute_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min - 1)
18
+ end
19
+ else
20
+ direction = pointer == :future ? 1 : -1
21
+ @current_minute_start += direction * MINUTE_SECONDS
22
+ end
23
+
24
+ Span.new(@current_minute_start, @current_minute_start + MINUTE_SECONDS)
25
+ end
26
+
27
+ def this(pointer = :future)
28
+ super
29
+
30
+ case pointer
31
+ when :future
32
+ minute_begin = @now
33
+ minute_end = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
34
+ when :past
35
+ minute_begin = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
36
+ minute_end = @now
37
+ when :none
38
+ minute_begin = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
39
+ minute_end = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min) + MINUTE_SECONDS
40
+ end
41
+
42
+ Span.new(minute_begin, minute_end)
43
+ end
44
+
45
+ def offset(span, amount, pointer)
46
+ direction = pointer == :future ? 1 : -1
47
+ span + direction * amount * MINUTE_SECONDS
48
+ end
49
+
50
+ def width
51
+ MINUTE_SECONDS
52
+ end
53
+
54
+ def to_s
55
+ super << '-minute'
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,79 @@
1
+ module Chronic
2
+ class RepeaterMonth < Repeater #:nodoc:
3
+ MONTH_SECONDS = 2_592_000 # 30 * 24 * 60 * 60
4
+ YEAR_MONTHS = 12
5
+ MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
6
+ MONTH_DAYS_LEAP = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
7
+
8
+ def initialize(type)
9
+ super
10
+ end
11
+
12
+ def next(pointer)
13
+ super
14
+
15
+ if !@current_month_start
16
+ @current_month_start = offset_by(Chronic.construct(@now.year, @now.month), 1, pointer)
17
+ else
18
+ @current_month_start = offset_by(Chronic.construct(@current_month_start.year, @current_month_start.month), 1, pointer)
19
+ end
20
+
21
+ Span.new(@current_month_start, Chronic.construct(@current_month_start.year, @current_month_start.month + 1))
22
+ end
23
+
24
+ def this(pointer = :future)
25
+ super
26
+
27
+ case pointer
28
+ when :future
29
+ month_start = Chronic.construct(@now.year, @now.month, @now.day + 1)
30
+ month_end = self.offset_by(Chronic.construct(@now.year, @now.month), 1, :future)
31
+ when :past
32
+ month_start = Chronic.construct(@now.year, @now.month)
33
+ month_end = Chronic.construct(@now.year, @now.month, @now.day)
34
+ when :none
35
+ month_start = Chronic.construct(@now.year, @now.month)
36
+ month_end = self.offset_by(Chronic.construct(@now.year, @now.month), 1, :future)
37
+ end
38
+
39
+ Span.new(month_start, month_end)
40
+ end
41
+
42
+ def offset(span, amount, pointer)
43
+ Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
44
+ end
45
+
46
+ def offset_by(time, amount, pointer)
47
+ direction = pointer == :future ? 1 : -1
48
+
49
+ amount_years = direction * amount / YEAR_MONTHS
50
+ amount_months = direction * amount % YEAR_MONTHS
51
+
52
+ new_year = time.year + amount_years
53
+ new_month = time.month + amount_months
54
+ if new_month > YEAR_MONTHS
55
+ new_year += 1
56
+ new_month -= YEAR_MONTHS
57
+ end
58
+
59
+ days = month_days(new_year, new_month)
60
+ new_day = time.day > days ? days : time.day
61
+
62
+ Chronic.construct(new_year, new_month, new_day, time.hour, time.min, time.sec)
63
+ end
64
+
65
+ def width
66
+ MONTH_SECONDS
67
+ end
68
+
69
+ def to_s
70
+ super << '-month'
71
+ end
72
+
73
+ private
74
+
75
+ def month_days(year, month)
76
+ Date.leap?(year) ? MONTH_DAYS_LEAP[month - 1] : MONTH_DAYS[month - 1]
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,94 @@
1
+ module Chronic
2
+ class RepeaterMonthName < Repeater #:nodoc:
3
+ MONTH_SECONDS = 2_592_000 # 30 * 24 * 60 * 60
4
+ MONTHS = {
5
+ :january => 1,
6
+ :february => 2,
7
+ :march => 3,
8
+ :april => 4,
9
+ :may => 5,
10
+ :june => 6,
11
+ :july => 7,
12
+ :august => 8,
13
+ :september => 9,
14
+ :october => 10,
15
+ :november => 11,
16
+ :december => 12
17
+ }
18
+
19
+ def initialize(type)
20
+ super
21
+ end
22
+
23
+ def next(pointer)
24
+ super
25
+
26
+ if !@current_month_begin
27
+ case pointer
28
+ when :future
29
+ if @now.month < index
30
+ @current_month_begin = Chronic.construct(@now.year, index)
31
+ elsif @now.month > index
32
+ @current_month_begin = Chronic.construct(@now.year + 1, index)
33
+ end
34
+ when :none
35
+ if @now.month <= index
36
+ @current_month_begin = Chronic.construct(@now.year, index)
37
+ elsif @now.month > index
38
+ @current_month_begin = Chronic.construct(@now.year + 1, index)
39
+ end
40
+ when :past
41
+ if @now.month >= index
42
+ @current_month_begin = Chronic.construct(@now.year, index)
43
+ elsif @now.month < index
44
+ @current_month_begin = Chronic.construct(@now.year - 1, index)
45
+ end
46
+ end
47
+ @current_month_begin || raise("Current month should be set by now")
48
+ else
49
+ case pointer
50
+ when :future
51
+ @current_month_begin = Chronic.construct(@current_month_begin.year + 1, @current_month_begin.month)
52
+ when :past
53
+ @current_month_begin = Chronic.construct(@current_month_begin.year - 1, @current_month_begin.month)
54
+ end
55
+ end
56
+
57
+ cur_month_year = @current_month_begin.year
58
+ cur_month_month = @current_month_begin.month
59
+
60
+ if cur_month_month == 12
61
+ next_month_year = cur_month_year + 1
62
+ next_month_month = 1
63
+ else
64
+ next_month_year = cur_month_year
65
+ next_month_month = cur_month_month + 1
66
+ end
67
+
68
+ Span.new(@current_month_begin, Chronic.construct(next_month_year, next_month_month))
69
+ end
70
+
71
+ def this(pointer = :future)
72
+ super
73
+
74
+ case pointer
75
+ when :past
76
+ self.next(pointer)
77
+ when :future, :none
78
+ self.next(:none)
79
+ end
80
+ end
81
+
82
+ def width
83
+ MONTH_SECONDS
84
+ end
85
+
86
+ def index
87
+ @index ||= MONTHS[@type]
88
+ end
89
+
90
+ def to_s
91
+ super << '-monthname-' << @type.to_s
92
+ end
93
+ end
94
+ end