chronic 0.2.3 → 0.4.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 (63) hide show
  1. data/HISTORY.md +103 -0
  2. data/LICENSE +21 -0
  3. data/Manifest.txt +16 -5
  4. data/README.md +171 -0
  5. data/Rakefile +136 -15
  6. data/benchmark/benchmark.rb +13 -0
  7. data/chronic.gemspec +82 -0
  8. data/lib/chronic/chronic.rb +65 -147
  9. data/lib/chronic/grabber.rb +13 -17
  10. data/lib/chronic/handlers.rb +216 -138
  11. data/lib/chronic/mini_date.rb +27 -0
  12. data/lib/chronic/numerizer.rb +120 -0
  13. data/lib/chronic/ordinal.rb +11 -16
  14. data/lib/chronic/pointer.rb +11 -13
  15. data/lib/chronic/repeater.rb +117 -106
  16. data/lib/chronic/repeaters/repeater_day.rb +50 -43
  17. data/lib/chronic/repeaters/repeater_day_name.rb +49 -42
  18. data/lib/chronic/repeaters/repeater_day_portion.rb +82 -80
  19. data/lib/chronic/repeaters/repeater_fortnight.rb +60 -53
  20. data/lib/chronic/repeaters/repeater_hour.rb +50 -43
  21. data/lib/chronic/repeaters/repeater_minute.rb +50 -43
  22. data/lib/chronic/repeaters/repeater_month.rb +63 -56
  23. data/lib/chronic/repeaters/repeater_month_name.rb +91 -82
  24. data/lib/chronic/repeaters/repeater_season.rb +125 -20
  25. data/lib/chronic/repeaters/repeater_season_name.rb +43 -22
  26. data/lib/chronic/repeaters/repeater_second.rb +40 -33
  27. data/lib/chronic/repeaters/repeater_time.rb +118 -106
  28. data/lib/chronic/repeaters/repeater_week.rb +64 -57
  29. data/lib/chronic/repeaters/repeater_weekday.rb +86 -0
  30. data/lib/chronic/repeaters/repeater_weekend.rb +58 -51
  31. data/lib/chronic/repeaters/repeater_year.rb +58 -50
  32. data/lib/chronic/scalar.rb +37 -27
  33. data/lib/chronic/separator.rb +34 -37
  34. data/lib/chronic/span.rb +31 -0
  35. data/lib/chronic/tag.rb +26 -0
  36. data/lib/chronic/time_zone.rb +9 -10
  37. data/lib/chronic/token.rb +35 -0
  38. data/lib/chronic.rb +34 -25
  39. data/test/helper.rb +6 -0
  40. data/test/test_Chronic.rb +22 -18
  41. data/test/test_DaylightSavings.rb +118 -0
  42. data/test/test_Handler.rb +37 -38
  43. data/test/test_Numerizer.rb +66 -42
  44. data/test/test_RepeaterDayName.rb +15 -16
  45. data/test/test_RepeaterFortnight.rb +16 -17
  46. data/test/test_RepeaterHour.rb +22 -19
  47. data/test/test_RepeaterMinute.rb +34 -0
  48. data/test/test_RepeaterMonth.rb +16 -17
  49. data/test/test_RepeaterMonthName.rb +17 -18
  50. data/test/test_RepeaterTime.rb +20 -22
  51. data/test/test_RepeaterWeek.rb +16 -17
  52. data/test/test_RepeaterWeekday.rb +55 -0
  53. data/test/test_RepeaterWeekend.rb +21 -22
  54. data/test/test_RepeaterYear.rb +17 -18
  55. data/test/test_Span.rb +5 -6
  56. data/test/test_Time.rb +11 -12
  57. data/test/test_Token.rb +5 -6
  58. data/test/test_parsing.rb +395 -208
  59. metadata +68 -52
  60. data/History.txt +0 -53
  61. data/README.txt +0 -149
  62. data/lib/numerizer/numerizer.rb +0 -103
  63. data/test/suite.rb +0 -9
