chronic 0.2.2 → 0.10.2

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 (76) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.travis.yml +8 -0
  4. data/HISTORY.md +243 -0
  5. data/LICENSE +21 -0
  6. data/README.md +182 -0
  7. data/Rakefile +62 -15
  8. data/chronic.gemspec +23 -0
  9. data/lib/chronic/date.rb +82 -0
  10. data/lib/chronic/grabber.rb +24 -17
  11. data/lib/chronic/handler.rb +97 -0
  12. data/lib/chronic/handlers.rb +490 -312
  13. data/lib/chronic/mini_date.rb +38 -0
  14. data/lib/chronic/numerizer.rb +130 -0
  15. data/lib/chronic/ordinal.rb +33 -24
  16. data/lib/chronic/parser.rb +268 -0
  17. data/lib/chronic/pointer.rb +22 -17
  18. data/lib/chronic/repeater.rb +137 -107
  19. data/lib/chronic/repeaters/repeater_day.rb +51 -44
  20. data/lib/chronic/repeaters/repeater_day_name.rb +50 -43
  21. data/lib/chronic/repeaters/repeater_day_portion.rb +97 -81
  22. data/lib/chronic/repeaters/repeater_fortnight.rb +61 -54
  23. data/lib/chronic/repeaters/repeater_hour.rb +51 -44
  24. data/lib/chronic/repeaters/repeater_minute.rb +51 -44
  25. data/lib/chronic/repeaters/repeater_month.rb +79 -60
  26. data/lib/chronic/repeaters/repeater_month_name.rb +85 -83
  27. data/lib/chronic/repeaters/repeater_season.rb +110 -22
  28. data/lib/chronic/repeaters/repeater_season_name.rb +41 -22
  29. data/lib/chronic/repeaters/repeater_second.rb +41 -34
  30. data/lib/chronic/repeaters/repeater_time.rb +128 -104
  31. data/lib/chronic/repeaters/repeater_week.rb +65 -58
  32. data/lib/chronic/repeaters/repeater_weekday.rb +86 -0
  33. data/lib/chronic/repeaters/repeater_weekend.rb +59 -52
  34. data/lib/chronic/repeaters/repeater_year.rb +72 -52
  35. data/lib/chronic/scalar.rb +53 -46
  36. data/lib/chronic/season.rb +26 -0
  37. data/lib/chronic/separator.rb +174 -43
  38. data/lib/chronic/sign.rb +49 -0
  39. data/lib/chronic/span.rb +31 -0
  40. data/lib/chronic/tag.rb +37 -0
  41. data/lib/chronic/time.rb +40 -0
  42. data/lib/chronic/time_zone.rb +21 -11
  43. data/lib/chronic/token.rb +51 -0
  44. data/lib/chronic.rb +94 -69
  45. data/test/helper.rb +12 -0
  46. data/test/test_chronic.rb +183 -0
  47. data/test/test_daylight_savings.rb +118 -0
  48. data/test/{test_Handler.rb → test_handler.rb} +73 -55
  49. data/test/test_mini_date.rb +32 -0
  50. data/test/test_numerizer.rb +86 -0
  51. data/test/test_parsing.rb +891 -248
  52. data/test/{test_RepeaterDayName.rb → test_repeater_day_name.rb} +16 -17
  53. data/test/test_repeater_day_portion.rb +254 -0
  54. data/test/{test_RepeaterFortnight.rb → test_repeater_fortnight.rb} +17 -18
  55. data/test/{test_RepeaterHour.rb → test_repeater_hour.rb} +23 -20
  56. data/test/test_repeater_minute.rb +34 -0
  57. data/test/{test_RepeaterMonth.rb → test_repeater_month.rb} +21 -18
  58. data/test/{test_RepeaterMonthName.rb → test_repeater_month_name.rb} +18 -19
  59. data/test/test_repeater_season.rb +40 -0
  60. data/test/{test_RepeaterTime.rb → test_repeater_time.rb} +39 -23
  61. data/test/{test_RepeaterWeek.rb → test_repeater_week.rb} +17 -18
  62. data/test/test_repeater_weekday.rb +55 -0
  63. data/test/{test_RepeaterWeekend.rb → test_repeater_weekend.rb} +22 -23
  64. data/test/{test_RepeaterYear.rb → test_repeater_year.rb} +25 -19
  65. data/test/{test_Span.rb → test_span.rb} +7 -8
  66. data/test/{test_Token.rb → test_token.rb} +6 -7
  67. metadata +163 -85
  68. data/History.txt +0 -49
  69. data/Manifest.txt +0 -47
  70. data/README.txt +0 -149
  71. data/lib/chronic/chronic.rb +0 -239
  72. data/lib/numerizer/numerizer.rb +0 -103
  73. data/test/suite.rb +0 -9
  74. data/test/test_Chronic.rb +0 -50
  75. data/test/test_Numerizer.rb +0 -48
  76. data/test/test_Time.rb +0 -50
