pangel-chronic 0.3.0.3 → 0.3.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/{README.rdoc → README.txt} +7 -22
  2. data/lib/chronic.rb +89 -12
  3. data/lib/chronic/chronic.rb +260 -301
  4. data/lib/chronic/grabber.rb +23 -23
  5. data/lib/chronic/handlers.rb +538 -557
  6. data/lib/chronic/ordinal.rb +36 -35
  7. data/lib/chronic/pointer.rb +24 -26
  8. data/lib/chronic/repeater.rb +128 -138
  9. data/lib/chronic/repeaters/repeater_day.rb +51 -51
  10. data/lib/chronic/repeaters/repeater_day_name.rb +50 -52
  11. data/lib/chronic/repeaters/repeater_day_portion.rb +93 -93
  12. data/lib/chronic/repeaters/repeater_fortnight.rb +66 -66
  13. data/lib/chronic/repeaters/repeater_hour.rb +56 -57
  14. data/lib/chronic/repeaters/repeater_minute.rb +56 -56
  15. data/lib/chronic/repeaters/repeater_month.rb +71 -62
  16. data/lib/chronic/repeaters/repeater_month_name.rb +95 -95
  17. data/lib/chronic/repeaters/repeater_season.rb +142 -142
  18. data/lib/chronic/repeaters/repeater_season_name.rb +42 -42
  19. data/lib/chronic/repeaters/repeater_second.rb +40 -40
  20. data/lib/chronic/repeaters/repeater_time.rb +124 -123
  21. data/lib/chronic/repeaters/repeater_week.rb +70 -70
  22. data/lib/chronic/repeaters/repeater_weekday.rb +76 -76
  23. data/lib/chronic/repeaters/repeater_weekend.rb +63 -63
  24. data/lib/chronic/repeaters/repeater_year.rb +63 -63
  25. data/lib/chronic/scalar.rb +89 -70
  26. data/lib/chronic/separator.rb +88 -88
  27. data/lib/chronic/time_zone.rb +23 -20
  28. data/lib/numerizer/numerizer.rb +93 -94
  29. data/test/suite.rb +2 -2
  30. data/test/test_Chronic.rb +47 -47
  31. data/test/test_Handler.rb +106 -106
  32. data/test/test_Numerizer.rb +47 -49
  33. data/test/test_RepeaterDayName.rb +48 -48
  34. data/test/test_RepeaterFortnight.rb +59 -59
  35. data/test/test_RepeaterHour.rb +61 -64
  36. data/test/test_RepeaterMonth.rb +43 -43
  37. data/test/test_RepeaterMonthName.rb +53 -53
  38. data/test/test_RepeaterTime.rb +68 -68
  39. data/test/test_RepeaterWeek.rb +59 -59
  40. data/test/test_RepeaterWeekday.rb +53 -53
  41. data/test/test_RepeaterWeekend.rb +71 -71
  42. data/test/test_RepeaterYear.rb +59 -59
  43. data/test/test_Span.rb +19 -28
  44. data/test/test_Time.rb +46 -46
  45. data/test/test_Token.rb +22 -22
  46. data/test/test_parsing.rb +726 -792
  47. metadata +6 -10
@@ -1,45 +1,45 @@
1
1
  require 'chronic/repeaters/repeater_season.rb'
2
2
 
3
3
  class Chronic::RepeaterSeasonName < Chronic::RepeaterSeason #:nodoc:
4
- SEASON_SECONDS = 7_862_400 # 91 * 24 * 60 * 60
5
- DAY_SECONDS = 86_400 # (24 * 60 * 60)
6
-
7
- def next(pointer)
8
- direction = pointer == :future ? 1 : -1
9
- find_next_season_span(direction, @type)
10
- end
11
-
12
- def this(pointer = :future)
13
- # super
14
-
15
- direction = pointer == :future ? 1 : -1
16
-
17
- today = Time.construct(@now.year, @now.month, @now.day)
18
- goal_ssn_start = today + direction * num_seconds_til_start(@type, direction)
19
- goal_ssn_end = today + direction * num_seconds_til_end(@type, direction)
20
- curr_ssn = find_current_season(@now.to_minidate)
21
- case pointer
22
- when :past
23
- this_ssn_start = goal_ssn_start
24
- this_ssn_end = (curr_ssn == @type) ? today : goal_ssn_end
25
- when :future
26
- this_ssn_start = (curr_ssn == @type) ? today + Chronic::RepeaterDay::DAY_SECONDS : goal_ssn_start
27
- this_ssn_end = goal_ssn_end
28
- when :none
29
- this_ssn_start = goal_ssn_start
30
- this_ssn_end = goal_ssn_end
31
- end
32
-
33
- Chronic::Span.new(this_ssn_start, this_ssn_end)
34
- end
35
-
36
- def offset(span, amount, pointer)
37
- Chronic::Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
38
- end
39
-
40
- def offset_by(time, amount, pointer)
41
- direction = pointer == :future ? 1 : -1
42
- time + amount * direction * Chronic::RepeaterYear::YEAR_SECONDS
43
- end
44
-
45
- end
4
+ SEASON_SECONDS = 7_862_400 # 91 * 24 * 60 * 60
5
+ DAY_SECONDS = 86_400 # (24 * 60 * 60)
6
+
7
+ def next(pointer)
8
+ direction = pointer == :future ? 1 : -1
9
+ find_next_season_span(direction, @type)
10
+ end
11
+
12
+ def this(pointer = :future)
13
+ # super
14
+
15
+ direction = pointer == :future ? 1 : -1
16
+
17
+ today = Time.construct(@now.year, @now.month, @now.day)
18
+ goal_ssn_start = today + direction * num_seconds_til_start(@type, direction)
19
+ goal_ssn_end = today + direction * num_seconds_til_end(@type, direction)
20
+ curr_ssn = find_current_season(@now.to_minidate)
21
+ case pointer
22
+ when :past
23
+ this_ssn_start = goal_ssn_start
24
+ this_ssn_end = (curr_ssn == @type) ? today : goal_ssn_end
25
+ when :future
26
+ this_ssn_start = (curr_ssn == @type) ? today + Chronic::RepeaterDay::DAY_SECONDS : goal_ssn_start
27
+ this_ssn_end = goal_ssn_end
28
+ when :none
29
+ this_ssn_start = goal_ssn_start
30
+ this_ssn_end = goal_ssn_end
31
+ end
32
+
33
+ Chronic::Span.new(this_ssn_start, this_ssn_end)
34
+ end
35
+
36
+ def offset(span, amount, pointer)
37
+ Chronic::Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
38
+ end
39
+
40
+ def offset_by(time, amount, pointer)
41
+ direction = pointer == :future ? 1 : -1
42
+ time + amount * direction * Chronic::RepeaterYear::YEAR_SECONDS
43
+ end
44
+
45
+ end
@@ -1,41 +1,41 @@
1
1
  class Chronic::RepeaterSecond < Chronic::Repeater #:nodoc:
2
- SECOND_SECONDS = 1 # haha, awesome
3
-
4
- def initialize(type)
5
- super
6
- @second_start = nil
7
- end
8
-
9
- def next(pointer = :future)
10
- super
11
-
12
- direction = pointer == :future ? 1 : -1
13
-
14
- if !@second_start
15
- @second_start = @now + (direction * SECOND_SECONDS)
16
- else
17
- @second_start += SECOND_SECONDS * direction
18
- end
19
-
20
- Chronic::Span.new(@second_start, @second_start + SECOND_SECONDS)
21
- end
22
-
23
- def this(pointer = :future)
24
- super
25
-
26
- Chronic::Span.new(@now, @now + 1)
27
- end
28
-
29
- def offset(span, amount, pointer)
30
- direction = pointer == :future ? 1 : -1
31
- span + direction * amount * SECOND_SECONDS
32
- end
33
-
34
- def width
35
- SECOND_SECONDS
36
- end
37
-
38
- def to_s
39
- super << '-second'
40
- end
41
- end
2
+ SECOND_SECONDS = 1 # haha, awesome
3
+
4
+ def initialize(type)
5
+ super
6
+ @second_start = nil
7
+ end
8
+
9
+ def next(pointer = :future)
10
+ super
11
+
12
+ direction = pointer == :future ? 1 : -1
13
+
14
+ if !@second_start
15
+ @second_start = @now + (direction * SECOND_SECONDS)
16
+ else
17
+ @second_start += SECOND_SECONDS * direction
18
+ end
19
+
20
+ Chronic::Span.new(@second_start, @second_start + SECOND_SECONDS)
21
+ end
22
+
23
+ def this(pointer = :future)
24
+ super
25
+
26
+ Chronic::Span.new(@now, @now + 1)
27
+ end
28
+
29
+ def offset(span, amount, pointer)
30
+ direction = pointer == :future ? 1 : -1
31
+ span + direction * amount * SECOND_SECONDS
32
+ end
33
+
34
+ def width
35
+ SECOND_SECONDS
36
+ end
37
+
38
+ def to_s
39
+ super << '-second'
40
+ end
41
+ end
@@ -1,124 +1,125 @@
1
1
  class Chronic::RepeaterTime < Chronic::Repeater #:nodoc:
2
- class Tick #:nodoc:
3
- attr_accessor :time
4
-
5
- def initialize(time, ambiguous = false)
6
- @time = time
7
- @ambiguous = ambiguous
8
- end
9
-
10
- def ambiguous?
11
- @ambiguous
12
- end
13
-
14
- def *(other)
15
- Tick.new(@time * other, @ambiguous)
16
- end
17
-
18
- def to_f
19
- @time.to_f
20
- end
21
-
22
- def to_s
23
- @time.to_s + (@ambiguous ? '?' : '')
24
- end
25
- end
26
-
27
- def initialize(time, options = {})
28
- @current_time = nil
29
- t = time.gsub(/\:/, '')
30
-
31
- @type =
32
- case t.size
33
- when 1..2
34
- hours = t.to_i
35
- hours == 12 ? Tick.new(0 * 60 * 60, true) : Tick.new(hours * 60 * 60, true)
36
- when 3
37
- Tick.new((t[0..0].to_i * 60 * 60) + (t[1..2].to_i * 60), true)
38
- when 4
39
- ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
40
- hours = t[0..1].to_i
41
- hours == 12 ? Tick.new(0 * 60 * 60 + t[2..3].to_i * 60, ambiguous) : Tick.new(hours * 60 * 60 + t[2..3].to_i * 60, ambiguous)
42
- when 5
43
- Tick.new(t[0..0].to_i * 60 * 60 + t[1..2].to_i * 60 + t[3..4].to_i, true)
44
- when 6
45
- ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
46
- hours = t[0..1].to_i
47
- hours == 12 ? Tick.new(0 * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous) : Tick.new(hours * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous)
48
- else
49
- raise("Time cannot exceed six digits")
50
- end
51
- end
52
-
53
- # Return the next past or future Span for the time that this Repeater represents
54
- # pointer - Symbol representing which temporal direction to fetch the next day
55
- # must be either :past or :future
56
- def next(pointer)
57
- super
58
-
59
- half_day = 60 * 60 * 12
60
- full_day = 60 * 60 * 24
61
-
62
- first = false
63
-
64
- unless @current_time
65
- first = true
66
- midnight = Chronic.time_class.local(@now.year, @now.month, @now.day)
67
-
68
- yesterday_midnight = midnight - full_day
69
- tomorrow_midnight = midnight + full_day
70
-
71
- offset_fix = midnight.gmt_offset - tomorrow_midnight.gmt_offset
72
- tomorrow_midnight += offset_fix
73
-
74
- catch :done do
75
- if pointer == :future
76
- if @type.ambiguous?
77
- [midnight + @type + offset_fix, midnight + half_day + @type + offset_fix, tomorrow_midnight + @type].each do |t|
78
- (@current_time = t; throw :done) if t >= @now
79
- end
80
- else
81
- [midnight + @type + offset_fix, tomorrow_midnight + @type].each do |t|
82
- (@current_time = t; throw :done) if t >= @now
83
- end
84
- end
85
- else # pointer == :past
86
- if @type.ambiguous?
87
- [midnight + half_day + @type + offset_fix, midnight + @type + offset_fix, yesterday_midnight + @type + half_day].each do |t|
88
- (@current_time = t; throw :done) if t <= @now
89
- end
90
- else
91
- [midnight + @type + offset_fix, yesterday_midnight + @type].each do |t|
92
- (@current_time = t; throw :done) if t <= @now
93
- end
94
- end
95
- end
96
- end
97
-
98
- @current_time || raise("Current time cannot be nil at this point")
99
- end
100
-
101
- unless first
102
- increment = @type.ambiguous? ? half_day : full_day
103
- @current_time += pointer == :future ? increment : -increment
104
- end
105
-
106
- Chronic::Span.new(@current_time, @current_time + width)
107
- end
108
-
109
- def this(context = :future)
110
- super
111
-
112
- context = :future if context == :none
113
-
114
- self.next(context)
115
- end
116
-
117
- def width
118
- 1
119
- end
120
-
121
- def to_s
122
- super << '-time-' << @type.to_s
123
- end
124
- end
2
+ class Tick #:nodoc:
3
+ attr_accessor :time
4
+
5
+ def initialize(time, ambiguous = false)
6
+ @time = time
7
+ @ambiguous = ambiguous
8
+ end
9
+
10
+ def ambiguous?
11
+ @ambiguous
12
+ end
13
+
14
+ def *(other)
15
+ Tick.new(@time * other, @ambiguous)
16
+ end
17
+
18
+ def to_f
19
+ @time.to_f
20
+ end
21
+
22
+ def to_s
23
+ @time.to_s + (@ambiguous ? '?' : '')
24
+ end
25
+
26
+ end
27
+
28
+ def initialize(time, options = {})
29
+ @current_time = nil
30
+ t = time.gsub(/\:/, '')
31
+
32
+ @type =
33
+ case t.size
34
+ when 1..2
35
+ hours = t.to_i
36
+ hours == 12 ? Tick.new(0 * 60 * 60, true) : Tick.new(hours * 60 * 60, true)
37
+ when 3
38
+ Tick.new((t[0..0].to_i * 60 * 60) + (t[1..2].to_i * 60), true)
39
+ when 4
40
+ ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
41
+ hours = t[0..1].to_i
42
+ (hours == 12 && time =~ /:/) ? Tick.new(0 * 60 * 60 + t[2..3].to_i * 60, ambiguous) : Tick.new(hours * 60 * 60 + t[2..3].to_i * 60, ambiguous)
43
+ when 5
44
+ Tick.new(t[0..0].to_i * 60 * 60 + t[1..2].to_i * 60 + t[3..4].to_i, true)
45
+ when 6
46
+ ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
47
+ hours = t[0..1].to_i
48
+ hours == 12 ? Tick.new(0 * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous) : Tick.new(hours * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous)
49
+ else
50
+ raise("Time cannot exceed six digits")
51
+ end
52
+ end
53
+
54
+ # Return the next past or future Span for the time that this Repeater represents
55
+ # pointer - Symbol representing which temporal direction to fetch the next day
56
+ # must be either :past or :future
57
+ def next(pointer)
58
+ super
59
+
60
+ half_day = 60 * 60 * 12
61
+ full_day = 60 * 60 * 24
62
+
63
+ first = false
64
+
65
+ unless @current_time
66
+ first = true
67
+ midnight = Chronic.time_class.local(@now.year, @now.month, @now.day)
68
+
69
+ yesterday_midnight = midnight - full_day
70
+ tomorrow_midnight = midnight + full_day
71
+
72
+ offset_fix = midnight.gmt_offset - tomorrow_midnight.gmt_offset
73
+ tomorrow_midnight += offset_fix
74
+
75
+ catch :done do
76
+ if pointer == :future
77
+ if @type.ambiguous?
78
+ [midnight + @type.time + offset_fix, midnight + half_day + @type.time + offset_fix, tomorrow_midnight + @type.time].each do |t|
79
+ (@current_time = t; throw :done) if t >= @now
80
+ end
81
+ else
82
+ [midnight + @type.time + offset_fix, tomorrow_midnight + @type.time].each do |t|
83
+ (@current_time = t; throw :done) if t >= @now
84
+ end
85
+ end
86
+ else # pointer == :past
87
+ if @type.ambiguous?
88
+ [midnight + half_day + @type.time + offset_fix, midnight + @type.time + offset_fix, yesterday_midnight + @type.time + half_day].each do |t|
89
+ (@current_time = t; throw :done) if t <= @now
90
+ end
91
+ else
92
+ [midnight + @type.time + offset_fix, yesterday_midnight + @type.time].each do |t|
93
+ (@current_time = t; throw :done) if t <= @now
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ @current_time || raise("Current time cannot be nil at this point")
100
+ end
101
+
102
+ unless first
103
+ increment = @type.ambiguous? ? half_day : full_day
104
+ @current_time += pointer == :future ? increment : -increment
105
+ end
106
+
107
+ Chronic::Span.new(@current_time, @current_time + width)
108
+ end
109
+
110
+ def this(context = :future)
111
+ super
112
+
113
+ context = :future if context == :none
114
+
115
+ self.next(context)
116
+ end
117
+
118
+ def width
119
+ 1
120
+ end
121
+
122
+ def to_s
123
+ super << '-time-' << @type.to_s
124
+ end
125
+ end
@@ -1,73 +1,73 @@
1
1
  class Chronic::RepeaterWeek < Chronic::Repeater #:nodoc:
2
- WEEK_SECONDS = 604800 # (7 * 24 * 60 * 60)
2
+ WEEK_SECONDS = 604800 # (7 * 24 * 60 * 60)
3
3
 
4
- def initialize(type)
5
- super
6
- @current_week_start = nil
7
- end
4
+ def initialize(type)
5
+ super
6
+ @current_week_start = nil
7
+ end
8
8
 
9
- def next(pointer)
10
- super
11
-
12
- if !@current_week_start
13
- case pointer
14
- when :future
15
- sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
16
- sunday_repeater.start = @now
17
- next_sunday_span = sunday_repeater.next(:future)
18
- @current_week_start = next_sunday_span.begin
19
- when :past
20
- sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
21
- sunday_repeater.start = (@now + Chronic::RepeaterDay::DAY_SECONDS)
22
- sunday_repeater.next(:past)
23
- last_sunday_span = sunday_repeater.next(:past)
24
- @current_week_start = last_sunday_span.begin
25
- end
26
- else
27
- direction = pointer == :future ? 1 : -1
28
- @current_week_start += direction * WEEK_SECONDS
29
- end
30
-
31
- Chronic::Span.new(@current_week_start, @current_week_start + WEEK_SECONDS)
32
- end
33
-
34
- def this(pointer = :future)
35
- super
36
-
37
- case pointer
38
- when :future
39
- this_week_start = Chronic.time_class.local(@now.year, @now.month, @now.day, @now.hour) + Chronic::RepeaterHour::HOUR_SECONDS
40
- sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
41
- sunday_repeater.start = @now
42
- this_sunday_span = sunday_repeater.this(:future)
43
- this_week_end = this_sunday_span.begin
44
- Chronic::Span.new(this_week_start, this_week_end)
45
- when :past
46
- this_week_end = Chronic.time_class.local(@now.year, @now.month, @now.day, @now.hour)
47
- sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
48
- sunday_repeater.start = @now
49
- last_sunday_span = sunday_repeater.next(:past)
50
- this_week_start = last_sunday_span.begin
51
- Chronic::Span.new(this_week_start, this_week_end)
52
- when :none
53
- sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
54
- sunday_repeater.start = @now
55
- last_sunday_span = sunday_repeater.next(:past)
56
- this_week_start = last_sunday_span.begin
57
- Chronic::Span.new(this_week_start, this_week_start + WEEK_SECONDS)
58
- end
59
- end
60
-
61
- def offset(span, amount, pointer)
62
- direction = pointer == :future ? 1 : -1
63
- span + direction * amount * WEEK_SECONDS
64
- end
65
-
66
- def width
67
- WEEK_SECONDS
68
- end
69
-
70
- def to_s
71
- super << '-week'
72
- end
73
- end
9
+ def next(pointer)
10
+ super
11
+
12
+ if !@current_week_start
13
+ case pointer
14
+ when :future
15
+ sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
16
+ sunday_repeater.start = @now
17
+ next_sunday_span = sunday_repeater.next(:future)
18
+ @current_week_start = next_sunday_span.begin
19
+ when :past
20
+ sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
21
+ sunday_repeater.start = (@now + Chronic::RepeaterDay::DAY_SECONDS)
22
+ sunday_repeater.next(:past)
23
+ last_sunday_span = sunday_repeater.next(:past)
24
+ @current_week_start = last_sunday_span.begin
25
+ end
26
+ else
27
+ direction = pointer == :future ? 1 : -1
28
+ @current_week_start += direction * WEEK_SECONDS
29
+ end
30
+
31
+ Chronic::Span.new(@current_week_start, @current_week_start + WEEK_SECONDS)
32
+ end
33
+
34
+ def this(pointer = :future)
35
+ super
36
+
37
+ case pointer
38
+ when :future
39
+ this_week_start = Chronic.time_class.local(@now.year, @now.month, @now.day, @now.hour) + Chronic::RepeaterHour::HOUR_SECONDS
40
+ sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
41
+ sunday_repeater.start = @now
42
+ this_sunday_span = sunday_repeater.this(:future)
43
+ this_week_end = this_sunday_span.begin
44
+ Chronic::Span.new(this_week_start, this_week_end)
45
+ when :past
46
+ this_week_end = Chronic.time_class.local(@now.year, @now.month, @now.day, @now.hour)
47
+ sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
48
+ sunday_repeater.start = @now
49
+ last_sunday_span = sunday_repeater.next(:past)
50
+ this_week_start = last_sunday_span.begin
51
+ Chronic::Span.new(this_week_start, this_week_end)
52
+ when :none
53
+ sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
54
+ sunday_repeater.start = @now
55
+ last_sunday_span = sunday_repeater.next(:past)
56
+ this_week_start = last_sunday_span.begin
57
+ Chronic::Span.new(this_week_start, this_week_start + WEEK_SECONDS)
58
+ end
59
+ end
60
+
61
+ def offset(span, amount, pointer)
62
+ direction = pointer == :future ? 1 : -1
63
+ span + direction * amount * WEEK_SECONDS
64
+ end
65
+
66
+ def width
67
+ WEEK_SECONDS
68
+ end
69
+
70
+ def to_s
71
+ super << '-week'
72
+ end
73
+ end