@@ -1,93 +1,95 @@
1
- class Chronic::RepeaterDayPortion < Chronic::Repeater #:nodoc:
2
- @@morning = (6 * 60 * 60)..(12 * 60 * 60) # 6am-12am
3
- @@afternoon = (13 * 60 * 60)..(17 * 60 * 60) # 1pm-5pm
4
- @@evening = (17 * 60 * 60)..(20 * 60 * 60) # 5pm-8pm
5
- @@night = (20 * 60 * 60)..(24 * 60 * 60) # 8pm-12pm
6
-
7
- def initialize(type)
8
- super
9
-
10
- if type.kind_of? Integer
11
- @range = (@type * 60 * 60)..((@type + 12) * 60 * 60)
12
- else
13
- lookup = {:am => 0..(12 * 60 * 60 - 1),
14
- :pm => (12 * 60 * 60)..(24 * 60 * 60 - 1),
15
- :morning => @@morning,
16
- :afternoon => @@afternoon,
17
- :evening => @@evening,
18
- :night => @@night}
19
- @range = lookup[type]
20
- lookup[type] || raise("Invalid type '#{type}' for RepeaterDayPortion")
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
+ @current_span = nil
15
+
16
+ if type.kind_of? Integer
17
+ @range = (@type * 60 * 60)..((@type + 12) * 60 * 60)
18
+ else
19
+ @range = PORTIONS[type]
20
+ @range || raise("Invalid type '#{type}' for RepeaterDayPortion")
21
+ end
22
+
23
+ @range || raise("Range should have been set by now")
21
24
  end
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 - Time.construct(@now.year, @now.month, @now.day)
32
- if now_seconds < @range.begin
33
- case pointer
34
- when :future
35
- range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
36
- when :past
37
- range_start = Time.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 = Time.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
43
- when :past
44
- range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
25
+
26
+ def next(pointer)
27
+ super
28
+
29
+ full_day = 60 * 60 * 24
30
+
31
+ if !@current_span
32
+ now_seconds = @now - Time.construct(@now.year, @now.month, @now.day)
33
+ if now_seconds < @range.begin
34
+ case pointer
35
+ when :future
36
+ range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
37
+ when :past
38
+ range_start = Time.construct(@now.year, @now.month, @now.day) - full_day + @range.begin
39
+ end
40
+ elsif now_seconds > @range.end
41
+ case pointer
42
+ when :future
43
+ range_start = Time.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
44
+ when :past
45
+ range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
46
+ end
47
+ else
48
+ case pointer
49
+ when :future
50
+ range_start = Time.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
51
+ when :past
52
+ range_start = Time.construct(@now.year, @now.month, @now.day) - full_day + @range.begin
53
+ end
45
54
  end
55
+
56
+ @current_span = Span.new(range_start, range_start + (@range.end - @range.begin))
46
57
  else
47
58
  case pointer
48
59
  when :future
49
- range_start = Time.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
60
+ @current_span += full_day
50
61
  when :past
51
- range_start = Time.construct(@now.year, @now.month, @now.day) - full_day + @range.begin
62
+ @current_span -= full_day
52
63
  end
53
64
  end
54
-
55
- @current_span = Chronic::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
65
+ end
66
+
67
+ def this(context = :future)
68
+ super
69
+
70
+ range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
71
+ @current_span = Span.new(range_start, range_start + (@range.end - @range.begin))
72
+ end
73
+
74
+ def offset(span, amount, pointer)
75
+ @now = span.begin
76
+ portion_span = self.next(pointer)
77
+ direction = pointer == :future ? 1 : -1
78
+ portion_span + (direction * (amount - 1) * RepeaterDay::DAY_SECONDS)
79
+ end
80
+
81
+ def width
82
+ @range || raise("Range has not been set")
83
+ return @current_span.width if @current_span
84
+ if @type.kind_of? Integer
85
+ return (12 * 60 * 60)
86
+ else
87
+ @range.end - @range.begin
62
88
  end