@@ -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, options = {})
6
+ super
7
+ @current_fortnight_start = nil
8
+ end
9
+
10
+ def next(pointer)
11
+ super
12
+
13
+ unless @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 = Chronic.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 = Chronic.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
- end
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, options = {})
6
+ super
7
+ @current_hour_start = nil
8
+ end
9
+
10
+ def next(pointer)
11
+ super
12
+
13
+ unless @current_hour_start
14
+ case pointer
15
+ when :future
16
+ @current_hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour + 1)
17
+ when :past
18
+ @current_hour_start = Chronic.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 = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
34
+ hour_end = Chronic.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 = Chronic.construct(@now.year, @now.month, @now.day, @now.hour)
37
+ hour_end = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
38
+ when :none
39
+ hour_start = Chronic.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
- end
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, options = {})
6
+ super
7
+ @current_minute_start = nil
8
+ end
9
+
10
+ def next(pointer = :future)
11
+ super
12
+
13
+ unless @current_minute_start
14
+ case pointer
15
+ when :future
16
+ @current_minute_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
17
+ when :past
18
+ @current_minute_start = Chronic.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 = Chronic.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 = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
37
+ minute_end = @now
38
+ when :none
39
+ minute_begin = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
40
+ minute_end = Chronic.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
- end
59
+ end
@@ -1,61 +1,80 @@
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)
12
- 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)
30
- 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
50
- 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'
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, options = {})
9
+ super
10
+ @current_month_start = nil
11
+ end
12
+
13
+ def next(pointer)
14
+ super
15
+
16
+ unless @current_month_start
17
+ @current_month_start = offset_by(Chronic.construct(@now.year, @now.month), 1, pointer)
18
+ else
19
+ @current_month_start = offset_by(Chronic.construct(@current_month_start.year, @current_month_start.month), 1, pointer)
20
+ end
21
+
22
+ Span.new(@current_month_start, Chronic.construct(@current_month_start.year, @current_month_start.month + 1))
23
+ end
24
+
25
+ def this(pointer = :future)
26
+ super
27
+
28
+ case pointer
29
+ when :future
30
+ month_start = Chronic.construct(@now.year, @now.month, @now.day + 1)
31
+ month_end = self.offset_by(Chronic.construct(@now.year, @now.month), 1, :future)
32
+ when :past
33
+ month_start = Chronic.construct(@now.year, @now.month)
34
+ month_end = Chronic.construct(@now.year, @now.month, @now.day)
35
+ when :none
36
+ month_start = Chronic.construct(@now.year, @now.month)
37
+ month_end = self.offset_by(Chronic.construct(@now.year, @now.month), 1, :future)
38
+ end
39
+
40
+ Span.new(month_start, month_end)
41
+ end
42
+
43
+ def offset(span, amount, pointer)
44
+ Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
45
+ end
46
+
47
+ def offset_by(time, amount, pointer)
48
+ direction = pointer == :future ? 1 : -1
49
+
50
+ amount_years = direction * amount / YEAR_MONTHS
51
+ amount_months = direction * amount % YEAR_MONTHS
52
+
53
+ new_year = time.year + amount_years
54
+ new_month = time.month + amount_months
55
+ if new_month > YEAR_MONTHS
56
+ new_year += 1
57
+ new_month -= YEAR_MONTHS
58
+ end
59
+
60
+ days = month_days(new_year, new_month)
61
+ new_day = time.day > days ? days : time.day
62
+
63
+ Chronic.construct(new_year, new_month, new_day, time.hour, time.min, time.sec)
64
+ end
65
+
66
+ def width
67
+ MONTH_SECONDS
68
+ end
69
+
70
+ def to_s
71
+ super << '-month'
72
+ end
73
+
74
+ private
75
+
76
+ def month_days(year, month)
77
+ ::Date.leap?(year) ? MONTH_DAYS_LEAP[month - 1] : MONTH_DAYS[month - 1]
78
+ end
60
79
  end
