chronic 0.4.1 → 0.6.5
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.
- data/.gemtest +0 -0
- data/.gitignore +5 -0
- data/.yardopts +3 -0
- data/HISTORY.md +75 -2
- data/README.md +10 -5
- data/Rakefile +12 -106
- data/chronic.gemspec +12 -78
- data/lib/chronic/chronic.rb +271 -101
- data/lib/chronic/grabber.rb +12 -3
- data/lib/chronic/handler.rb +90 -0
- data/lib/chronic/handlers.rb +247 -295
- data/lib/chronic/mini_date.rb +15 -5
- data/lib/chronic/numerizer.rb +3 -2
- data/lib/chronic/ordinal.rb +15 -4
- data/lib/chronic/pointer.rb +13 -5
- data/lib/chronic/repeater.rb +31 -15
- data/lib/chronic/repeaters/repeater_day.rb +6 -7
- data/lib/chronic/repeaters/repeater_day_name.rb +1 -2
- data/lib/chronic/repeaters/repeater_day_portion.rb +12 -13
- data/lib/chronic/repeaters/repeater_fortnight.rb +2 -3
- data/lib/chronic/repeaters/repeater_hour.rb +7 -8
- data/lib/chronic/repeaters/repeater_minute.rb +6 -7
- data/lib/chronic/repeaters/repeater_month.rb +22 -11
- data/lib/chronic/repeaters/repeater_month_name.rb +16 -24
- data/lib/chronic/repeaters/repeater_season.rb +10 -29
- data/lib/chronic/repeaters/repeater_season_name.rb +2 -4
- data/lib/chronic/repeaters/repeater_second.rb +0 -1
- data/lib/chronic/repeaters/repeater_time.rb +19 -20
- data/lib/chronic/repeaters/repeater_week.rb +0 -1
- data/lib/chronic/repeaters/repeater_weekday.rb +1 -2
- data/lib/chronic/repeaters/repeater_weekend.rb +0 -1
- data/lib/chronic/repeaters/repeater_year.rb +29 -18
- data/lib/chronic/scalar.rb +29 -1
- data/lib/chronic/season.rb +37 -0
- data/lib/chronic/separator.rb +24 -7
- data/lib/chronic/tag.rb +12 -1
- data/lib/chronic/time_zone.rb +14 -5
- data/lib/chronic/token.rb +14 -4
- data/lib/chronic.rb +66 -89
- data/test/helper.rb +1 -1
- data/test/test_Chronic.rb +97 -3
- data/test/test_DaylightSavings.rb +1 -1
- data/test/test_Handler.rb +1 -6
- data/test/test_MiniDate.rb +3 -3
- data/test/test_Numerizer.rb +1 -1
- data/test/test_RepeaterDayName.rb +1 -1
- data/test/test_RepeaterFortnight.rb +1 -1
- data/test/test_RepeaterHour.rb +1 -1
- data/test/test_RepeaterMinute.rb +1 -1
- data/test/test_RepeaterMonth.rb +5 -1
- data/test/test_RepeaterMonthName.rb +1 -1
- data/test/test_RepeaterSeason.rb +40 -0
- data/test/test_RepeaterTime.rb +1 -1
- data/test/test_RepeaterWeek.rb +1 -1
- data/test/test_RepeaterWeekday.rb +1 -1
- data/test/test_RepeaterWeekend.rb +1 -1
- data/test/test_RepeaterYear.rb +8 -1
- data/test/test_Span.rb +1 -1
- data/test/test_Token.rb +1 -1
- data/test/test_parsing.rb +228 -117
- metadata +30 -32
- data/Manifest.txt +0 -59
- data/benchmark/benchmark.rb +0 -13
- data/test/test_Time.rb +0 -49
|
@@ -1,22 +1,4 @@
|
|
|
1
1
|
module Chronic
|
|
2
|
-
class Season
|
|
3
|
-
attr_reader :start, :end
|
|
4
|
-
|
|
5
|
-
def initialize(myStart, myEnd)
|
|
6
|
-
@start = myStart
|
|
7
|
-
@end = myEnd
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def self.find_next_season(season, pointer)
|
|
11
|
-
lookup = {:spring => 0, :summer => 1, :autumn => 2, :winter => 3}
|
|
12
|
-
next_season_num = (lookup[season]+1*pointer) % 4
|
|
13
|
-
lookup.invert[next_season_num]
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def self.season_after(season); find_next_season(season, +1); end
|
|
17
|
-
def self.season_before(season); find_next_season(season, -1); end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
2
|
class RepeaterSeason < Repeater #:nodoc:
|
|
21
3
|
SEASON_SECONDS = 7_862_400 # 91 * 24 * 60 * 60
|
|
22
4
|
SEASONS = {
|
|
@@ -28,14 +10,13 @@ module Chronic
|
|
|
28
10
|
|
|
29
11
|
def initialize(type)
|
|
30
12
|
super
|
|
31
|
-
@next_season_start = nil
|
|
32
13
|
end
|
|
33
14
|
|
|
34
15
|
def next(pointer)
|
|
35
16
|
super
|
|
36
17
|
|
|
37
18
|
direction = pointer == :future ? 1 : -1
|
|
38
|
-
next_season = Season.find_next_season(find_current_season(@now
|
|
19
|
+
next_season = Season.find_next_season(find_current_season(MiniDate.from_time(@now)), direction)
|
|
39
20
|
|
|
40
21
|
find_next_season_span(direction, next_season)
|
|
41
22
|
end
|
|
@@ -45,8 +26,8 @@ module Chronic
|
|
|
45
26
|
|
|
46
27
|
direction = pointer == :future ? 1 : -1
|
|
47
28
|
|
|
48
|
-
today =
|
|
49
|
-
this_ssn = find_current_season(@now
|
|
29
|
+
today = Chronic.construct(@now.year, @now.month, @now.day)
|
|
30
|
+
this_ssn = find_current_season(MiniDate.from_time(@now))
|
|
50
31
|
case pointer
|
|
51
32
|
when :past
|
|
52
33
|
this_ssn_start = today + direction * num_seconds_til_start(this_ssn, direction)
|
|
@@ -82,9 +63,9 @@ module Chronic
|
|
|
82
63
|
private
|
|
83
64
|
|
|
84
65
|
def find_next_season_span(direction, next_season)
|
|
85
|
-
|
|
86
|
-
@next_season_start =
|
|
87
|
-
@next_season_end =
|
|
66
|
+
unless @next_season_start or @next_season_end
|
|
67
|
+
@next_season_start = Chronic.construct(@now.year, @now.month, @now.day)
|
|
68
|
+
@next_season_end = Chronic.construct(@now.year, @now.month, @now.day)
|
|
88
69
|
end
|
|
89
70
|
|
|
90
71
|
@next_season_start += direction * num_seconds_til_start(next_season, direction)
|
|
@@ -100,10 +81,10 @@ module Chronic
|
|
|
100
81
|
end
|
|
101
82
|
|
|
102
83
|
def num_seconds_til(goal, direction)
|
|
103
|
-
start =
|
|
84
|
+
start = Chronic.construct(@now.year, @now.month, @now.day)
|
|
104
85
|
seconds = 0
|
|
105
86
|
|
|
106
|
-
until (start + direction * seconds).
|
|
87
|
+
until MiniDate.from_time(start + direction * seconds).equals?(goal)
|
|
107
88
|
seconds += RepeaterDay::DAY_SECONDS
|
|
108
89
|
end
|
|
109
90
|
|
|
@@ -120,8 +101,8 @@ module Chronic
|
|
|
120
101
|
|
|
121
102
|
def construct_season(start, finish)
|
|
122
103
|
Span.new(
|
|
123
|
-
|
|
124
|
-
|
|
104
|
+
Chronic.construct(start.year, start.month, start.day),
|
|
105
|
+
Chronic.construct(finish.year, finish.month, finish.day)
|
|
125
106
|
)
|
|
126
107
|
end
|
|
127
108
|
end
|
|
@@ -9,14 +9,12 @@ module Chronic
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
def this(pointer = :future)
|
|
12
|
-
# super
|
|
13
|
-
|
|
14
12
|
direction = pointer == :future ? 1 : -1
|
|
15
13
|
|
|
16
|
-
today =
|
|
14
|
+
today = Chronic.construct(@now.year, @now.month, @now.day)
|
|
17
15
|
goal_ssn_start = today + direction * num_seconds_til_start(@type, direction)
|
|
18
16
|
goal_ssn_end = today + direction * num_seconds_til_end(@type, direction)
|
|
19
|
-
curr_ssn = find_current_season(@now
|
|
17
|
+
curr_ssn = find_current_season(MiniDate.from_time(@now))
|
|
20
18
|
case pointer
|
|
21
19
|
when :past
|
|
22
20
|
this_ssn_start = goal_ssn_start
|
|
@@ -27,30 +27,29 @@ module Chronic
|
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def initialize(time)
|
|
30
|
-
@current_time = nil
|
|
31
30
|
t = time.gsub(/\:/, '')
|
|
32
31
|
|
|
33
32
|
@type =
|
|
34
33
|
case t.size
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
34
|
+
when 1..2
|
|
35
|
+
hours = t.to_i
|
|
36
|
+
Tick.new((hours == 12 ? 0 : hours) * 60 * 60, true)
|
|
37
|
+
when 3
|
|
38
|
+
hours = t[0..0].to_i
|
|
39
|
+
ambiguous = hours > 0
|
|
40
|
+
Tick.new((hours * 60 * 60) + (t[1..2].to_i * 60), ambiguous)
|
|
41
|
+
when 4
|
|
42
|
+
ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
|
|
43
|
+
hours = t[0..1].to_i
|
|
44
|
+
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)
|
|
45
|
+
when 5
|
|
46
|
+
Tick.new(t[0..0].to_i * 60 * 60 + t[1..2].to_i * 60 + t[3..4].to_i, true)
|
|
47
|
+
when 6
|
|
48
|
+
ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
|
|
49
|
+
hours = t[0..1].to_i
|
|
50
|
+
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)
|
|
51
|
+
else
|
|
52
|
+
raise("Time cannot exceed six digits")
|
|
54
53
|
end
|
|
55
54
|
end
|
|
56
55
|
|
|
@@ -13,7 +13,6 @@ module Chronic
|
|
|
13
13
|
|
|
14
14
|
def initialize(type)
|
|
15
15
|
super
|
|
16
|
-
@current_weekday_start = nil
|
|
17
16
|
end
|
|
18
17
|
|
|
19
18
|
def next(pointer)
|
|
@@ -22,7 +21,7 @@ module Chronic
|
|
|
22
21
|
direction = pointer == :future ? 1 : -1
|
|
23
22
|
|
|
24
23
|
if !@current_weekday_start
|
|
25
|
-
@current_weekday_start =
|
|
24
|
+
@current_weekday_start = Chronic.construct(@now.year, @now.month, @now.day)
|
|
26
25
|
@current_weekday_start += direction * DAY_SECONDS
|
|
27
26
|
|
|
28
27
|
until is_weekday?(@current_weekday_start.wday)
|
|
@@ -4,7 +4,6 @@ module Chronic
|
|
|
4
4
|
|
|
5
5
|
def initialize(type)
|
|
6
6
|
super
|
|
7
|
-
@current_year_start = nil
|
|
8
7
|
end
|
|
9
8
|
|
|
10
9
|
def next(pointer)
|
|
@@ -13,16 +12,16 @@ module Chronic
|
|
|
13
12
|
if !@current_year_start
|
|
14
13
|
case pointer
|
|
15
14
|
when :future
|
|
16
|
-
@current_year_start =
|
|
15
|
+
@current_year_start = Chronic.construct(@now.year + 1)
|
|
17
16
|
when :past
|
|
18
|
-
@current_year_start =
|
|
17
|
+
@current_year_start = Chronic.construct(@now.year - 1)
|
|
19
18
|
end
|
|
20
19
|
else
|
|
21
20
|
diff = pointer == :future ? 1 : -1
|
|
22
|
-
@current_year_start =
|
|
21
|
+
@current_year_start = Chronic.construct(@current_year_start.year + diff)
|
|
23
22
|
end
|
|
24
23
|
|
|
25
|
-
Span.new(@current_year_start,
|
|
24
|
+
Span.new(@current_year_start, Chronic.construct(@current_year_start.year + 1))
|
|
26
25
|
end
|
|
27
26
|
|
|
28
27
|
def this(pointer = :future)
|
|
@@ -30,14 +29,14 @@ module Chronic
|
|
|
30
29
|
|
|
31
30
|
case pointer
|
|
32
31
|
when :future
|
|
33
|
-
this_year_start =
|
|
34
|
-
this_year_end =
|
|
32
|
+
this_year_start = Chronic.construct(@now.year, @now.month, @now.day + 1)
|
|
33
|
+
this_year_end = Chronic.construct(@now.year + 1, 1, 1)
|
|
35
34
|
when :past
|
|
36
|
-
this_year_start =
|
|
37
|
-
this_year_end =
|
|
35
|
+
this_year_start = Chronic.construct(@now.year, 1, 1)
|
|
36
|
+
this_year_end = Chronic.construct(@now.year, @now.month, @now.day)
|
|
38
37
|
when :none
|
|
39
|
-
this_year_start =
|
|
40
|
-
this_year_end =
|
|
38
|
+
this_year_start = Chronic.construct(@now.year, 1, 1)
|
|
39
|
+
this_year_end = Chronic.construct(@now.year + 1, 1, 1)
|
|
41
40
|
end
|
|
42
41
|
|
|
43
42
|
Span.new(this_year_start, this_year_end)
|
|
@@ -45,13 +44,8 @@ module Chronic
|
|
|
45
44
|
|
|
46
45
|
def offset(span, amount, pointer)
|
|
47
46
|
direction = pointer == :future ? 1 : -1
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
new_begin = Time.construct(sb.year + (amount * direction), sb.month, sb.day, sb.hour, sb.min, sb.sec)
|
|
51
|
-
|
|
52
|
-
se = span.end
|
|
53
|
-
new_end = Time.construct(se.year + (amount * direction), se.month, se.day, se.hour, se.min, se.sec)
|
|
54
|
-
|
|
47
|
+
new_begin = build_offset_time(span.begin, amount, direction)
|
|
48
|
+
new_end = build_offset_time(span.end, amount, direction)
|
|
55
49
|
Span.new(new_begin, new_end)
|
|
56
50
|
end
|
|
57
51
|
|
|
@@ -62,5 +56,22 @@ module Chronic
|
|
|
62
56
|
def to_s
|
|
63
57
|
super << '-year'
|
|
64
58
|
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def build_offset_time(time, amount, direction)
|
|
63
|
+
year = time.year + (amount * direction)
|
|
64
|
+
days = month_days(year, time.month)
|
|
65
|
+
day = time.day > days ? days : time.day
|
|
66
|
+
Chronic.construct(year, time.month, day, time.hour, time.min, time.sec)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def month_days(year, month)
|
|
70
|
+
if Date.leap?(year)
|
|
71
|
+
RepeaterMonth::MONTH_DAYS_LEAP[month - 1]
|
|
72
|
+
else
|
|
73
|
+
RepeaterMonth::MONTH_DAYS[month - 1]
|
|
74
|
+
end
|
|
75
|
+
end
|
|
65
76
|
end
|
|
66
77
|
end
|
data/lib/chronic/scalar.rb
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
module Chronic
|
|
2
|
-
class Scalar < Tag
|
|
2
|
+
class Scalar < Tag
|
|
3
3
|
DAY_PORTIONS = %w( am pm morning afternoon evening night )
|
|
4
4
|
|
|
5
|
+
# Scan an Array of {Token}s and apply any necessary Scalar tags to
|
|
6
|
+
# each token
|
|
7
|
+
#
|
|
8
|
+
# @param [Array<Token>] tokens Array of tokens to scan
|
|
9
|
+
# @param [Hash] options Options specified in {Chronic.parse}
|
|
10
|
+
# @return [Array] list of tokens
|
|
5
11
|
def self.scan(tokens, options)
|
|
6
12
|
tokens.each_index do |i|
|
|
7
13
|
if t = scan_for_scalars(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
|
|
@@ -11,6 +17,9 @@ module Chronic
|
|
|
11
17
|
end
|
|
12
18
|
end
|
|
13
19
|
|
|
20
|
+
# @param [Token] token
|
|
21
|
+
# @param [Token] post_token
|
|
22
|
+
# @return [Scalar, nil]
|
|
14
23
|
def self.scan_for_scalars(token, post_token)
|
|
15
24
|
if token.word =~ /^\d*$/
|
|
16
25
|
unless post_token && DAY_PORTIONS.include?(post_token.word)
|
|
@@ -19,6 +28,9 @@ module Chronic
|
|
|
19
28
|
end
|
|
20
29
|
end
|
|
21
30
|
|
|
31
|
+
# @param [Token] token
|
|
32
|
+
# @param [Token] post_token
|
|
33
|
+
# @return [ScalarDay, nil]
|
|
22
34
|
def self.scan_for_days(token, post_token)
|
|
23
35
|
if token.word =~ /^\d\d?$/
|
|
24
36
|
toi = token.word.to_i
|
|
@@ -28,6 +40,9 @@ module Chronic
|
|
|
28
40
|
end
|
|
29
41
|
end
|
|
30
42
|
|
|
43
|
+
# @param [Token] token
|
|
44
|
+
# @param [Token] post_token
|
|
45
|
+
# @return [ScalarMonth, nil]
|
|
31
46
|
def self.scan_for_months(token, post_token)
|
|
32
47
|
if token.word =~ /^\d\d?$/
|
|
33
48
|
toi = token.word.to_i
|
|
@@ -37,6 +52,10 @@ module Chronic
|
|
|
37
52
|
end
|
|
38
53
|
end
|
|
39
54
|
|
|
55
|
+
# @param [Token] token
|
|
56
|
+
# @param [Token] post_token
|
|
57
|
+
# @param [Hash] options Options specified in {Chronic.parse}
|
|
58
|
+
# @return [ScalarYear, nil]
|
|
40
59
|
def self.scan_for_years(token, post_token, options)
|
|
41
60
|
if token.word =~ /^([1-9]\d)?\d\d?$/
|
|
42
61
|
unless post_token && DAY_PORTIONS.include?(post_token.word)
|
|
@@ -47,6 +66,15 @@ module Chronic
|
|
|
47
66
|
end
|
|
48
67
|
|
|
49
68
|
# Build a year from a 2 digit suffix
|
|
69
|
+
#
|
|
70
|
+
# @example
|
|
71
|
+
# make_year(96, 50) #=> 1996
|
|
72
|
+
# make_year(79, 20) #=> 2079
|
|
73
|
+
# make_year(00, 50) #=> 2000
|
|
74
|
+
#
|
|
75
|
+
# @param [Integer] year The two digit year to build from
|
|
76
|
+
# @param [Integer] bias The amount of future years to bias
|
|
77
|
+
# @return [Integer] The 4 digit year
|
|
50
78
|
def self.make_year(year, bias)
|
|
51
79
|
return year if year.to_s.size > 2
|
|
52
80
|
start_year = Chronic.time_class.now.year - bias
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module Chronic
|
|
2
|
+
class Season
|
|
3
|
+
# @return [MiniDate]
|
|
4
|
+
attr_reader :start
|
|
5
|
+
|
|
6
|
+
# @return [MiniDate]
|
|
7
|
+
attr_reader :end
|
|
8
|
+
|
|
9
|
+
# @param [MiniDate] start_date
|
|
10
|
+
# @param [MiniDate] end_date
|
|
11
|
+
def initialize(start_date, end_date)
|
|
12
|
+
@start = start_date
|
|
13
|
+
@end = end_date
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# @param [Symbol] season The season name
|
|
17
|
+
# @param [Integer] pointer The direction (-1 for past, 1 for future)
|
|
18
|
+
# @return [Symbol] The new season name
|
|
19
|
+
def self.find_next_season(season, pointer)
|
|
20
|
+
lookup = [:spring, :summer, :autumn, :winter]
|
|
21
|
+
next_season_num = (lookup.index(season) + 1 * pointer) % 4
|
|
22
|
+
lookup[next_season_num]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @param [Symbol] season The season name
|
|
26
|
+
# @return [Symbol] The new season name
|
|
27
|
+
def self.season_after(season)
|
|
28
|
+
find_next_season(season, +1)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @param [Symbol] season The season name
|
|
32
|
+
# @return [Symbol] The new season name
|
|
33
|
+
def self.season_before(season)
|
|
34
|
+
find_next_season(season, -1)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
data/lib/chronic/separator.rb
CHANGED
|
@@ -1,19 +1,30 @@
|
|
|
1
1
|
module Chronic
|
|
2
|
-
class Separator < Tag
|
|
2
|
+
class Separator < Tag
|
|
3
|
+
|
|
4
|
+
# Scan an Array of {Token}s and apply any necessary Separator 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
|
|
3
10
|
def self.scan(tokens, options)
|
|
4
|
-
tokens.
|
|
5
|
-
if t = scan_for_commas(
|
|
6
|
-
if t = scan_for_slash_or_dash(
|
|
7
|
-
if t = scan_for_at(
|
|
8
|
-
if t = scan_for_in(
|
|
9
|
-
if t = scan_for_on(
|
|
11
|
+
tokens.each do |token|
|
|
12
|
+
if t = scan_for_commas(token) then token.tag(t); next end
|
|
13
|
+
if t = scan_for_slash_or_dash(token) then token.tag(t); next end
|
|
14
|
+
if t = scan_for_at(token) then token.tag(t); next end
|
|
15
|
+
if t = scan_for_in(token) then token.tag(t); next end
|
|
16
|
+
if t = scan_for_on(token) then token.tag(t); next end
|
|
10
17
|
end
|
|
11
18
|
end
|
|
12
19
|
|
|
20
|
+
# @param [Token] token
|
|
21
|
+
# @return [SeparatorComma, nil]
|
|
13
22
|
def self.scan_for_commas(token)
|
|
14
23
|
scan_for token, SeparatorComma, { /^,$/ => :comma }
|
|
15
24
|
end
|
|
16
25
|
|
|
26
|
+
# @param [Token] token
|
|
27
|
+
# @return [SeparatorSlashOrDash, nil]
|
|
17
28
|
def self.scan_for_slash_or_dash(token)
|
|
18
29
|
scan_for token, SeparatorSlashOrDash,
|
|
19
30
|
{
|
|
@@ -22,14 +33,20 @@ module Chronic
|
|
|
22
33
|
}
|
|
23
34
|
end
|
|
24
35
|
|
|
36
|
+
# @param [Token] token
|
|
37
|
+
# @return [SeparatorAt, nil]
|
|
25
38
|
def self.scan_for_at(token)
|
|
26
39
|
scan_for token, SeparatorAt, { /^(at|@)$/ => :at }
|
|
27
40
|
end
|
|
28
41
|
|
|
42
|
+
# @param [Token] token
|
|
43
|
+
# @return [SeparatorIn, nil]
|
|
29
44
|
def self.scan_for_in(token)
|
|
30
45
|
scan_for token, SeparatorIn, { /^in$/ => :in }
|
|
31
46
|
end
|
|
32
47
|
|
|
48
|
+
# @param [Token] token
|
|
49
|
+
# @return [SeparatorOn, nil]
|
|
33
50
|
def self.scan_for_on(token)
|
|
34
51
|
scan_for token, SeparatorOn, { /^on$/ => :on }
|
|
35
52
|
end
|
data/lib/chronic/tag.rb
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
module Chronic
|
|
2
2
|
# Tokens are tagged with subclassed instances of this class when
|
|
3
3
|
# they match specific criteria
|
|
4
|
-
class Tag
|
|
4
|
+
class Tag
|
|
5
|
+
|
|
6
|
+
# @return [Symbol]
|
|
5
7
|
attr_accessor :type
|
|
6
8
|
|
|
9
|
+
# @param [Symbol] type
|
|
7
10
|
def initialize(type)
|
|
8
11
|
@type = type
|
|
9
12
|
end
|
|
10
13
|
|
|
14
|
+
# @param [Time] s Set the start timestamp for this Tag
|
|
11
15
|
def start=(s)
|
|
12
16
|
@now = s
|
|
13
17
|
end
|
|
@@ -15,6 +19,11 @@ module Chronic
|
|
|
15
19
|
class << self
|
|
16
20
|
private
|
|
17
21
|
|
|
22
|
+
# @param [Token] token
|
|
23
|
+
# @param [Class] klass The class instance to create
|
|
24
|
+
# @param [Regexp, Hash] items
|
|
25
|
+
# @return [Object, nil] either a new instance of `klass` or `nil` if
|
|
26
|
+
# no match is found
|
|
18
27
|
def scan_for(token, klass, items={})
|
|
19
28
|
case items
|
|
20
29
|
when Regexp
|
|
@@ -26,6 +35,8 @@ module Chronic
|
|
|
26
35
|
end
|
|
27
36
|
nil
|
|
28
37
|
end
|
|
38
|
+
|
|
29
39
|
end
|
|
40
|
+
|
|
30
41
|
end
|
|
31
42
|
end
|
data/lib/chronic/time_zone.rb
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
module Chronic
|
|
2
|
-
class TimeZone < Tag
|
|
2
|
+
class TimeZone < Tag
|
|
3
|
+
|
|
4
|
+
# Scan an Array of {Token}s and apply any necessary TimeZone 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
|
|
3
10
|
def self.scan(tokens, options)
|
|
4
|
-
tokens.
|
|
5
|
-
if t = scan_for_all(
|
|
11
|
+
tokens.each do |token|
|
|
12
|
+
if t = scan_for_all(token) then token.tag(t); next end
|
|
6
13
|
end
|
|
7
14
|
end
|
|
8
15
|
|
|
16
|
+
# @param [Token] token
|
|
17
|
+
# @return [TimeZone, nil]
|
|
9
18
|
def self.scan_for_all(token)
|
|
10
19
|
scan_for token, self,
|
|
11
20
|
{
|
|
12
|
-
/[PMCE][DS]T/i => :tz,
|
|
13
|
-
/(tzminus)?\d{
|
|
21
|
+
/[PMCE][DS]T|UTC/i => :tz,
|
|
22
|
+
/(tzminus)?\d{2}:?\d{2}/ => :tz
|
|
14
23
|
}
|
|
15
24
|
end
|
|
16
25
|
|
data/lib/chronic/token.rb
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
module Chronic
|
|
2
|
-
class Token
|
|
3
|
-
|
|
2
|
+
class Token
|
|
3
|
+
|
|
4
|
+
# @return [String] The word this Token represents
|
|
5
|
+
attr_accessor :word
|
|
6
|
+
|
|
7
|
+
# @return [Array] A list of tag associated with this Token
|
|
8
|
+
attr_accessor :tags
|
|
4
9
|
|
|
5
10
|
def initialize(word)
|
|
6
11
|
@word = word
|
|
@@ -8,21 +13,26 @@ module Chronic
|
|
|
8
13
|
end
|
|
9
14
|
|
|
10
15
|
# Tag this token with the specified tag
|
|
16
|
+
#
|
|
17
|
+
# @param [Tag] new_tag An instance of {Tag} or one of its subclasses
|
|
11
18
|
def tag(new_tag)
|
|
12
19
|
@tags << new_tag
|
|
13
20
|
end
|
|
14
21
|
|
|
15
22
|
# Remove all tags of the given class
|
|
23
|
+
#
|
|
24
|
+
# @param [Class] The tag class to remove
|
|
16
25
|
def untag(tag_class)
|
|
17
26
|
@tags.delete_if { |m| m.kind_of? tag_class }
|
|
18
27
|
end
|
|
19
28
|
|
|
20
|
-
#
|
|
29
|
+
# @return [Boolean] true if this token has any tags
|
|
21
30
|
def tagged?
|
|
22
31
|
@tags.size > 0
|
|
23
32
|
end
|
|
24
33
|
|
|
25
|
-
#
|
|
34
|
+
# @param [Class] tag_class The tag class to search for
|
|
35
|
+
# @return [Tag] The first Tag that matches the given class
|
|
26
36
|
def get_tag(tag_class)
|
|
27
37
|
@tags.find { |m| m.kind_of? tag_class }
|
|
28
38
|
end
|