63
89
  end
64
- end
65
-
66
- def this(context = :future)
67
- super
68
-
69
- range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
70
- @current_span = Chronic::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) * Chronic::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
90
+
91
+ def to_s
92
+ super << '-dayportion-' << @type.to_s
87
93
  end
88
94
  end
89
-
90
- def to_s
91
- super << '-dayportion-' << @type.to_s
92
- end
93
95
  end
@@ -1,65 +1,72 @@
1
- class Chronic::RepeaterFortnight < Chronic::Repeater #:nodoc:
2
- FORTNIGHT_SECONDS = 1_209_600 # (14 * 24 * 60 * 60)
3
-
4
- def next(pointer)
5
- super
6
-
7
- if !@current_fortnight_start
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
+ @current_fortnight_start = nil
8
+ end
9
+
10
+ def next(pointer)
11
+ super
12
+
13
+ if !@current_fortnight_start
14
+ case pointer
15
+ when :future
16
+ sunday_repeater = RepeaterDayName.new(:sunday)
17
+ sunday_repeater.start = @now
18
+ next_sunday_span = sunday_repeater.next(:future)
19
+ @current_fortnight_start = next_sunday_span.begin
20
+ when :past
21
+ sunday_repeater = RepeaterDayName.new(:sunday)
22
+ sunday_repeater.start = (@now + RepeaterDay::DAY_SECONDS)
23
+ 2.times { sunday_repeater.next(:past) }
24
+ last_sunday_span = sunday_repeater.next(:past)
25
+ @current_fortnight_start = last_sunday_span.begin
26
+ end
27
+ else
28
+ direction = pointer == :future ? 1 : -1
29
+ @current_fortnight_start += direction * FORTNIGHT_SECONDS
30
+ end
31
+
32
+ Span.new(@current_fortnight_start, @current_fortnight_start + FORTNIGHT_SECONDS)
33
+ end
34
+
35
+ def this(pointer = :future)
36
+ super
37
+
38
+ pointer = :future if pointer == :none
39
+
8
40
  case pointer
9
41
  when :future
10
- sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
42
+ this_fortnight_start = Time.construct(@now.year, @now.month, @now.day, @now.hour) + RepeaterHour::HOUR_SECONDS
43
+ sunday_repeater = RepeaterDayName.new(:sunday)
11
44
  sunday_repeater.start = @now
12
- next_sunday_span = sunday_repeater.next(:future)
13
- @current_fortnight_start = next_sunday_span.begin
45
+ sunday_repeater.this(:future)
46
+ this_sunday_span = sunday_repeater.this(:future)
47
+ this_fortnight_end = this_sunday_span.begin
48
+ Span.new(this_fortnight_start, this_fortnight_end)
14
49
  when :past
15
- sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
16
- sunday_repeater.start = (@now + Chronic::RepeaterDay::DAY_SECONDS)
17
- 2.times { sunday_repeater.next(:past) }
50
+ this_fortnight_end = Time.construct(@now.year, @now.month, @now.day, @now.hour)
51
+ sunday_repeater = RepeaterDayName.new(:sunday)
52
+ sunday_repeater.start = @now
18
53
  last_sunday_span = sunday_repeater.next(:past)
19
- @current_fortnight_start = last_sunday_span.begin
54
+ this_fortnight_start = last_sunday_span.begin
55
+ Span.new(this_fortnight_start, this_fortnight_end)
20
56
  end
21
- else
57
+ end
58
+
59
+ def offset(span, amount, pointer)
22
60
  direction = pointer == :future ? 1 : -1
23
- @current_fortnight_start += direction * FORTNIGHT_SECONDS
61
+ span + direction * amount * FORTNIGHT_SECONDS
24
62
  end
