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