aaronh-chronic 0.3.9

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.
Files changed (47) hide show
  1. data/README.txt +167 -0
  2. data/lib/chronic.rb +134 -0
  3. data/lib/chronic/chronic.rb +262 -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 +75 -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 +125 -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 +95 -0
  26. data/lib/chronic/separator.rb +91 -0
  27. data/lib/chronic/time_zone.rb +26 -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 +731 -0
  47. metadata +110 -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,731 @@
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
+ time = parse_now("5/24/10")
126
+ assert_equal Time.local(2010, 5, 24, 12), time
127
+
128
+ # sd_sm_sy
129
+
130
+ time = parse_now("27/5/1979")
131
+ assert_equal Time.local(1979, 5, 27, 12), time
132
+
133
+ time = parse_now("27/5/1979 @ 0700")
134
+ assert_equal Time.local(1979, 5, 27, 7), time
135
+
136
+ # sm_sy
137
+
138
+ time = parse_now("05/06")
139
+ assert_equal Time.local(2006, 5, 16, 12), time
140
+
141
+ time = parse_now("12/06")
142
+ assert_equal Time.local(2006, 12, 16, 12), time
143
+
144
+ time = parse_now("13/06")
145
+ assert_equal nil, time
146
+
147
+
148
+ # sy_sm_sd
149
+
150
+ time = parse_now("2000-1-1")
151
+ assert_equal Time.local(2000, 1, 1, 12), time
152
+
153
+ time = parse_now("2006-08-20")
154
+ assert_equal Time.local(2006, 8, 20, 12), time
155
+
156
+ time = parse_now("2006-08-20 7pm")
157
+ assert_equal Time.local(2006, 8, 20, 19), time
158
+
159
+ time = parse_now("2006-08-20 03:00")
160
+ assert_equal Time.local(2006, 8, 20, 3), time
161
+
162
+ time = parse_now("2006-08-20 03:30:30")
163
+ assert_equal Time.local(2006, 8, 20, 3, 30, 30), time
164
+
165
+ time = parse_now("2006-08-20 15:30:30")
166
+ assert_equal Time.local(2006, 8, 20, 15, 30, 30), time
167
+
168
+ time = parse_now("2006-08-20 15:30.30")
169
+ assert_equal Time.local(2006, 8, 20, 15, 30, 30), time
170
+
171
+ # rdn_rm_rd_rt_rtz_ry
172
+
173
+ time = parse_now("Mon Apr 02 17:00:00 PDT 2007")
174
+ assert_equal 1175558400, time.to_i
175
+
176
+ now = Time.now
177
+ time = parse_now(now.to_s)
178
+ assert_equal now.to_s, time.to_s
179
+
180
+ # rm_sd_rt
181
+
182
+ time = parse_now("jan 5 13:00")
183
+ assert_equal Time.local(2007, 1, 5, 13), time
184
+
185
+ time = parse_now("may 40")
186
+ assert_equal Time.local(1940, 5, 16, 12), time
187
+
188
+ time = parse_now("may 27 40")
189
+ assert_equal Time.local(1940, 5, 27, 12), time
190
+
191
+ time = parse_now("1800-08-20")
192
+ assert_equal Time.local(1800, 8, 20, 12), time
193
+ end
194
+
195
+ def test_parse_guess_r
196
+ time = parse_now("friday")
197
+ assert_equal Time.local(2006, 8, 18, 12), time
198
+
199
+ time = parse_now("tue")
200
+ assert_equal Time.local(2006, 8, 22, 12), time
201
+
202
+ time = parse_now("5")
203
+ assert_equal Time.local(2006, 8, 16, 17), time
204
+
205
+ time = Chronic.parse("5", :now => Time.local(2006, 8, 16, 3, 0, 0, 0), :ambiguous_time_range => :none)
206
+ assert_equal Time.local(2006, 8, 16, 5), time
207
+
208
+ time = parse_now("13:00")
209
+ assert_equal Time.local(2006, 8, 17, 13), time
210
+
211
+ time = parse_now("13:45")
212
+ assert_equal Time.local(2006, 8, 17, 13, 45), time
213
+
214
+ time = parse_now("november")
215
+ assert_equal Time.local(2006, 11, 16), time
216
+
217
+ time = parse_now("1234")
218
+ assert_equal Time.local(2006, 8, 17, 12, 34), time
219
+
220
+ time = Chronic.parse("1234", :now => Time.local(2006, 8, 16, 3, 0, 0, 0), :ambiguous_time_range => :none)
221
+ assert_equal Time.local(2006, 8, 16, 12, 34), time
222
+
223
+ time = parse_now("0034")
224
+ assert_equal Time.local(2006, 8, 17, 0, 34), time
225
+
226
+ time = parse_now("1730")
227
+ assert_equal Time.local(2006, 8, 16, 17, 30), time
228
+ end
229
+
230
+ def test_parse_guess_rr
231
+ time = parse_now("friday 13:00")
232
+ assert_equal Time.local(2006, 8, 18, 13), time
233
+
234
+ time = parse_now("monday 4:00")
235
+ assert_equal Time.local(2006, 8, 21, 16), time
236
+
237
+ time = parse_now("sat 4:00", :ambiguous_time_range => :none)
238
+ assert_equal Time.local(2006, 8, 19, 4), time
239
+
240
+ time = parse_now("sunday 4:20", :ambiguous_time_range => :none)
241
+ assert_equal Time.local(2006, 8, 20, 4, 20), time
242
+
243
+ time = parse_now("4 pm")
244
+ assert_equal Time.local(2006, 8, 16, 16), time
245
+
246
+ time = parse_now("4 am", :ambiguous_time_range => :none)
247
+ assert_equal Time.local(2006, 8, 16, 4), time
248
+
249
+ time = parse_now("12 pm")
250
+ assert_equal Time.local(2006, 8, 16, 12), time
251
+
252
+ time = parse_now("12:01 pm")
253
+ assert_equal Time.local(2006, 8, 16, 12, 1), time
254
+
255
+ time = parse_now("12:01 am")
256
+ assert_equal Time.local(2006, 8, 16, 0, 1), time
257
+
258
+ time = parse_now("12 am")
259
+ assert_equal Time.local(2006, 8, 16), time
260
+
261
+ time = parse_now("4:00 in the morning")
262
+ assert_equal Time.local(2006, 8, 16, 4), time
263
+
264
+ time = parse_now("november 4")
265
+ assert_equal Time.local(2006, 11, 4, 12), time
266
+
267
+ time = parse_now("aug 24")
268
+ assert_equal Time.local(2006, 8, 24, 12), time
269
+ end
270
+
271
+ def test_parse_guess_rrr
272
+ time = parse_now("friday 1 pm")
273
+ assert_equal Time.local(2006, 8, 18, 13), time
274
+
275
+ time = parse_now("friday 11 at night")
276
+ assert_equal Time.local(2006, 8, 18, 23), time
277
+
278
+ time = parse_now("friday 11 in the evening")
279
+ assert_equal Time.local(2006, 8, 18, 23), time
280
+
281
+ time = parse_now("sunday 6am")
282
+ assert_equal Time.local(2006, 8, 20, 6), time
283
+
284
+ time = parse_now("friday evening at 7")
285
+ assert_equal Time.local(2006, 8, 18, 19), time
286
+ end
287
+
288
+ def test_parse_guess_gr
289
+ # year
290
+
291
+ time = parse_now("this year")
292
+ assert_equal Time.local(2006, 10, 24, 12, 30), time
293
+
294
+ time = parse_now("this year", :context => :past)
295
+ assert_equal Time.local(2006, 4, 24, 12, 30), time
296
+
297
+ # month
298
+
299
+ time = parse_now("this month")
300
+ assert_equal Time.local(2006, 8, 24, 12), time
301
+
302
+ time = parse_now("this month", :context => :past)
303
+ assert_equal Time.local(2006, 8, 8, 12), time
304
+
305
+ time = Chronic.parse("next month", :now => Time.local(2006, 11, 15))
306
+ assert_equal Time.local(2006, 12, 16, 12), time
307
+
308
+ # month name
309
+
310
+ time = parse_now("last november")
311
+ assert_equal Time.local(2005, 11, 16), time
312
+
313
+ # fortnight
314
+
315
+ time = parse_now("this fortnight")
316
+ assert_equal Time.local(2006, 8, 21, 19, 30), time
317
+
318
+ time = parse_now("this fortnight", :context => :past)
319
+ assert_equal Time.local(2006, 8, 14, 19), time
320
+
321
+ # week
322
+
323
+ time = parse_now("this week")
324
+ assert_equal Time.local(2006, 8, 18, 7, 30), time
325
+
326
+ time = parse_now("this week", :context => :past)
327
+ assert_equal Time.local(2006, 8, 14, 19), time
328
+
329
+ # weekend
330
+
331
+ time = parse_now("this weekend")
332
+ assert_equal Time.local(2006, 8, 20), time
333
+
334
+ time = parse_now("this weekend", :context => :past)
335
+ assert_equal Time.local(2006, 8, 13), time
336
+
337
+ time = parse_now("last weekend")
338
+ assert_equal Time.local(2006, 8, 13), time
339
+
340
+ # day
341
+
342
+ time = parse_now("this day")
343
+ assert_equal Time.local(2006, 8, 16, 19, 30), time
344
+
345
+ time = parse_now("this day", :context => :past)
346
+ assert_equal Time.local(2006, 8, 16, 7), time
347
+
348
+ time = parse_now("today")
349
+ assert_equal Time.local(2006, 8, 16, 19, 30), time
350
+
351
+ time = parse_now("yesterday")
352
+ assert_equal Time.local(2006, 8, 15, 12), time
353
+
354
+ time = parse_now("tomorrow")
355
+ assert_equal Time.local(2006, 8, 17, 12), time
356
+
357
+ # day name
358
+
359
+ time = parse_now("this tuesday")
360
+ assert_equal Time.local(2006, 8, 22, 12), time
361
+
362
+ time = parse_now("next tuesday")
363
+ assert_equal Time.local(2006, 8, 22, 12), time
364
+
365
+ time = parse_now("last tuesday")
366
+ assert_equal Time.local(2006, 8, 15, 12), time
367
+
368
+ time = parse_now("this wed")
369
+ assert_equal Time.local(2006, 8, 23, 12), time
370
+
371
+ time = parse_now("next wed")
372
+ assert_equal Time.local(2006, 8, 23, 12), time
373
+
374
+ time = parse_now("last wed")
375
+ assert_equal Time.local(2006, 8, 9, 12), time
376
+
377
+ # day portion
378
+
379
+ time = parse_now("this morning")
380
+ assert_equal Time.local(2006, 8, 16, 9), time
381
+
382
+ time = parse_now("tonight")
383
+ assert_equal Time.local(2006, 8, 16, 22), time
384
+
385
+ # minute
386
+
387
+ time = parse_now("next minute")
388
+ assert_equal Time.local(2006, 8, 16, 14, 1, 30), time
389
+
390
+ # second
391
+
392
+ time = parse_now("this second")
393
+ assert_equal Time.local(2006, 8, 16, 14), time
394
+
395
+ time = parse_now("this second", :context => :past)
396
+ assert_equal Time.local(2006, 8, 16, 14), time
397
+
398
+ time = parse_now("next second")
399
+ assert_equal Time.local(2006, 8, 16, 14, 0, 1), time
400
+
401
+ time = parse_now("last second")
402
+ assert_equal Time.local(2006, 8, 16, 13, 59, 59), time
403
+ end
404
+
405
+ def test_parse_guess_grr
406
+ time = parse_now("yesterday at 4:00")
407
+ assert_equal Time.local(2006, 8, 15, 16), time
408
+
409
+ time = parse_now("today at 9:00")
410
+ assert_equal Time.local(2006, 8, 16, 9), time
411
+
412
+ time = parse_now("today at 2100")
413
+ assert_equal Time.local(2006, 8, 16, 21), time
414
+
415
+ time = parse_now("this day at 0900")
416
+ assert_equal Time.local(2006, 8, 16, 9), time
417
+
418
+ time = parse_now("tomorrow at 0900")
419
+ assert_equal Time.local(2006, 8, 17, 9), time
420
+
421
+ time = parse_now("yesterday at 4:00", :ambiguous_time_range => :none)
422
+ assert_equal Time.local(2006, 8, 15, 4), time
423
+
424
+ time = parse_now("last friday at 4:00")
425
+ assert_equal Time.local(2006, 8, 11, 16), time
426
+
427
+ time = parse_now("next wed 4:00")
428
+ assert_equal Time.local(2006, 8, 23, 16), time
429
+
430
+ time = parse_now("yesterday afternoon")
431
+ assert_equal Time.local(2006, 8, 15, 15), time
432
+
433
+ time = parse_now("last week tuesday")
434
+ assert_equal Time.local(2006, 8, 8, 12), time
435
+
436
+ time = parse_now("tonight at 7")
437
+ assert_equal Time.local(2006, 8, 16, 19), time
438
+
439
+ time = parse_now("tonight 7")
440
+ assert_equal Time.local(2006, 8, 16, 19), time
441
+
442
+ time = parse_now("7 tonight")
443
+ assert_equal Time.local(2006, 8, 16, 19), time
444
+ end
445
+
446
+ def test_parse_guess_grrr
447
+ time = parse_now("today at 6:00pm")
448
+ assert_equal Time.local(2006, 8, 16, 18), time
449
+
450
+ time = parse_now("today at 6:00am")
451
+ assert_equal Time.local(2006, 8, 16, 6), time
452
+
453
+ time = parse_now("this day 1800")
454
+ assert_equal Time.local(2006, 8, 16, 18), time
455
+
456
+ time = parse_now("yesterday at 4:00pm")
457
+ assert_equal Time.local(2006, 8, 15, 16), time
458
+
459
+ time = parse_now("tomorrow evening at 7")
460
+ assert_equal Time.local(2006, 8, 17, 19), time
461
+
462
+ time = parse_now("tomorrow morning at 5:30")
463
+ assert_equal Time.local(2006, 8, 17, 5, 30), time
464
+
465
+ time = parse_now("next monday at 12:01 am")
466
+ assert_equal Time.local(2006, 8, 21, 00, 1), time
467
+
468
+ time = parse_now("next monday at 12:01 pm")
469
+ assert_equal Time.local(2006, 8, 21, 12, 1), time
470
+ end
471
+
472
+ def test_parse_guess_rgr
473
+ time = parse_now("afternoon yesterday")
474
+ assert_equal Time.local(2006, 8, 15, 15), time
475
+
476
+ time = parse_now("tuesday last week")
477
+ assert_equal Time.local(2006, 8, 8, 12), time
478
+ end
479
+
480
+ def test_parse_guess_s_r_p
481
+ # past
482
+
483
+ time = parse_now("3 years ago")
484
+ assert_equal Time.local(2003, 8, 16, 14), time
485
+
486
+ time = parse_now("1 month ago")
487
+ assert_equal Time.local(2006, 7, 16, 14), time
488
+
489
+ time = parse_now("1 month ago", :now => Time.local(2010,05,31,14))
490
+ assert_equal Time.local(2010, 04, 30, 14), time
491
+
492
+ time = parse_now("1 month ago", :now => Time.local(2010,03,31,14))
493
+ assert_equal Time.local(2010, 02, 28, 14), time
494
+
495
+ time = parse_now("1 fortnight ago")
496
+ assert_equal Time.local(2006, 8, 2, 14), time
497
+
498
+ time = parse_now("2 fortnights ago")
499
+ assert_equal Time.local(2006, 7, 19, 14), time
500
+
501
+ time = parse_now("3 weeks ago")
502
+ assert_equal Time.local(2006, 7, 26, 14), time
503
+
504
+ time = parse_now("2 weekends ago")
505
+ assert_equal Time.local(2006, 8, 5), time
506
+
507
+ time = parse_now("3 days ago")
508
+ assert_equal Time.local(2006, 8, 13, 14), time
509
+
510
+ #time = parse_now("1 monday ago")
511
+ #assert_equal Time.local(2006, 8, 14, 12), time
512
+
513
+ time = parse_now("5 mornings ago")
514
+ assert_equal Time.local(2006, 8, 12, 9), time
515
+
516
+ time = parse_now("7 hours ago")
517
+ assert_equal Time.local(2006, 8, 16, 7), time
518
+
519
+ time = parse_now("3 minutes ago")
520
+ assert_equal Time.local(2006, 8, 16, 13, 57), time
521
+
522
+ time = parse_now("20 seconds before now")
523
+ assert_equal Time.local(2006, 8, 16, 13, 59, 40), time
524
+
525
+ # future
526
+
527
+ time = parse_now("3 years from now")
528
+ assert_equal Time.local(2009, 8, 16, 14, 0, 0), time
529
+
530
+ time = parse_now("6 months hence")
531
+ assert_equal Time.local(2007, 2, 16, 14), time
532
+
533
+ time = parse_now("3 fortnights hence")
534
+ assert_equal Time.local(2006, 9, 27, 14), time
535
+
536
+ time = parse_now("1 week from now")
537
+ assert_equal Time.local(2006, 8, 23, 14, 0, 0), time
538
+
539
+ time = parse_now("1 weekend from now")
540
+ assert_equal Time.local(2006, 8, 19), time
541
+
542
+ time = parse_now("2 weekends from now")
543
+ assert_equal Time.local(2006, 8, 26), time
544
+
545
+ time = parse_now("1 day hence")
546
+ assert_equal Time.local(2006, 8, 17, 14), time
547
+
548
+ time = parse_now("5 mornings hence")
549
+ assert_equal Time.local(2006, 8, 21, 9), time
550
+
551
+ time = parse_now("1 hour from now")
552
+ assert_equal Time.local(2006, 8, 16, 15), time
553
+
554
+ time = parse_now("20 minutes hence")
555
+ assert_equal Time.local(2006, 8, 16, 14, 20), time
556
+
557
+ time = parse_now("20 seconds from now")
558
+ assert_equal Time.local(2006, 8, 16, 14, 0, 20), time
559
+
560
+ time = Chronic.parse("2 months ago", :now => Time.parse("2007-03-07 23:30"))
561
+ assert_equal Time.local(2007, 1, 7, 23, 30), time
562
+ end
563
+
564
+ def test_parse_guess_p_s_r
565
+ time = parse_now("in 3 hours")
566
+ assert_equal Time.local(2006, 8, 16, 17), time
567
+ end
568
+
569
+ def test_parse_guess_s_r_p_a
570
+ # past
571
+
572
+ time = parse_now("3 years ago tomorrow")
573
+ assert_equal Time.local(2003, 8, 17, 12), time
574
+
575
+ time = parse_now("3 years ago this friday")
576
+ assert_equal Time.local(2003, 8, 18, 12), time
577
+
578
+ time = parse_now("3 months ago saturday at 5:00 pm")
579
+ assert_equal Time.local(2006, 5, 19, 17), time
580
+
581
+ time = parse_now("2 days from this second")
582
+ assert_equal Time.local(2006, 8, 18, 14), time
583
+
584
+ time = parse_now("7 hours before tomorrow at midnight")
585
+ assert_equal Time.local(2006, 8, 17, 17), time
586
+
587
+ # future
588
+ end
589
+
590
+ def test_parse_guess_o_r_s_r
591
+ time = parse_now("3rd wednesday in november")
592
+ assert_equal Time.local(2006, 11, 15, 12), time
593
+
594
+ time = parse_now("10th wednesday in november")
595
+ assert_equal nil, time
596
+
597
+ # time = parse_now("3rd wednesday in 2007")
598
+ # assert_equal Time.local(2007, 1, 20, 12), time
599
+ end
600
+
601
+ def test_parse_guess_o_r_g_r
602
+ time = parse_now("3rd month next year")
603
+ assert_equal Time.local(2007, 3, 16, 12, 30), time
604
+
605
+ time = parse_now("3rd thursday this september")
606
+ assert_equal Time.local(2006, 9, 21, 12), time
607
+
608
+ time = parse_now("4th day last week")
609
+ assert_equal Time.local(2006, 8, 9, 12), time
610
+ end
611
+
612
+ def test_parse_guess_nonsense
613
+ time = parse_now("some stupid nonsense")
614
+ assert_equal nil, time
615
+
616
+ time = parse_now("Ham Sandwich")
617
+ assert_equal nil, time
618
+ end
619
+
620
+ def test_parse_span
621
+ span = parse_now("friday", :guess => false)
622
+ assert_equal Time.local(2006, 8, 18), span.begin
623
+ assert_equal Time.local(2006, 8, 19), span.end
624
+
625
+ span = parse_now("november", :guess => false)
626
+ assert_equal Time.local(2006, 11), span.begin
627
+ assert_equal Time.local(2006, 12), span.end
628
+
629
+ span = Chronic.parse("weekend" , :now => @time_2006_08_16_14_00_00, :guess => false)
630
+ assert_equal Time.local(2006, 8, 19), span.begin
631
+ assert_equal Time.local(2006, 8, 21), span.end
632
+ end
633
+
634
+ def test_parse_with_endian_precedence
635
+ date = '11/02/2007'
636
+
637
+ expect_for_middle_endian = Time.local(2007, 11, 2, 12)
638
+ expect_for_little_endian = Time.local(2007, 2, 11, 12)
639
+
640
+ # default precedence should be toward middle endianness
641
+ assert_equal expect_for_middle_endian, Chronic.parse(date)
642
+
643
+ assert_equal expect_for_middle_endian, Chronic.parse(date, :endian_precedence => [:middle, :little])
644
+
645
+ assert_equal expect_for_little_endian, Chronic.parse(date, :endian_precedence => [:little, :middle])
646
+ end
647
+
648
+ def test_parse_words
649
+ assert_equal parse_now("33 days from now"), parse_now("thirty-three days from now")
650
+ 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")
651
+ assert_equal parse_now("may 10th"), parse_now("may tenth")
652
+ end
653
+
654
+ def test_parse_only_complete_pointers
655
+ assert_equal parse_now("eat pasty buns today at 2pm"), @time_2006_08_16_14_00_00
656
+ assert_equal parse_now("futuristically speaking today at 2pm"), @time_2006_08_16_14_00_00
657
+ assert_equal parse_now("meeting today at 2pm"), @time_2006_08_16_14_00_00
658
+ end
659
+
660
+ def test_am_pm
661
+ assert_equal Time.local(2006, 8, 16), parse_now("8/16/2006 at 12am")
662
+ assert_equal Time.local(2006, 8, 16, 12), parse_now("8/16/2006 at 12pm")
663
+ end
664
+
665
+ def test_a_p
666
+ assert_equal Time.local(2006, 8, 16, 0, 15), parse_now("8/16/2006 at 12:15a")
667
+ assert_equal Time.local(2006, 8, 16, 18, 30), parse_now("8/16/2006 at 6:30p")
668
+ end
669
+
670
+ def test_argument_validation
671
+ assert_raise(Chronic::InvalidArgumentException) do
672
+ time = Chronic.parse("may 27", :foo => :bar)
673
+ end
674
+
675
+ assert_raise(Chronic::InvalidArgumentException) do
676
+ time = Chronic.parse("may 27", :context => :bar)
677
+ end
678
+ end
679
+
680
+ def test_seasons
681
+ t = parse_now("this spring", :guess => false)
682
+ assert_equal Time.local(2007, 3, 20), t.begin
683
+ assert_equal Time.local(2007, 6, 20), t.end
684
+
685
+ t = parse_now("this winter", :guess => false)
686
+ assert_equal Time.local(2006, 12, 22, 23), t.begin
687
+ assert_equal Time.local(2007, 3, 19), t.end
688
+
689
+ t = parse_now("last spring", :guess => false)
690
+ assert_equal Time.local(2006, 3, 20, 23), t.begin
691
+ assert_equal Time.local(2006, 6, 20), t.end
692
+
693
+ t = parse_now("last winter", :guess => false)
694
+ assert_equal Time.local(2005, 12, 22, 23), t.begin
695
+ assert_equal Time.local(2006, 3, 19, 23), t.end
696
+
697
+ t = parse_now("next spring", :guess => false)
698
+ assert_equal Time.local(2007, 3, 20), t.begin
699
+ assert_equal Time.local(2007, 6, 20), t.end
700
+ end
701
+
702
+ # regression
703
+
704
+ # def test_partial
705
+ # assert_equal '', parse_now("2 hours")
706
+ # end
707
+
708
+ def test_days_in_november
709
+ t1 = Chronic.parse('1st thursday in november', :now => Time.local(2007))
710
+ assert_equal Time.local(2007, 11, 1, 12), t1
711
+
712
+ t1 = Chronic.parse('1st friday in november', :now => Time.local(2007))
713
+ assert_equal Time.local(2007, 11, 2, 12), t1
714
+
715
+ t1 = Chronic.parse('1st saturday in november', :now => Time.local(2007))
716
+ assert_equal Time.local(2007, 11, 3, 12), t1
717
+
718
+ t1 = Chronic.parse('1st sunday in november', :now => Time.local(2007))
719
+ assert_equal Time.local(2007, 11, 4, 11), t1
720
+
721
+ # Chronic.debug = true
722
+ #
723
+ # t1 = Chronic.parse('1st monday in november', :now => Time.local(2007))
724
+ # assert_equal Time.local(2007, 11, 5, 11), t1
725
+ end
726
+
727
+ private
728
+ def parse_now(string, options={})
729
+ Chronic.parse(string, {:now => TIME_2006_08_16_14_00_00 }.merge(options))
730
+ end
731
+ end