25
-
26
- Chronic::Span.new(@current_fortnight_start, @current_fortnight_start + FORTNIGHT_SECONDS)
27
- end
28
-
29
- def this(pointer = :future)
30
- super
31
-
32
- pointer = :future if pointer == :none
33
-
34
- case pointer
35
- when :future
36
- this_fortnight_start = Time.construct(@now.year, @now.month, @now.day, @now.hour) + Chronic::RepeaterHour::HOUR_SECONDS
37
- sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
38
- sunday_repeater.start = @now
39
- sunday_repeater.this(:future)
40
- this_sunday_span = sunday_repeater.this(:future)
41
- this_fortnight_end = this_sunday_span.begin
42
- Chronic::Span.new(this_fortnight_start, this_fortnight_end)
43
- when :past
44
- this_fortnight_end = Time.construct(@now.year, @now.month, @now.day, @now.hour)
45
- sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
46
- sunday_repeater.start = @now
47
- last_sunday_span = sunday_repeater.next(:past)
48
- this_fortnight_start = last_sunday_span.begin
49
- Chronic::Span.new(this_fortnight_start, this_fortnight_end)
63
+
64
+ def width
65
+ FORTNIGHT_SECONDS
50
66
  end
51
- end
52
-
53
- def offset(span, amount, pointer)
54
- direction = pointer == :future ? 1 : -1
55
- span + direction * amount * FORTNIGHT_SECONDS
56
- end
57
67
 
58
- def width
59
- FORTNIGHT_SECONDS
60
- end
61
-
62
- def to_s
63
- super << '-fortnight'
68
+ def to_s
69
+ super << '-fortnight'
70
+ end
64
71
  end
65
72
  end
@@ -1,52 +1,59 @@
1
- class Chronic::RepeaterHour < Chronic::Repeater #:nodoc:
2
- HOUR_SECONDS = 3600 # 60 * 60
3
-
4
- def next(pointer)
5
- super
6
-
7
- if !@current_hour_start
1
+ module Chronic
2
+ class RepeaterHour < Repeater #:nodoc:
3
+ HOUR_SECONDS = 3600 # 60 * 60
4
+
5
+ def initialize(type)
6
+ super
7
+ @current_hour_start = nil
8
+ end
9
+
10
+ def next(pointer)
11
+ super
12
+
13
+ if !@current_hour_start
14
+ case pointer
15
+ when :future
16
+ @current_hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour + 1)
17
+ when :past
18
+ @current_hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour - 1)
19
+ end
20
+ else
21
+ direction = pointer == :future ? 1 : -1
22
+ @current_hour_start += direction * HOUR_SECONDS
23
+ end
24
+
25
+ Span.new(@current_hour_start, @current_hour_start + HOUR_SECONDS)
26
+ end
27
+
28
+ def this(pointer = :future)
29
+ super
30
+
8
31
  case pointer
9
32
  when :future
10
- @current_hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour + 1)
33
+ hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
34
+ hour_end = Time.construct(@now.year, @now.month, @now.day, @now.hour + 1)
11
35
  when :past
12
- @current_hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour - 1)
36
+ hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour)
37
+ hour_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
38
+ when :none
39
+ hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour)
40
+ hour_end = hour_start + HOUR_SECONDS
13
41
  end
14
- else
42
+
43
+ Span.new(hour_start, hour_end)
44
+ end
45
+
46
+ def offset(span, amount, pointer)
15
47
  direction = pointer == :future ? 1 : -1
16
- @current_hour_start += direction * HOUR_SECONDS
48
+ span + direction * amount * HOUR_SECONDS
17
49
  end
18
-
19
- Chronic::Span.new(@current_hour_start, @current_hour_start + HOUR_SECONDS)
20
- end
21
-
22
- def this(pointer = :future)
23
- super
24
-
25
- case pointer
26
- when :future
27
- hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
28
- hour_end = Time.construct(@now.year, @now.month, @now.day, @now.hour + 1)
29
- when :past
30
- hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour)
31
- hour_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
32
- when :none
33
- hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour)
34
- hour_end = hour_begin + HOUR_SECONDS
50
+
51
+ def width
52
+ HOUR_SECONDS
53
+ end
54
+
55
+ def to_s
56
+ super << '-hour'
35
57
  end
