caleb-chronic 0.3.0

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 +167 -0
  2. data/lib/chronic.rb +127 -0
  3. data/lib/chronic/chronic.rb +249 -0
  4. data/lib/chronic/grabber.rb +26 -0
  5. data/lib/chronic/handlers.rb +524 -0
  6. data/lib/chronic/ordinal.rb +40 -0
  7. data/lib/chronic/pointer.rb +27 -0
  8. data/lib/chronic/repeater.rb +129 -0
  9. data/lib/chronic/repeaters/repeater_day.rb +52 -0
  10. data/lib/chronic/repeaters/repeater_day_name.rb +51 -0
  11. data/lib/chronic/repeaters/repeater_day_portion.rb +94 -0
  12. data/lib/chronic/repeaters/repeater_fortnight.rb +70 -0
  13. data/lib/chronic/repeaters/repeater_hour.rb +57 -0
  14. data/lib/chronic/repeaters/repeater_minute.rb +57 -0
  15. data/lib/chronic/repeaters/repeater_month.rb +66 -0
  16. data/lib/chronic/repeaters/repeater_month_name.rb +98 -0
  17. data/lib/chronic/repeaters/repeater_season.rb +150 -0
  18. data/lib/chronic/repeaters/repeater_season_name.rb +45 -0
  19. data/lib/chronic/repeaters/repeater_second.rb +41 -0
  20. data/lib/chronic/repeaters/repeater_time.rb +124 -0
  21. data/lib/chronic/repeaters/repeater_week.rb +73 -0
  22. data/lib/chronic/repeaters/repeater_weekday.rb +77 -0
  23. data/lib/chronic/repeaters/repeater_weekend.rb +65 -0
  24. data/lib/chronic/repeaters/repeater_year.rb +64 -0
  25. data/lib/chronic/scalar.rb +76 -0
  26. data/lib/chronic/separator.rb +91 -0
  27. data/lib/chronic/time_zone.rb +23 -0
  28. data/lib/numerizer/numerizer.rb +97 -0
  29. data/test/suite.rb +9 -0
  30. data/test/test_Chronic.rb +50 -0
  31. data/test/test_Handler.rb +110 -0
  32. data/test/test_Numerizer.rb +52 -0
  33. data/test/test_RepeaterDayName.rb +52 -0
  34. data/test/test_RepeaterFortnight.rb +63 -0
  35. data/test/test_RepeaterHour.rb +65 -0
  36. data/test/test_RepeaterMonth.rb +47 -0
  37. data/test/test_RepeaterMonthName.rb +57 -0
  38. data/test/test_RepeaterTime.rb +72 -0
  39. data/test/test_RepeaterWeek.rb +63 -0
  40. data/test/test_RepeaterWeekday.rb +56 -0
  41. data/test/test_RepeaterWeekend.rb +75 -0
  42. data/test/test_RepeaterYear.rb +63 -0
  43. data/test/test_Span.rb +24 -0
  44. data/test/test_Time.rb +50 -0
  45. data/test/test_Token.rb +26 -0
  46. data/test/test_parsing.rb +711 -0
  47. metadata +101 -0
