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.
- data/{README.rdoc → README.txt} +7 -22
- data/lib/chronic.rb +89 -12
- data/lib/chronic/chronic.rb +260 -301
- data/lib/chronic/grabber.rb +23 -23
- data/lib/chronic/handlers.rb +538 -557
- data/lib/chronic/ordinal.rb +36 -35
- data/lib/chronic/pointer.rb +24 -26
- data/lib/chronic/repeater.rb +128 -138
- data/lib/chronic/repeaters/repeater_day.rb +51 -51
- data/lib/chronic/repeaters/repeater_day_name.rb +50 -52
- data/lib/chronic/repeaters/repeater_day_portion.rb +93 -93
- data/lib/chronic/repeaters/repeater_fortnight.rb +66 -66
- data/lib/chronic/repeaters/repeater_hour.rb +56 -57
- data/lib/chronic/repeaters/repeater_minute.rb +56 -56
- data/lib/chronic/repeaters/repeater_month.rb +71 -62
- data/lib/chronic/repeaters/repeater_month_name.rb +95 -95
- data/lib/chronic/repeaters/repeater_season.rb +142 -142
- data/lib/chronic/repeaters/repeater_season_name.rb +42 -42
- data/lib/chronic/repeaters/repeater_second.rb +40 -40
- data/lib/chronic/repeaters/repeater_time.rb +124 -123
- data/lib/chronic/repeaters/repeater_week.rb +70 -70
- data/lib/chronic/repeaters/repeater_weekday.rb +76 -76
- data/lib/chronic/repeaters/repeater_weekend.rb +63 -63
- data/lib/chronic/repeaters/repeater_year.rb +63 -63
- data/lib/chronic/scalar.rb +89 -70
- data/lib/chronic/separator.rb +88 -88
- data/lib/chronic/time_zone.rb +23 -20
- data/lib/numerizer/numerizer.rb +93 -94
- data/test/suite.rb +2 -2
- data/test/test_Chronic.rb +47 -47
- data/test/test_Handler.rb +106 -106
- data/test/test_Numerizer.rb +47 -49
- data/test/test_RepeaterDayName.rb +48 -48
- data/test/test_RepeaterFortnight.rb +59 -59
- data/test/test_RepeaterHour.rb +61 -64
- data/test/test_RepeaterMonth.rb +43 -43
- data/test/test_RepeaterMonthName.rb +53 -53
- data/test/test_RepeaterTime.rb +68 -68
- data/test/test_RepeaterWeek.rb +59 -59
- data/test/test_RepeaterWeekday.rb +53 -53
- data/test/test_RepeaterWeekend.rb +71 -71
- data/test/test_RepeaterYear.rb +59 -59
- data/test/test_Span.rb +19 -28
- data/test/test_Time.rb +46 -46
- data/test/test_Token.rb +22 -22
- data/test/test_parsing.rb +726 -792
- 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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
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
|
-
|
2
|
+
WEEK_SECONDS = 604800 # (7 * 24 * 60 * 60)
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
def initialize(type)
|
5
|
+
super
|
6
|
+
@current_week_start = nil
|
7
|
+
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|