36
-
37
- Chronic::Span.new(hour_start, hour_end)
38
- end
39
-
40
- def offset(span, amount, pointer)
41
- direction = pointer == :future ? 1 : -1
42
- span + direction * amount * HOUR_SECONDS
43
- end
44
-
45
- def width
46
- HOUR_SECONDS
47
- end
48
-
49
- def to_s
50
- super << '-hour'
51
58
  end
52
59
  end
@@ -1,52 +1,59 @@
1
- class Chronic::RepeaterMinute < Chronic::Repeater #:nodoc:
2
- MINUTE_SECONDS = 60
3
-
4
- def next(pointer = :future)
5
- super
6
-
7
- if !@current_minute_start
1
+ module Chronic
2
+ class RepeaterMinute < Repeater #:nodoc:
3
+ MINUTE_SECONDS = 60
4
+
5
+ def initialize(type)
6
+ super
7
+ @current_minute_start = nil
8
+ end
9
+
10
+ def next(pointer = :future)
11
+ super
12
+
13
+ if !@current_minute_start
14
+ case pointer
15
+ when :future
16
+ @current_minute_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
17
+ when :past
18
+ @current_minute_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min - 1)
19
+ end
20
+ else
21
+ direction = pointer == :future ? 1 : -1
22
+ @current_minute_start += direction * MINUTE_SECONDS
23
+ end
24
+
25
+ Span.new(@current_minute_start, @current_minute_start + MINUTE_SECONDS)
26
+ end
27
+
28
+ def this(pointer = :future)
29
+ super
30
+
8
31
  case pointer
9
32
  when :future
10
- @current_minute_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
33
+ minute_begin = @now
34
+ minute_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
11
35
  when :past
12
- @current_minute_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min - 1)
36
+ minute_begin = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
37
+ minute_end = @now
38
+ when :none
39
+ minute_begin = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
40
+ minute_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min) + MINUTE_SECONDS
13
41
  end
14
- else
42
+
43
+ Span.new(minute_begin, minute_end)
44
+ end
45
+
46
+ def offset(span, amount, pointer)
15
47
  direction = pointer == :future ? 1 : -1
16
- @current_minute_start += direction * MINUTE_SECONDS
48
+ span + direction * amount * MINUTE_SECONDS
17
49
  end
18
-
19
- Chronic::Span.new(@current_minute_start, @current_minute_start + MINUTE_SECONDS)
20
- end
21
-
22
- def this(pointer = :future)
23
- super
24
-
25
- case pointer
26
- when :future
27
- minute_begin = @now
28
- minute_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
29
- when :past
30
- minute_begin = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
31
- minute_end = @now
32
- when :none
33
- minute_begin = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
34
- minute_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min) + MINUTE_SECONDS
50
+
51
+ def width
52
+ MINUTE_SECONDS
53
+ end
54
+
55
+ def to_s
56
+ super << '-minute'
35
57
  end
36
-
37
- Chronic::Span.new(minute_begin, minute_end)
38
- end
39
-
40
- def offset(span, amount, pointer)
41
- direction = pointer == :future ? 1 : -1
42
- span + direction * amount * MINUTE_SECONDS
43
- end
44
-
45
- def width
46
- MINUTE_SECONDS
47
- end
48
-
49
- def to_s
50
- super << '-minute'
51
58
  end
52
59
  end