@@ -0,0 +1,56 @@
1
+ require 'lib/chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterWeekday < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @now = Time.local(2007, 6, 11, 14, 0, 0, 0) # Mon
8
+ end
9
+
10
+ def test_next_future
11
+ weekdays = Chronic::RepeaterWeekday.new(:weekday)
12
+ weekdays.start = @now
13
+
14
+ next1_weekday = weekdays.next(:future) # Tues
15
+ assert_equal Time.local(2007, 6, 12), next1_weekday.begin
16
+ assert_equal Time.local(2007, 6, 13), next1_weekday.end
17
+
18
+ next2_weekday = weekdays.next(:future) # Wed
19
+ assert_equal Time.local(2007, 6, 13), next2_weekday.begin
20
+ assert_equal Time.local(2007, 6, 14), next2_weekday.end
21
+
22
+ next3_weekday = weekdays.next(:future) # Thurs
23
+ assert_equal Time.local(2007, 6, 14), next3_weekday.begin
24
+ assert_equal Time.local(2007, 6, 15), next3_weekday.end
25
+
26
+ next4_weekday = weekdays.next(:future) # Fri
27
+ assert_equal Time.local(2007, 6, 15), next4_weekday.begin
28
+ assert_equal Time.local(2007, 6, 16), next4_weekday.end
29
+
30
+ next5_weekday = weekdays.next(:future) # Mon
31
+ assert_equal Time.local(2007, 6, 18), next5_weekday.begin
32
+ assert_equal Time.local(2007, 6, 19), next5_weekday.end
33
+ end
34
+
35
+ def test_next_past
36
+ weekdays = Chronic::RepeaterWeekday.new(:weekday)
37
+ weekdays.start = @now
38
+
39
+ last1_weekday = weekdays.next(:past) # Fri
40
+ assert_equal Time.local(2007, 6, 8), last1_weekday.begin
41
+ assert_equal Time.local(2007, 6, 9), last1_weekday.end
42
+
43
+ last2_weekday = weekdays.next(:past) # Thurs
44
+ assert_equal Time.local(2007, 6, 7), last2_weekday.begin
45
+ assert_equal Time.local(2007, 6, 8), last2_weekday.end
46
+ end
47
+
48
+ def test_offset
49
+ span = Chronic::Span.new(@now, @now + 1)
50
+
51
+ offset_span = Chronic::RepeaterWeekday.new(:weekday).offset(span, 5, :future)
52
+
53
+ assert_equal Time.local(2007, 6, 18, 14), offset_span.begin
54
+ assert_equal Time.local(2007, 6, 18, 14, 0, 1), offset_span.end
55
+ end
56
+ end
@@ -0,0 +1,75 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterWeekend < Test::Unit::TestCase
5
+
6
+ def setup
7
+ # Wed Aug 16 14:00:00 2006
8
+ @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
9
+ end
10
+
11
+ def test_next_future
12
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
13
+ weekend.start = @now
14
+
15
+ next_weekend = weekend.next(:future)
16
+ assert_equal Time.local(2006, 8, 19), next_weekend.begin
17
+ assert_equal Time.local(2006, 8, 21), next_weekend.end
18
+ end
19
+
20
+ def test_next_past
21
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
22
+ weekend.start = @now
23
+
24
+ next_weekend = weekend.next(:past)
25
+ assert_equal Time.local(2006, 8, 12), next_weekend.begin
26
+ assert_equal Time.local(2006, 8, 14), next_weekend.end
27
+ end
28
+
29
+ def test_this_future
30
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
31
+ weekend.start = @now
32
+
33
+ next_weekend = weekend.this(:future)
34
+ assert_equal Time.local(2006, 8, 19), next_weekend.begin
35
+ assert_equal Time.local(2006, 8, 21), next_weekend.end
36
+ end
37
+
38
+ def test_this_past
39
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
40
+ weekend.start = @now
41
+
42
+ next_weekend = weekend.this(:past)
43
+ assert_equal Time.local(2006, 8, 12), next_weekend.begin
44
+ assert_equal Time.local(2006, 8, 14), next_weekend.end
45
+ end
46
+
47
+ def test_this_none
48
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
49
+ weekend.start = @now
50
+
51
+ next_weekend = weekend.this(:future)
52
+ assert_equal Time.local(2006, 8, 19), next_weekend.begin
53
+ assert_equal Time.local(2006, 8, 21), next_weekend.end
54
+ end
55
+
56
+ def test_offset
57
+ span = Chronic::Span.new(@now, @now + 1)
58
+
59
+ offset_span = Chronic::RepeaterWeekend.new(:weekend).offset(span, 3, :future)
60
+
61
+ assert_equal Time.local(2006, 9, 2), offset_span.begin
62
+ assert_equal Time.local(2006, 9, 2, 0, 0, 1), offset_span.end
63
+
64
+ offset_span = Chronic::RepeaterWeekend.new(:weekend).offset(span, 1, :past)
65
+
66
+ assert_equal Time.local(2006, 8, 12), offset_span.begin
67
+ assert_equal Time.local(2006, 8, 12, 0, 0, 1), offset_span.end
68
+
69
+ offset_span = Chronic::RepeaterWeekend.new(:weekend).offset(span, 0, :future)
70
+
71
+ assert_equal Time.local(2006, 8, 12), offset_span.begin
72
+ assert_equal Time.local(2006, 8, 12, 0, 0, 1), offset_span.end
73
+ end
74
+
75
+ end
@@ -0,0 +1,63 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterYear < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
8
+ end
9
+
10
+ def test_next_future
11
+ years = Chronic::RepeaterYear.new(:year)
12
+ years.start = @now
13
+
14
+ next_year = years.next(:future)
15
+ assert_equal Time.local(2007, 1, 1), next_year.begin
16
+ assert_equal Time.local(2008, 1, 1), next_year.end
17
+
18
+ next_next_year = years.next(:future)
19
+ assert_equal Time.local(2008, 1, 1), next_next_year.begin
20
+ assert_equal Time.local(2009, 1, 1), next_next_year.end
21
+ end
22
+
23
+ def test_next_past
24
+ years = Chronic::RepeaterYear.new(:year)
25
+ years.start = @now
26
+
27
+ last_year = years.next(:past)
28
+ assert_equal Time.local(2005, 1, 1), last_year.begin
29
+ assert_equal Time.local(2006, 1, 1), last_year.end
30
+
31
+ last_last_year = years.next(:past)
32
+ assert_equal Time.local(2004, 1, 1), last_last_year.begin
33
+ assert_equal Time.local(2005, 1, 1), last_last_year.end
34
+ end
35
+
36
+ def test_this
37
+ years = Chronic::RepeaterYear.new(:year)
38
+ years.start = @now
39
+
40
+ this_year = years.this(:future)
41
+ assert_equal Time.local(2006, 8, 17), this_year.begin
42
+ assert_equal Time.local(2007, 1, 1), this_year.end
43
+
44
+ this_year = years.this(:past)
45
+ assert_equal Time.local(2006, 1, 1), this_year.begin
46
+ assert_equal Time.local(2006, 8, 16), this_year.end
47
+ end
48
+
49
+ def test_offset
50
+ span = Chronic::Span.new(@now, @now + 1)
51
+
52
+ offset_span = Chronic::RepeaterYear.new(:year).offset(span, 3, :future)
53
+
54
+ assert_equal Time.local(2009, 8, 16, 14), offset_span.begin
55
+ assert_equal Time.local(2009, 8, 16, 14, 0, 1), offset_span.end
56
+
57
+ offset_span = Chronic::RepeaterYear.new(:year).offset(span, 10, :past)
58
+
59
+ assert_equal Time.local(1996, 8, 16, 14), offset_span.begin
60
+ assert_equal Time.local(1996, 8, 16, 14, 0, 1), offset_span.end
61
+ end
62
+
63
+ end
@@ -0,0 +1,24 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestSpan < Test::Unit::TestCase
5
+
6
+ def setup
7
+ # Wed Aug 16 14:00:00 UTC 2006
8
+ @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
9
+ end
10
+
11
+ def test_span_width
12
+ span = Chronic::Span.new(Time.local(2006, 8, 16, 0), Time.local(2006, 8, 17, 0))
13
+ assert_equal (60 * 60 * 24), span.width
14
+ end
15
+
16
+ def test_span_math
17
+ s = Chronic::Span.new(1, 2)
18
+ assert_equal 2, (s + 1).begin
19
+ assert_equal 3, (s + 1).end
20
+ assert_equal 0, (s - 1).begin
21
+ assert_equal 1, (s - 1).end
22
+ end
23
+
24
+ end
@@ -0,0 +1,50 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestTime < Test::Unit::TestCase
5
+
6
+ def setup
7
+ end
8
+
9
+ def test_normal
10
+ assert_equal Time.local(2006, 1, 2, 0, 0, 0), Time.construct(2006, 1, 2, 0, 0, 0)
11
+ assert_equal Time.local(2006, 1, 2, 3, 0, 0), Time.construct(2006, 1, 2, 3, 0, 0)
12
+ assert_equal Time.local(2006, 1, 2, 3, 4, 0), Time.construct(2006, 1, 2, 3, 4, 0)
13
+ assert_equal Time.local(2006, 1, 2, 3, 4, 5), Time.construct(2006, 1, 2, 3, 4, 5)
14
+ end
15
+
16
+ def test_second_overflow
17
+ assert_equal Time.local(2006, 1, 1, 0, 1, 30), Time.construct(2006, 1, 1, 0, 0, 90)
18
+ assert_equal Time.local(2006, 1, 1, 0, 5, 0), Time.construct(2006, 1, 1, 0, 0, 300)
19
+ end
20
+
21
+ def test_minute_overflow
22
+ assert_equal Time.local(2006, 1, 1, 1, 30), Time.construct(2006, 1, 1, 0, 90)
23
+ assert_equal Time.local(2006, 1, 1, 5), Time.construct(2006, 1, 1, 0, 300)
24
+ end
25
+
26
+ def test_hour_overflow
27
+ assert_equal Time.local(2006, 1, 2, 12), Time.construct(2006, 1, 1, 36)
28
+ assert_equal Time.local(2006, 1, 7), Time.construct(2006, 1, 1, 144)
29
+ end
30
+
31
+ def test_day_overflow
32
+ assert_equal Time.local(2006, 2, 1), Time.construct(2006, 1, 32)
33
+ assert_equal Time.local(2006, 3, 5), Time.construct(2006, 2, 33)
34
+ assert_equal Time.local(2004, 3, 4), Time.construct(2004, 2, 33)
35
+ assert_equal Time.local(2000, 3, 5), Time.construct(2000, 2, 33)
36
+
37
+ assert_nothing_raised do
38
+ Time.construct(2006, 1, 56)
39
+ end
40
+
41
+ assert_raise(RuntimeError) do
42
+ Time.construct(2006, 1, 57)
43
+ end
44
+ end
45
+
46
+ def test_month_overflow
47
+ assert_equal Time.local(2006, 1), Time.construct(2005, 13)
48
+ assert_equal Time.local(2005, 12), Time.construct(2000, 72)
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestToken < Test::Unit::TestCase
5
+
6
+ def setup
7
+ # Wed Aug 16 14:00:00 UTC 2006
8
+ @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
9
+ end
10
+
11
+ def test_token
12
+ token = Chronic::Token.new('foo')
13
+ assert_equal 0, token.tags.size
14
+ assert !token.tagged?
15
+ token.tag("mytag")
16
+ assert_equal 1, token.tags.size
17
+ assert token.tagged?
18
+ assert_equal String, token.get_tag(String).class
19
+ token.tag(5)
20
+ assert_equal 2, token.tags.size
21
+ token.untag(String)
22
+ assert_equal 1, token.tags.size
23
+ assert_equal 'foo', token.word
24
+ end
25
+
26
+ end
@@ -0,0 +1,711 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestParsing < Test::Unit::TestCase
5
+ # Wed Aug 16 14:00:00 UTC 2006
6
+ TIME_2006_08_16_14_00_00 = Time.local(2006, 8, 16, 14, 0, 0, 0)
7
+
8
+ def setup
9
+ @time_2006_08_16_14_00_00 = TIME_2006_08_16_14_00_00
10
+ end
11
+
12
+ def test_parse_guess_dates
13
+ # rm_sd
14
+
15
+ time = parse_now("may 27")
16
+ assert_equal Time.local(2007, 5, 27, 12), time
17
+
18
+ time = parse_now("may 28", :context => :past)
19
+ assert_equal Time.local(2006, 5, 28, 12), time
20
+
21
+ time = parse_now("may 28 5pm", :context => :past)
22
+ assert_equal Time.local(2006, 5, 28, 17), time
23
+
24
+ time = parse_now("may 28 at 5pm", :context => :past)
25
+ assert_equal Time.local(2006, 5, 28, 17), time
26
+
27
+ time = parse_now("may 28 at 5:32.19pm", :context => :past)
28
+ assert_equal Time.local(2006, 5, 28, 17, 32, 19), time
29
+
30
+ # rm_sd_on
31
+
32
+ time = parse_now("5pm on may 28")
33
+ assert_equal Time.local(2007, 5, 28, 17), time
34
+
35
+ time = parse_now("5pm may 28")
36
+ assert_equal Time.local(2007, 5, 28, 17), time
37
+
38
+ time = parse_now("5 on may 28", :ambiguous_time_range => :none)
39
+ assert_equal Time.local(2007, 5, 28, 05), time
40
+
41
+ # rm_od
42
+
43
+ time = parse_now("may 27th")
44
+ assert_equal Time.local(2007, 5, 27, 12), time
45
+
46
+ time = parse_now("may 27th", :context => :past)
47
+ assert_equal Time.local(2006, 5, 27, 12), time
48
+
49
+ time = parse_now("may 27th 5:00 pm", :context => :past)
50
+ assert_equal Time.local(2006, 5, 27, 17), time
51
+
52
+ time = parse_now("may 27th at 5pm", :context => :past)
53
+ assert_equal Time.local(2006, 5, 27, 17), time
54
+
55
+ time = parse_now("may 27th at 5", :ambiguous_time_range => :none)
56
+ assert_equal Time.local(2007, 5, 27, 5), time
57
+
58
+ # rm_od_on
59
+
60
+ time = parse_now("5:00 pm may 27th", :context => :past)
61
+ assert_equal Time.local(2006, 5, 27, 17), time
62
+
63
+ time = parse_now("5pm on may 27th", :context => :past)
64
+ assert_equal Time.local(2006, 5, 27, 17), time
65
+
66
+ time = parse_now("5 on may 27th", :ambiguous_time_range => :none)
67
+ assert_equal Time.local(2007, 5, 27, 5), time
68
+
69
+ # rm_sy
70
+
71
+ time = parse_now("June 1979")
72
+ assert_equal Time.local(1979, 6, 16, 0), time
73
+
74
+ time = parse_now("dec 79")
75
+ assert_equal Time.local(1979, 12, 16, 12), time
76
+
77
+ # rm_sd_sy
78
+
79
+ time = parse_now("jan 3 2010")
80
+ assert_equal Time.local(2010, 1, 3, 12), time
81
+
82
+ time = parse_now("jan 3 2010 midnight")
83
+ assert_equal Time.local(2010, 1, 4, 0), time
84
+
85
+ time = parse_now("jan 3 2010 at midnight")
86
+ assert_equal Time.local(2010, 1, 4, 0), time
87
+
88
+ time = parse_now("jan 3 2010 at 4", :ambiguous_time_range => :none)
89
+ assert_equal Time.local(2010, 1, 3, 4), time
90
+
91
+ #time = parse_now("January 12, '00")
92
+ #assert_equal Time.local(2000, 1, 12, 12), time
93
+
94
+ time = parse_now("may 27, 1979")
95
+ assert_equal Time.local(1979, 5, 27, 12), time
96
+
97
+ time = parse_now("may 27 79")
98
+ assert_equal Time.local(1979, 5, 27, 12), time
99
+
100
+ time = parse_now("may 27 79 4:30")
101
+ assert_equal Time.local(1979, 5, 27, 16, 30), time
102
+
103
+ time = parse_now("may 27 79 at 4:30", :ambiguous_time_range => :none)
104
+ assert_equal Time.local(1979, 5, 27, 4, 30), time
105
+
106
+ # sd_rm_sy
107
+
108
+ time = parse_now("3 jan 2010")
109
+ assert_equal Time.local(2010, 1, 3, 12), time
110
+
111
+ time = parse_now("3 jan 2010 4pm")
112
+ assert_equal Time.local(2010, 1, 3, 16), time
113
+
114
+ time = parse_now("27 Oct 2006 7:30pm")
115
+ assert_equal Time.local(2006, 10, 27, 19, 30), time
116
+
117
+ # sm_sd_sy
118
+
119
+ time = parse_now("5/27/1979")
120
+ assert_equal Time.local(1979, 5, 27, 12), time
121
+
122
+ time = parse_now("5/27/1979 4am")
123
+ assert_equal Time.local(1979, 5, 27, 4), time
124
+
125
+ # sd_sm_sy
126
+
127
+ time = parse_now("27/5/1979")
128
+ assert_equal Time.local(1979, 5, 27, 12), time
129
+
130
+ time = parse_now("27/5/1979 @ 0700")
131
+ assert_equal Time.local(1979, 5, 27, 7), time
132
+
133
+ # sm_sy
134
+
135
+ time = parse_now("05/06")
136
+ assert_equal Time.local(2006, 5, 16, 12), time
137
+
138
+ time = parse_now("12/06")
139
+ assert_equal Time.local(2006, 12, 16, 12), time
140
+
141
+ time = parse_now("13/06")
142
+ assert_equal nil, time
143
+
144
+ # sy_sm_sd
145
+
146
+ time = parse_now("2000-1-1")
147
+ assert_equal Time.local(2000, 1, 1, 12), time
148
+
149
+ time = parse_now("2006-08-20")
150
+ assert_equal Time.local(2006, 8, 20, 12), time
151
+
152
+ time = parse_now("2006-08-20 7pm")
153
+ assert_equal Time.local(2006, 8, 20, 19), time
154
+
155
+ time = parse_now("2006-08-20 03:00")
156
+ assert_equal Time.local(2006, 8, 20, 3), time
157
+
158
+ time = parse_now("2006-08-20 03:30:30")
159
+ assert_equal Time.local(2006, 8, 20, 3, 30, 30), time
160
+
161
+ time = parse_now("2006-08-20 15:30:30")
162
+ assert_equal Time.local(2006, 8, 20, 15, 30, 30), time
163
+
164
+ time = parse_now("2006-08-20 15:30.30")
165
+ assert_equal Time.local(2006, 8, 20, 15, 30, 30), time
166
+
167
+ # rdn_rm_rd_rt_rtz_ry
168
+
169
+ time = parse_now("Mon Apr 02 17:00:00 PDT 2007")
170
+ assert_equal 1175558400, time.to_i
171
+
172
+ now = Time.now
173
+ time = parse_now(now.to_s)
174
+ assert_equal now.to_s, time.to_s
175
+
176
+ # rm_sd_rt
177
+
178
+ #time = parse_now("jan 5 13:00")
179
+ #assert_equal Time.local(2007, 1, 5, 13), time
180
+
181
+ # due to limitations of the Time class, these don't work
182
+
183
+ time = parse_now("may 40")
184
+ assert_equal nil, time
185
+
186
+ time = parse_now("may 27 40")
187
+ assert_equal nil, time
188
+
189
+ time = parse_now("1800-08-20")
190
+ assert_equal nil, time
191
+ end
192
+
193
+ def test_parse_guess_r
194
+ time = parse_now("friday")
195
+ assert_equal Time.local(2006, 8, 18, 12), time
196
+
197
+ time = parse_now("tue")
198
+ assert_equal Time.local(2006, 8, 22, 12), time
199
+
200
+ time = parse_now("5")
201
+ assert_equal Time.local(2006, 8, 16, 17), time
202
+
203
+ time = Chronic.parse("5", :now => Time.local(2006, 8, 16, 3, 0, 0, 0), :ambiguous_time_range => :none)
204
+ assert_equal Time.local(2006, 8, 16, 5), time
205
+
206
+ time = parse_now("13:00")
207
+ assert_equal Time.local(2006, 8, 17, 13), time
208
+
209
+ time = parse_now("13:45")
210
+ assert_equal Time.local(2006, 8, 17, 13, 45), time
211
+
212
+ time = parse_now("november")
213
+ assert_equal Time.local(2006, 11, 16), time
214
+ end
215
+
216
+ def test_parse_guess_rr
217
+ time = parse_now("friday 13:00")
218
+ assert_equal Time.local(2006, 8, 18, 13), time
219
+
220
+ time = parse_now("monday 4:00")
221
+ assert_equal Time.local(2006, 8, 21, 16), time
222
+
223
+ time = parse_now("sat 4:00", :ambiguous_time_range => :none)
224
+ assert_equal Time.local(2006, 8, 19, 4), time
225
+
226
+ time = parse_now("sunday 4:20", :ambiguous_time_range => :none)
227
+ assert_equal Time.local(2006, 8, 20, 4, 20), time
228
+
229
+ time = parse_now("4 pm")
230
+ assert_equal Time.local(2006, 8, 16, 16), time
231
+
232
+ time = parse_now("4 am", :ambiguous_time_range => :none)
233
+ assert_equal Time.local(2006, 8, 16, 4), time
234
+
235
+ time = parse_now("12 pm")
236
+ assert_equal Time.local(2006, 8, 16, 12), time
237
+
238
+ time = parse_now("12:01 pm")
239
+ assert_equal Time.local(2006, 8, 16, 12, 1), time
240
+
241
+ time = parse_now("12:01 am")
242
+ assert_equal Time.local(2006, 8, 16, 0, 1), time
243
+
244
+ time = parse_now("12 am")
245
+ assert_equal Time.local(2006, 8, 16), time
246
+
247
+ time = parse_now("4:00 in the morning")
248
+ assert_equal Time.local(2006, 8, 16, 4), time
249
+
250
+ time = parse_now("november 4")
251
+ assert_equal Time.local(2006, 11, 4, 12), time
252
+
253
+ time = parse_now("aug 24")
254
+ assert_equal Time.local(2006, 8, 24, 12), time
255
+ end
256
+
257
+ def test_parse_guess_rrr
258
+ time = parse_now("friday 1 pm")
259
+ assert_equal Time.local(2006, 8, 18, 13), time
260
+
261
+ time = parse_now("friday 11 at night")
262
+ assert_equal Time.local(2006, 8, 18, 23), time
263
+
264
+ time = parse_now("friday 11 in the evening")
265
+ assert_equal Time.local(2006, 8, 18, 23), time
266
+
267
+ time = parse_now("sunday 6am")
268
+ assert_equal Time.local(2006, 8, 20, 6), time
269
+
270
+ time = parse_now("friday evening at 7")
271
+ assert_equal Time.local(2006, 8, 18, 19), time
272
+ end
273
+
274
+ def test_parse_guess_gr
275
+ # year
276
+
277
+ time = parse_now("this year")
278
+ assert_equal Time.local(2006, 10, 24, 12, 30), time
279
+
280
+ time = parse_now("this year", :context => :past)
281
+ assert_equal Time.local(2006, 4, 24, 12, 30), time
282
+
283
+ # month
284
+
285
+ time = parse_now("this month")
286
+ assert_equal Time.local(2006, 8, 24, 12), time
287
+
288
+ time = parse_now("this month", :context => :past)
289
+ assert_equal Time.local(2006, 8, 8, 12), time
290
+
291
+ time = Chronic.parse("next month", :now => Time.local(2006, 11, 15))
292
+ assert_equal Time.local(2006, 12, 16, 12), time
293
+
294
+ # month name
295
+
296
+ time = parse_now("last november")
297
+ assert_equal Time.local(2005, 11, 16), time
298
+
299
+ # fortnight
300
+
301
+ time = parse_now("this fortnight")
302
+ assert_equal Time.local(2006, 8, 21, 19, 30), time
303
+
304
+ time = parse_now("this fortnight", :context => :past)
305
+ assert_equal Time.local(2006, 8, 14, 19), time
306
+
307
+ # week
308
+
309
+ time = parse_now("this week")
310
+ assert_equal Time.local(2006, 8, 18, 7, 30), time
311
+
312
+ time = parse_now("this week", :context => :past)
313
+ assert_equal Time.local(2006, 8, 14, 19), time
314
+
315
+ # weekend
316
+
317
+ time = parse_now("this weekend")
318
+ assert_equal Time.local(2006, 8, 20), time
319
+
320
+ time = parse_now("this weekend", :context => :past)
321
+ assert_equal Time.local(2006, 8, 13), time
322
+
323
+ time = parse_now("last weekend")
324
+ assert_equal Time.local(2006, 8, 13), time
325
+
326
+ # day
327
+
328
+ time = parse_now("this day")
329
+ assert_equal Time.local(2006, 8, 16, 19, 30), time
330
+
331
+ time = parse_now("this day", :context => :past)
332
+ assert_equal Time.local(2006, 8, 16, 7), time
333
+
334
+ time = parse_now("today")
335
+ assert_equal Time.local(2006, 8, 16, 19, 30), time
336
+
337
+ time = parse_now("yesterday")
338
+ assert_equal Time.local(2006, 8, 15, 12), time
339
+
340
+ time = parse_now("tomorrow")
341
+ assert_equal Time.local(2006, 8, 17, 12), time
342
+
343
+ # day name
344
+
345
+ time = parse_now("this tuesday")
346
+ assert_equal Time.local(2006, 8, 22, 12), time
347
+
348
+ time = parse_now("next tuesday")
349
+ assert_equal Time.local(2006, 8, 22, 12), time
350
+
351
+ time = parse_now("last tuesday")
352
+ assert_equal Time.local(2006, 8, 15, 12), time
353
+
354
+ time = parse_now("this wed")
355
+ assert_equal Time.local(2006, 8, 23, 12), time
356
+
357
+ time = parse_now("next wed")
358
+ assert_equal Time.local(2006, 8, 23, 12), time
359
+
360
+ time = parse_now("last wed")
361
+ assert_equal Time.local(2006, 8, 9, 12), time
362
+
363
+ # day portion
364
+
365
+ time = parse_now("this morning")
366
+ assert_equal Time.local(2006, 8, 16, 9), time
367
+
368
+ time = parse_now("tonight")
369
+ assert_equal Time.local(2006, 8, 16, 22), time
370
+
371
+ # minute
372
+
373
+ time = parse_now("next minute")
374
+ assert_equal Time.local(2006, 8, 16, 14, 1, 30), time
375
+
376
+ # second
377
+
378
+ time = parse_now("this second")
379
+ assert_equal Time.local(2006, 8, 16, 14), time
380
+
381
+ time = parse_now("this second", :context => :past)
382
+ assert_equal Time.local(2006, 8, 16, 14), time
383
+
384
+ time = parse_now("next second")
385
+ assert_equal Time.local(2006, 8, 16, 14, 0, 1), time
386
+
387
+ time = parse_now("last second")
388
+ assert_equal Time.local(2006, 8, 16, 13, 59, 59), time
389
+ end
390
+
391
+ def test_parse_guess_grr
392
+ time = parse_now("yesterday at 4:00")
393
+ assert_equal Time.local(2006, 8, 15, 16), time
394
+
395
+ time = parse_now("today at 9:00")
396
+ assert_equal Time.local(2006, 8, 16, 9), time
397
+
398
+ time = parse_now("today at 2100")
399
+ assert_equal Time.local(2006, 8, 16, 21), time
400
+
401
+ time = parse_now("this day at 0900")
402
+ assert_equal Time.local(2006, 8, 16, 9), time
403
+
404
+ time = parse_now("tomorrow at 0900")
405
+ assert_equal Time.local(2006, 8, 17, 9), time
406
+
407
+ time = parse_now("yesterday at 4:00", :ambiguous_time_range => :none)
408
+ assert_equal Time.local(2006, 8, 15, 4), time
409
+
410
+ time = parse_now("last friday at 4:00")
411
+ assert_equal Time.local(2006, 8, 11, 16), time
412
+
413
+ time = parse_now("next wed 4:00")
414
+ assert_equal Time.local(2006, 8, 23, 16), time
415
+
416
+ time = parse_now("yesterday afternoon")
417
+ assert_equal Time.local(2006, 8, 15, 15), time
418
+
419
+ time = parse_now("last week tuesday")
420
+ assert_equal Time.local(2006, 8, 8, 12), time
421
+
422
+ time = parse_now("tonight at 7")
423
+ assert_equal Time.local(2006, 8, 16, 19), time
424
+
425
+ time = parse_now("tonight 7")
426
+ assert_equal Time.local(2006, 8, 16, 19), time
427
+
428
+ time = parse_now("7 tonight")
429
+ assert_equal Time.local(2006, 8, 16, 19), time
430
+ end
431
+
432
+ def test_parse_guess_grrr
433
+ time = parse_now("today at 6:00pm")
434
+ assert_equal Time.local(2006, 8, 16, 18), time
435
+
436
+ time = parse_now("today at 6:00am")
437
+ assert_equal Time.local(2006, 8, 16, 6), time
438
+
439
+ time = parse_now("this day 1800")
440
+ assert_equal Time.local(2006, 8, 16, 18), time
441
+
442
+ time = parse_now("yesterday at 4:00pm")
443
+ assert_equal Time.local(2006, 8, 15, 16), time
444
+
445
+ time = parse_now("tomorrow evening at 7")
446
+ assert_equal Time.local(2006, 8, 17, 19), time
447
+
448
+ time = parse_now("tomorrow morning at 5:30")
449
+ assert_equal Time.local(2006, 8, 17, 5, 30), time
450
+
451
+ time = parse_now("next monday at 12:01 am")
452
+ assert_equal Time.local(2006, 8, 21, 00, 1), time
453
+
454
+ time = parse_now("next monday at 12:01 pm")
455
+ assert_equal Time.local(2006, 8, 21, 12, 1), time
456
+ end
457
+
458
+ def test_parse_guess_rgr
459
+ time = parse_now("afternoon yesterday")
460
+ assert_equal Time.local(2006, 8, 15, 15), time
461
+
462
+ time = parse_now("tuesday last week")
463
+ assert_equal Time.local(2006, 8, 8, 12), time
464
+ end
465
+
466
+ def test_parse_guess_s_r_p
467
+ # past
468
+
469
+ time = parse_now("3 years ago")
470
+ assert_equal Time.local(2003, 8, 16, 14), time
471
+
472
+ time = parse_now("1 month ago")
473
+ assert_equal Time.local(2006, 7, 16, 14), time
474
+
475
+ time = parse_now("1 fortnight ago")
476
+ assert_equal Time.local(2006, 8, 2, 14), time
477
+
478
+ time = parse_now("2 fortnights ago")
479
+ assert_equal Time.local(2006, 7, 19, 14), time
480
+
481
+ time = parse_now("3 weeks ago")
482
+ assert_equal Time.local(2006, 7, 26, 14), time
483
+
484
+ time = parse_now("2 weekends ago")
485
+ assert_equal Time.local(2006, 8, 5), time
486
+
487
+ time = parse_now("3 days ago")
488
+ assert_equal Time.local(2006, 8, 13, 14), time
489
+
490
+ #time = parse_now("1 monday ago")
491
+ #assert_equal Time.local(2006, 8, 14, 12), time
492
+
493
+ time = parse_now("5 mornings ago")
494
+ assert_equal Time.local(2006, 8, 12, 9), time
495
+
496
+ time = parse_now("7 hours ago")
497
+ assert_equal Time.local(2006, 8, 16, 7), time
498
+
499
+ time = parse_now("3 minutes ago")
500
+ assert_equal Time.local(2006, 8, 16, 13, 57), time
501
+
502
+ time = parse_now("20 seconds before now")
503
+ assert_equal Time.local(2006, 8, 16, 13, 59, 40), time
504
+
505
+ # future
506
+
507
+ time = parse_now("3 years from now")
508
+ assert_equal Time.local(2009, 8, 16, 14, 0, 0), time
509
+
510
+ time = parse_now("6 months hence")
511
+ assert_equal Time.local(2007, 2, 16, 14), time
512
+
513
+ time = parse_now("3 fortnights hence")
514
+ assert_equal Time.local(2006, 9, 27, 14), time
515
+
516
+ time = parse_now("1 week from now")
517
+ assert_equal Time.local(2006, 8, 23, 14, 0, 0), time
518
+
519
+ time = parse_now("1 weekend from now")
520
+ assert_equal Time.local(2006, 8, 19), time
521
+
522
+ time = parse_now("2 weekends from now")
523
+ assert_equal Time.local(2006, 8, 26), time
524
+
525
+ time = parse_now("1 day hence")
526
+ assert_equal Time.local(2006, 8, 17, 14), time
527
+
528
+ time = parse_now("5 mornings hence")
529
+ assert_equal Time.local(2006, 8, 21, 9), time
530
+
531
+ time = parse_now("1 hour from now")
532
+ assert_equal Time.local(2006, 8, 16, 15), time
533
+
534
+ time = parse_now("20 minutes hence")
535
+ assert_equal Time.local(2006, 8, 16, 14, 20), time
536
+
537
+ time = parse_now("20 seconds from now")
538
+ assert_equal Time.local(2006, 8, 16, 14, 0, 20), time
539
+
540
+ time = Chronic.parse("2 months ago", :now => Time.parse("2007-03-07 23:30"))
541
+ assert_equal Time.local(2007, 1, 7, 23, 30), time
542
+ end
543
+
544
+ def test_parse_guess_p_s_r
545
+ time = parse_now("in 3 hours")
546
+ assert_equal Time.local(2006, 8, 16, 17), time
547
+ end
548
+
549
+ def test_parse_guess_s_r_p_a
550
+ # past
551
+
552
+ time = parse_now("3 years ago tomorrow")
553
+ assert_equal Time.local(2003, 8, 17, 12), time
554
+
555
+ time = parse_now("3 years ago this friday")
556
+ assert_equal Time.local(2003, 8, 18, 12), time
557
+
558
+ time = parse_now("3 months ago saturday at 5:00 pm")
559
+ assert_equal Time.local(2006, 5, 19, 17), time
560
+
561
+ time = parse_now("2 days from this second")
562
+ assert_equal Time.local(2006, 8, 18, 14), time
563
+
564
+ time = parse_now("7 hours before tomorrow at midnight")
565
+ assert_equal Time.local(2006, 8, 17, 17), time
566
+
567
+ # future
568
+ end
569
+
570
+ def test_parse_guess_o_r_s_r
571
+ time = parse_now("3rd wednesday in november")
572
+ assert_equal Time.local(2006, 11, 15, 12), time
573
+
574
+ time = parse_now("10th wednesday in november")
575
+ assert_equal nil, time
576
+
577
+ # time = parse_now("3rd wednesday in 2007")
578
+ # assert_equal Time.local(2007, 1, 20, 12), time
579
+ end
580
+
581
+ def test_parse_guess_o_r_g_r
582
+ time = parse_now("3rd month next year")
583
+ assert_equal Time.local(2007, 3, 16, 12, 30), time
584
+
585
+ time = parse_now("3rd thursday this september")
586
+ assert_equal Time.local(2006, 9, 21, 12), time
587
+
588
+ time = parse_now("4th day last week")
589
+ assert_equal Time.local(2006, 8, 9, 12), time
590
+ end
591
+
592
+ def test_parse_guess_nonsense
593
+ time = parse_now("some stupid nonsense")
594
+ assert_equal nil, time
595
+
596
+ time = parse_now("Ham Sandwich")
597
+ assert_equal nil, time
598
+ end
599
+
600
+ def test_parse_span
601
+ span = parse_now("friday", :guess => false)
602
+ assert_equal Time.local(2006, 8, 18), span.begin
603
+ assert_equal Time.local(2006, 8, 19), span.end
604
+
605
+ span = parse_now("november", :guess => false)
606
+ assert_equal Time.local(2006, 11), span.begin
607
+ assert_equal Time.local(2006, 12), span.end
608
+
609
+ span = Chronic.parse("weekend" , :now => @time_2006_08_16_14_00_00, :guess => false)
610
+ assert_equal Time.local(2006, 8, 19), span.begin
611
+ assert_equal Time.local(2006, 8, 21), span.end
612
+ end
613
+
614
+ def test_parse_with_endian_precedence
615
+ date = '11/02/2007'
616
+
617
+ expect_for_middle_endian = Time.local(2007, 11, 2, 12)
618
+ expect_for_little_endian = Time.local(2007, 2, 11, 12)
619
+
620
+ # default precedence should be toward middle endianness
621
+ assert_equal expect_for_middle_endian, Chronic.parse(date)
622
+
623
+ assert_equal expect_for_middle_endian, Chronic.parse(date, :endian_precedence => [:middle, :little])
624
+
625
+ assert_equal expect_for_little_endian, Chronic.parse(date, :endian_precedence => [:little, :middle])
626
+ end
627
+
628
+ def test_parse_words
629
+ assert_equal parse_now("33 days from now"), parse_now("thirty-three days from now")
630
+ assert_equal parse_now("2867532 seconds from now"), parse_now("two million eight hundred and sixty seven thousand five hundred and thirty two seconds from now")
631
+ assert_equal parse_now("may 10th"), parse_now("may tenth")
632
+ end
633
+
634
+ def test_parse_only_complete_pointers
635
+ assert_equal parse_now("eat pasty buns today at 2pm"), @time_2006_08_16_14_00_00
636
+ assert_equal parse_now("futuristically speaking today at 2pm"), @time_2006_08_16_14_00_00
637
+ assert_equal parse_now("meeting today at 2pm"), @time_2006_08_16_14_00_00
638
+ end
639
+
640
+ def test_am_pm
641
+ assert_equal Time.local(2006, 8, 16), parse_now("8/16/2006 at 12am")
642
+ assert_equal Time.local(2006, 8, 16, 12), parse_now("8/16/2006 at 12pm")
643
+ end
644
+
645
+ def test_a_p
646
+ assert_equal Time.local(2006, 8, 16, 0, 15), parse_now("8/16/2006 at 12:15a")
647
+ assert_equal Time.local(2006, 8, 16, 18, 30), parse_now("8/16/2006 at 6:30p")
648
+ end
649
+
650
+ def test_argument_validation
651
+ assert_raise(Chronic::InvalidArgumentException) do
652
+ time = Chronic.parse("may 27", :foo => :bar)
653
+ end
654
+
655
+ assert_raise(Chronic::InvalidArgumentException) do
656
+ time = Chronic.parse("may 27", :context => :bar)
657
+ end
658
+ end
659
+
660
+ def test_seasons
661
+ t = parse_now("this spring", :guess => false)
662
+ assert_equal Time.local(2007, 3, 20), t.begin
663
+ assert_equal Time.local(2007, 6, 20), t.end
664
+
665
+ t = parse_now("this winter", :guess => false)
666
+ assert_equal Time.local(2006, 12, 22, 23), t.begin
667
+ assert_equal Time.local(2007, 3, 19), t.end
668
+
669
+ t = parse_now("last spring", :guess => false)
670
+ assert_equal Time.local(2006, 3, 20, 23), t.begin
671
+ assert_equal Time.local(2006, 6, 20), t.end
672
+
673
+ t = parse_now("last winter", :guess => false)
674
+ assert_equal Time.local(2005, 12, 22, 23), t.begin
675
+ assert_equal Time.local(2006, 3, 19, 23), t.end
676
+
677
+ t = parse_now("next spring", :guess => false)
678
+ assert_equal Time.local(2007, 3, 20), t.begin
679
+ assert_equal Time.local(2007, 6, 20), t.end
680
+ end
681
+
682
+ # regression
683
+
684
+ # def test_partial
685
+ # assert_equal '', parse_now("2 hours")
686
+ # end
687
+
688
+ def test_days_in_november
689
+ t1 = Chronic.parse('1st thursday in november', :now => Time.local(2007))
690
+ assert_equal Time.local(2007, 11, 1, 12), t1
691
+
692
+ t1 = Chronic.parse('1st friday in november', :now => Time.local(2007))
693
+ assert_equal Time.local(2007, 11, 2, 12), t1
694
+
695
+ t1 = Chronic.parse('1st saturday in november', :now => Time.local(2007))
696
+ assert_equal Time.local(2007, 11, 3, 12), t1
697
+
698
+ t1 = Chronic.parse('1st sunday in november', :now => Time.local(2007))
699
+ assert_equal Time.local(2007, 11, 4, 11), t1
700
+
701
+ # Chronic.debug = true
702
+ #
703
+ # t1 = Chronic.parse('1st monday in november', :now => Time.local(2007))
704
+ # assert_equal Time.local(2007, 11, 5, 11), t1
705
+ end
706
+
707
+ private
708
+ def parse_now(string, options={})
709
+ Chronic.parse(string, {:now => TIME_2006_08_16_14_00_00 }.merge(options))
710
+ end
711
+ end