61
- end
80
+ end
@@ -1,93 +1,95 @@
1
- class Chronic::RepeaterMonthName < Chronic::Repeater #:nodoc:
2
- MONTH_SECONDS = 2_592_000 # 30 * 24 * 60 * 60
3
-
4
- def next(pointer)
5
- super
6
-
7
- if !@current_month_begin
8
- target_month = symbol_to_number(@type)
9
- case pointer
10
- when :future
11
- if @now.month < target_month
12
- @current_month_begin = Time.construct(@now.year, target_month)
13
- else @now.month > target_month
14
- @current_month_begin = Time.construct(@now.year + 1, target_month)
15
- end
16
- when :none
17
- if @now.month <= target_month
18
- @current_month_begin = Time.construct(@now.year, target_month)
19
- else @now.month > target_month
20
- @current_month_begin = Time.construct(@now.year + 1, target_month)
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, options = {})
20
+ super
21
+ @current_month_begin = nil
22
+ end
23
+
24
+ def next(pointer)
25
+ super
26
+
27
+ unless @current_month_begin
28
+ case pointer
29
+ when :future
30
+ if @now.month < index
31
+ @current_month_begin = Chronic.construct(@now.year, index)
32
+ elsif @now.month > index
33
+ @current_month_begin = Chronic.construct(@now.year + 1, index)
34
+ end
35
+ when :none
36
+ if @now.month <= index
37
+ @current_month_begin = Chronic.construct(@now.year, index)
38
+ elsif @now.month > index
39
+ @current_month_begin = Chronic.construct(@now.year + 1, index)
40
+ end
41
+ when :past
42
+ if @now.month >= index
43
+ @current_month_begin = Chronic.construct(@now.year, index)
44
+ elsif @now.month < index
45
+ @current_month_begin = Chronic.construct(@now.year - 1, index)
46
+ end
21
47
  end
22
- when :past
23
- if @now.month > target_month
24
- @current_month_begin = Time.construct(@now.year, target_month)
25
- else @now.month < target_month
26
- @current_month_begin = Time.construct(@now.year - 1, target_month)
48
+ @current_month_begin || raise("Current month should be set by now")
49
+ else
50
+ case pointer
51
+ when :future
52
+ @current_month_begin = Chronic.construct(@current_month_begin.year + 1, @current_month_begin.month)
53
+ when :past
54
+ @current_month_begin = Chronic.construct(@current_month_begin.year - 1, @current_month_begin.month)
27
55
  end
28
56
  end
29
- @current_month_begin || raise("Current month should be set by now")
30
- else
57
+
58
+ cur_month_year = @current_month_begin.year
59
+ cur_month_month = @current_month_begin.month
60
+
61
+ if cur_month_month == 12
62
+ next_month_year = cur_month_year + 1
63
+ next_month_month = 1
64
+ else
65
+ next_month_year = cur_month_year
66
+ next_month_month = cur_month_month + 1
67
+ end
68
+
69
+ Span.new(@current_month_begin, Chronic.construct(next_month_year, next_month_month))
70
+ end
71
+
72
+ def this(pointer = :future)
73
+ super
74
+
31
75
  case pointer
32
- when :future
33
- @current_month_begin = Time.construct(@current_month_begin.year + 1, @current_month_begin.month)
34
76
  when :past
35
- @current_month_begin = Time.construct(@current_month_begin.year - 1, @current_month_begin.month)
77
+ self.next(pointer)
78
+ when :future, :none
79
+ self.next(:none)
36
80
  end
37
81
  end
38
-
39
- cur_month_year = @current_month_begin.year
40
- cur_month_month = @current_month_begin.month
41
-
42
- if cur_month_month == 12
43
- next_month_year = cur_month_year + 1
44
- next_month_month = 1
45
- else
46
- next_month_year = cur_month_year
47
- next_month_month = cur_month_month + 1
82
+
83
+ def width
84
+ MONTH_SECONDS
48
85
  end
49
-
50
- Chronic::Span.new(@current_month_begin, Time.construct(next_month_year, next_month_month))
51
- end
52
-
53
- def this(pointer = :future)
54
- super
55
-
56
- case pointer
57
- when :past
58
- self.next(pointer)
59
- when :future, :none
60
- self.next(:none)
86
+
87
+ def index
88
+ @index ||= MONTHS[@type]
89
+ end
90
+
91
+ def to_s
92
+ super << '-monthname-' << @type.to_s
61
93
  end
62
94
  end
63
-
64
- def width
65
- MONTH_SECONDS
66
- end
67
-
68
- def index
69
- symbol_to_number(@type)
70
- end
71
-
72
- def to_s
73
- super << '-monthname-' << @type.to_s
74
- end
75
-
76
- private
77
-
78
- def symbol_to_number(sym)
79
- lookup = {:january => 1,
80
- :february => 2,
81
- :march => 3,
82
- :april => 4,
83
- :may => 5,
84
- :june => 6,
85
- :july => 7,
86
- :august => 8,
87
- :september => 9,
88
- :october => 10,
89
- :november => 11,
90
- :december => 12}
91
- lookup[sym] || raise("Invalid symbol specified")
92
- end
93
- end
95
+ end