@@ -1,61 +1,68 @@
1
- class Chronic::RepeaterMonth < Chronic::Repeater #:nodoc:
2
- MONTH_SECONDS = 2_592_000 # 30 * 24 * 60 * 60
3
- YEAR_MONTHS = 12
4
-
5
- def next(pointer)
6
- super
7
-
8
- if !@current_month_start
9
- @current_month_start = offset_by(Time.construct(@now.year, @now.month), 1, pointer)
10
- else
11
- @current_month_start = offset_by(Time.construct(@current_month_start.year, @current_month_start.month), 1, pointer)
1
+ module Chronic
2
+ class RepeaterMonth < Repeater #:nodoc:
3
+ MONTH_SECONDS = 2_592_000 # 30 * 24 * 60 * 60
4
+ YEAR_MONTHS = 12
5
+
6
+ def initialize(type)
7
+ super
8
+ @current_month_start = nil
12
9
  end
13
-
14
- Chronic::Span.new(@current_month_start, Time.construct(@current_month_start.year, @current_month_start.month + 1))
15
- end
16
-
17
- def this(pointer = :future)
18
- super
19
-
20
- case pointer
21
- when :future
22
- month_start = Time.construct(@now.year, @now.month, @now.day + 1)
23
- month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
24
- when :past
25
- month_start = Time.construct(@now.year, @now.month)
26
- month_end = Time.construct(@now.year, @now.month, @now.day)
27
- when :none
28
- month_start = Time.construct(@now.year, @now.month)
29
- month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
10
+
11
+ def next(pointer)
12
+ super
13
+
14
+ if !@current_month_start
15
+ @current_month_start = offset_by(Time.construct(@now.year, @now.month), 1, pointer)
16
+ else
17
+ @current_month_start = offset_by(Time.construct(@current_month_start.year, @current_month_start.month), 1, pointer)
18
+ end
19
+
20
+ Span.new(@current_month_start, Time.construct(@current_month_start.year, @current_month_start.month + 1))
30
21
  end
31
-
32
- Chronic::Span.new(month_start, month_end)
33
- end
34
-
35
- def offset(span, amount, pointer)
36
- Chronic::Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
37
- end
38
-
39
- def offset_by(time, amount, pointer)
40
- direction = pointer == :future ? 1 : -1
41
-
42
- amount_years = direction * amount / YEAR_MONTHS
43
- amount_months = direction * amount % YEAR_MONTHS
44
-
45
- new_year = time.year + amount_years
46
- new_month = time.month + amount_months
47
- if new_month > YEAR_MONTHS
48
- new_year += 1
49
- new_month -= YEAR_MONTHS
22
+
23
+ def this(pointer = :future)
24
+ super
25
+
26
+ case pointer
27
+ when :future
28
+ month_start = Time.construct(@now.year, @now.month, @now.day + 1)
29
+ month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
30
+ when :past
31
+ month_start = Time.construct(@now.year, @now.month)
32
+ month_end = Time.construct(@now.year, @now.month, @now.day)
33
+ when :none
34
+ month_start = Time.construct(@now.year, @now.month)
35
+ month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
36
+ end
37
+
38
+ Span.new(month_start, month_end)
39
+ end
40
+
41
+ def offset(span, amount, pointer)
42
+ Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
43
+ end
44
+
45
+ def offset_by(time, amount, pointer)
46
+ direction = pointer == :future ? 1 : -1
47
+
48
+ amount_years = direction * amount / YEAR_MONTHS
49
+ amount_months = direction * amount % YEAR_MONTHS
50
+
51
+ new_year = time.year + amount_years
52
+ new_month = time.month + amount_months
53
+ if new_month > YEAR_MONTHS
54
+ new_year += 1
55
+ new_month -= YEAR_MONTHS
56
+ end
57
+ Time.construct(new_year, new_month, time.day, time.hour, time.min, time.sec)
58
+ end
59
+
60
+ def width
61
+ MONTH_SECONDS
62
+ end
63
+
64
+ def to_s
65
+ super << '-month'
50
66
  end
51
- Time.construct(new_year, new_month, time.day, time.hour, time.min, time.sec)
52
- end
53
-
54
- def width
55
- MONTH_SECONDS
56
- end
57
-
58
- def to_s
59
- super << '-month'
60
67
  end
61
68
  end