chronic 0.4.1 → 0.6.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.
- data/.gemtest +0 -0
- data/.gitignore +5 -0
- data/.yardopts +3 -0
- data/HISTORY.md +49 -2
- data/README.md +8 -2
- data/Rakefile +9 -101
- data/chronic.gemspec +12 -78
- data/lib/chronic/chronic.rb +260 -101
- data/lib/chronic/grabber.rb +12 -3
- data/lib/chronic/handler.rb +90 -0
- data/lib/chronic/handlers.rb +189 -301
- 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 +8 -9
- 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 +1 -2
- 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 +12 -3
- data/lib/chronic/token.rb +14 -4
- data/lib/chronic.rb +66 -89
- 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 +167 -115
- metadata +32 -32
- data/Manifest.txt +0 -59
- data/benchmark/benchmark.rb +0 -13
- data/test/test_Time.rb +0 -49
data/test/test_Chronic.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'helper'
|
|
2
2
|
|
|
3
3
|
class TestChronic < Test::Unit::TestCase
|
|
4
4
|
|
|
@@ -21,7 +21,7 @@ class TestChronic < Test::Unit::TestCase
|
|
|
21
21
|
|
|
22
22
|
assert_equal :morning, tokens[1].tags[0].type
|
|
23
23
|
|
|
24
|
-
tokens = Chronic.dealias_and_disambiguate_times(tokens, {})
|
|
24
|
+
tokens = Chronic::Handlers.dealias_and_disambiguate_times(tokens, {})
|
|
25
25
|
|
|
26
26
|
assert_equal :am, tokens[1].tags[0].type
|
|
27
27
|
assert_equal 2, tokens.size
|
|
@@ -34,7 +34,7 @@ class TestChronic < Test::Unit::TestCase
|
|
|
34
34
|
|
|
35
35
|
assert_equal :morning, tokens[1].tags[0].type
|
|
36
36
|
|
|
37
|
-
tokens = Chronic.dealias_and_disambiguate_times(tokens, {})
|
|
37
|
+
tokens = Chronic::Handlers.dealias_and_disambiguate_times(tokens, {})
|
|
38
38
|
|
|
39
39
|
assert_equal :morning, tokens[1].tags[0].type
|
|
40
40
|
assert_equal 2, tokens.size
|
|
@@ -51,4 +51,98 @@ class TestChronic < Test::Unit::TestCase
|
|
|
51
51
|
assert_equal Time.local(2006, 11, 16), Chronic.guess(span)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
def test_now
|
|
55
|
+
Chronic.parse('now', :now => Time.local(2006, 01))
|
|
56
|
+
assert_equal Time.local(2006, 01), Chronic.now
|
|
57
|
+
|
|
58
|
+
Chronic.parse('now', :now => Time.local(2007, 01))
|
|
59
|
+
assert_equal Time.local(2007, 01), Chronic.now
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_endian_definitions
|
|
63
|
+
# middle, little
|
|
64
|
+
endians = [
|
|
65
|
+
Chronic::Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
|
|
66
|
+
Chronic::Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
assert_equal endians, Chronic.definitions[:endian]
|
|
70
|
+
|
|
71
|
+
defs = Chronic.definitions(:endian_precedence => :little)
|
|
72
|
+
assert_equal endians.reverse, defs[:endian]
|
|
73
|
+
|
|
74
|
+
defs = Chronic.definitions(:endian_precedence => [:little, :middle])
|
|
75
|
+
assert_equal endians.reverse, defs[:endian]
|
|
76
|
+
|
|
77
|
+
assert_raises(ArgumentError) do
|
|
78
|
+
Chronic.definitions(:endian_precedence => :invalid)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_passing_options
|
|
83
|
+
assert_raises(ArgumentError) do
|
|
84
|
+
Chronic.parse('now', :invalid => :option)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
assert_raises(ArgumentError) do
|
|
88
|
+
Chronic.parse('now', :context => :invalid_context)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def test_debug
|
|
93
|
+
require 'stringio'
|
|
94
|
+
$stdout = StringIO.new
|
|
95
|
+
Chronic.debug = true
|
|
96
|
+
|
|
97
|
+
Chronic.parse 'now'
|
|
98
|
+
assert $stdout.string.include?('this(grabber-this)')
|
|
99
|
+
ensure
|
|
100
|
+
$stdout = STDOUT
|
|
101
|
+
Chronic.debug = false
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Chronic.construct
|
|
105
|
+
|
|
106
|
+
def test_normal
|
|
107
|
+
assert_equal Time.local(2006, 1, 2, 0, 0, 0), Chronic.construct(2006, 1, 2, 0, 0, 0)
|
|
108
|
+
assert_equal Time.local(2006, 1, 2, 3, 0, 0), Chronic.construct(2006, 1, 2, 3, 0, 0)
|
|
109
|
+
assert_equal Time.local(2006, 1, 2, 3, 4, 0), Chronic.construct(2006, 1, 2, 3, 4, 0)
|
|
110
|
+
assert_equal Time.local(2006, 1, 2, 3, 4, 5), Chronic.construct(2006, 1, 2, 3, 4, 5)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def test_second_overflow
|
|
114
|
+
assert_equal Time.local(2006, 1, 1, 0, 1, 30), Chronic.construct(2006, 1, 1, 0, 0, 90)
|
|
115
|
+
assert_equal Time.local(2006, 1, 1, 0, 5, 0), Chronic.construct(2006, 1, 1, 0, 0, 300)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def test_minute_overflow
|
|
119
|
+
assert_equal Time.local(2006, 1, 1, 1, 30), Chronic.construct(2006, 1, 1, 0, 90)
|
|
120
|
+
assert_equal Time.local(2006, 1, 1, 5), Chronic.construct(2006, 1, 1, 0, 300)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def test_hour_overflow
|
|
124
|
+
assert_equal Time.local(2006, 1, 2, 12), Chronic.construct(2006, 1, 1, 36)
|
|
125
|
+
assert_equal Time.local(2006, 1, 7), Chronic.construct(2006, 1, 1, 144)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def test_day_overflow
|
|
129
|
+
assert_equal Time.local(2006, 2, 1), Chronic.construct(2006, 1, 32)
|
|
130
|
+
assert_equal Time.local(2006, 3, 5), Chronic.construct(2006, 2, 33)
|
|
131
|
+
assert_equal Time.local(2004, 3, 4), Chronic.construct(2004, 2, 33)
|
|
132
|
+
assert_equal Time.local(2000, 3, 4), Chronic.construct(2000, 2, 33)
|
|
133
|
+
|
|
134
|
+
assert_nothing_raised do
|
|
135
|
+
Chronic.construct(2006, 1, 56)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
assert_raise(RuntimeError) do
|
|
139
|
+
Chronic.construct(2006, 1, 57)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def test_month_overflow
|
|
144
|
+
assert_equal Time.local(2006, 1), Chronic.construct(2005, 13)
|
|
145
|
+
assert_equal Time.local(2005, 12), Chronic.construct(2000, 72)
|
|
146
|
+
end
|
|
147
|
+
|
|
54
148
|
end
|
data/test/test_Handler.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'helper'
|
|
2
2
|
|
|
3
3
|
class TestHandler < Test::Unit::TestCase
|
|
4
4
|
|
|
@@ -101,9 +101,4 @@ class TestHandler < Test::Unit::TestCase
|
|
|
101
101
|
assert handler.match(tokens, Chronic.definitions)
|
|
102
102
|
end
|
|
103
103
|
|
|
104
|
-
def test_constantize
|
|
105
|
-
handler = Chronic::Handler.new([], :handler)
|
|
106
|
-
assert_equal Chronic::RepeaterTime, handler.constantize(:repeater_time)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
104
|
end
|
data/test/test_MiniDate.rb
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'helper'
|
|
2
2
|
|
|
3
3
|
class TestMiniDate < Test::Unit::TestCase
|
|
4
4
|
def test_valid_month
|
|
5
|
-
assert_raise(
|
|
6
|
-
assert_raise(
|
|
5
|
+
assert_raise(ArgumentError){ Chronic::MiniDate.new(0,12) }
|
|
6
|
+
assert_raise(ArgumentError){ Chronic::MiniDate.new(13,1) }
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def test_is_between
|
data/test/test_Numerizer.rb
CHANGED
data/test/test_RepeaterHour.rb
CHANGED
data/test/test_RepeaterMinute.rb
CHANGED
data/test/test_RepeaterMonth.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'helper'
|
|
2
2
|
|
|
3
3
|
class TestRepeaterMonth < Test::Unit::TestCase
|
|
4
4
|
|
|
@@ -23,6 +23,10 @@ class TestRepeaterMonth < Test::Unit::TestCase
|
|
|
23
23
|
|
|
24
24
|
time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 10, :past)
|
|
25
25
|
assert_equal Time.local(2005, 10, 16, 14), time
|
|
26
|
+
|
|
27
|
+
time = Chronic::RepeaterMonth.new(:month).offset_by(Time.local(2010, 3, 29), 1, :past)
|
|
28
|
+
assert_equal 2, time.month
|
|
29
|
+
assert_equal 28, time.day
|
|
26
30
|
end
|
|
27
31
|
|
|
28
32
|
def test_offset
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestRepeaterSeason < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
@now = Time.local(2006, 8, 16, 14, 0, 0, 0)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def test_next_future
|
|
10
|
+
seasons = Chronic::RepeaterSeason.new(:season)
|
|
11
|
+
seasons.start = @now
|
|
12
|
+
|
|
13
|
+
next_season = seasons.next(:future)
|
|
14
|
+
assert_equal Time.local(2006, 9, 23), next_season.begin
|
|
15
|
+
assert_equal Time.local(2006, 12, 21), next_season.end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_next_past
|
|
19
|
+
seasons = Chronic::RepeaterSeason.new(:season)
|
|
20
|
+
seasons.start = @now
|
|
21
|
+
|
|
22
|
+
last_season = seasons.next(:past)
|
|
23
|
+
assert_equal Time.local(2006, 3, 20), last_season.begin
|
|
24
|
+
assert_equal Time.local(2006, 6, 20), last_season.end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_this
|
|
28
|
+
seasons = Chronic::RepeaterSeason.new(:season)
|
|
29
|
+
seasons.start = @now
|
|
30
|
+
|
|
31
|
+
this_season = seasons.this(:future)
|
|
32
|
+
assert_equal Time.local(2006, 8, 17), this_season.begin
|
|
33
|
+
assert_equal Time.local(2006, 9, 22), this_season.end
|
|
34
|
+
|
|
35
|
+
this_season = seasons.this(:past)
|
|
36
|
+
assert_equal Time.local(2006, 6, 21), this_season.begin
|
|
37
|
+
assert_equal Time.local(2006, 8, 16), this_season.end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
data/test/test_RepeaterTime.rb
CHANGED
data/test/test_RepeaterWeek.rb
CHANGED
data/test/test_RepeaterYear.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'helper'
|
|
2
2
|
|
|
3
3
|
class TestRepeaterYear < Test::Unit::TestCase
|
|
4
4
|
|
|
@@ -57,6 +57,13 @@ class TestRepeaterYear < Test::Unit::TestCase
|
|
|
57
57
|
|
|
58
58
|
assert_equal Time.local(1996, 8, 16, 14), offset_span.begin
|
|
59
59
|
assert_equal Time.local(1996, 8, 16, 14, 0, 1), offset_span.end
|
|
60
|
+
|
|
61
|
+
now = Time.local(2008, 2, 29)
|
|
62
|
+
span = Chronic::Span.new(now, now + 1)
|
|
63
|
+
offset_span = Chronic::RepeaterYear.new(:year).offset(span, 1, :past)
|
|
64
|
+
|
|
65
|
+
assert_equal Time.local(2007, 2, 28), offset_span.begin
|
|
66
|
+
assert_equal Time.local(2007, 2, 28, 0, 0, 1), offset_span.end
|
|
60
67
|
end
|
|
61
68
|
|
|
62
69
|
end
|
data/test/test_Span.rb
CHANGED
data/test/test_Token.rb
CHANGED