pangel-chronic 0.3.0.3 → 0.3.10

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.rdoc → README.txt} +7 -22
  2. data/lib/chronic.rb +89 -12
  3. data/lib/chronic/chronic.rb +260 -301
  4. data/lib/chronic/grabber.rb +23 -23
  5. data/lib/chronic/handlers.rb +538 -557
  6. data/lib/chronic/ordinal.rb +36 -35
  7. data/lib/chronic/pointer.rb +24 -26
  8. data/lib/chronic/repeater.rb +128 -138
  9. data/lib/chronic/repeaters/repeater_day.rb +51 -51
  10. data/lib/chronic/repeaters/repeater_day_name.rb +50 -52
  11. data/lib/chronic/repeaters/repeater_day_portion.rb +93 -93
  12. data/lib/chronic/repeaters/repeater_fortnight.rb +66 -66
  13. data/lib/chronic/repeaters/repeater_hour.rb +56 -57
  14. data/lib/chronic/repeaters/repeater_minute.rb +56 -56
  15. data/lib/chronic/repeaters/repeater_month.rb +71 -62
  16. data/lib/chronic/repeaters/repeater_month_name.rb +95 -95
  17. data/lib/chronic/repeaters/repeater_season.rb +142 -142
  18. data/lib/chronic/repeaters/repeater_season_name.rb +42 -42
  19. data/lib/chronic/repeaters/repeater_second.rb +40 -40
  20. data/lib/chronic/repeaters/repeater_time.rb +124 -123
  21. data/lib/chronic/repeaters/repeater_week.rb +70 -70
  22. data/lib/chronic/repeaters/repeater_weekday.rb +76 -76
  23. data/lib/chronic/repeaters/repeater_weekend.rb +63 -63
  24. data/lib/chronic/repeaters/repeater_year.rb +63 -63
  25. data/lib/chronic/scalar.rb +89 -70
  26. data/lib/chronic/separator.rb +88 -88
  27. data/lib/chronic/time_zone.rb +23 -20
  28. data/lib/numerizer/numerizer.rb +93 -94
  29. data/test/suite.rb +2 -2
  30. data/test/test_Chronic.rb +47 -47
  31. data/test/test_Handler.rb +106 -106
  32. data/test/test_Numerizer.rb +47 -49
  33. data/test/test_RepeaterDayName.rb +48 -48
  34. data/test/test_RepeaterFortnight.rb +59 -59
  35. data/test/test_RepeaterHour.rb +61 -64
  36. data/test/test_RepeaterMonth.rb +43 -43
  37. data/test/test_RepeaterMonthName.rb +53 -53
  38. data/test/test_RepeaterTime.rb +68 -68
  39. data/test/test_RepeaterWeek.rb +59 -59
  40. data/test/test_RepeaterWeekday.rb +53 -53
  41. data/test/test_RepeaterWeekend.rb +71 -71
  42. data/test/test_RepeaterYear.rb +59 -59
  43. data/test/test_Span.rb +19 -28
  44. data/test/test_Time.rb +46 -46
  45. data/test/test_Token.rb +22 -22
  46. data/test/test_parsing.rb +726 -792
  47. metadata +6 -10
@@ -2,32 +2,23 @@ require 'chronic'
2
2
  require 'test/unit'
3
3
 
4
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
5
10
 
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
- def test_span_exclusive
25
- s = Chronic::Span.new(1, 4)
26
- assert s.include?(3)
27
- assert !s.include?(4)
28
- t = Chronic::Span.new(Time.local(1980, 03, 01, 0), Time.local(1980, 04, 01, 0))
29
- assert t.include?(Time.local(1980, 03, 31, 0))
30
- assert !t.include?(Time.local(1980, 04, 01, 0))
31
- end
32
-
33
- end
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
@@ -2,49 +2,49 @@ require 'chronic'
2
2
  require 'test/unit'
3
3
 
4
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
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
@@ -2,25 +2,25 @@ require 'chronic'
2
2
  require 'test/unit'
3
3
 
4
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
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
@@ -2,796 +2,730 @@ require 'chronic'
2
2
  require 'test/unit'
3
3
 
4
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), time
284
-
285
- time = parse_now("this year", :context => :past)
286
- assert_equal Time.local(2006, 4, 24, 13), 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_rrgr
472
- time = parse_now("5pm tomorrow")
473
- assert_equal Time.local(2006, 8, 17, 17), time
474
- end
475
-
476
- def test_parse_guess_s_r_p
477
- # past
478
-
479
- time = parse_now("3 years ago")
480
- assert_equal Time.local(2003, 8, 16, 14), time
481
-
482
- time = parse_now("1 month ago")
483
- assert_equal Time.local(2006, 7, 16, 14), time
484
-
485
- time = parse_now("1 fortnight ago")
486
- assert_equal Time.local(2006, 8, 2, 14), time
487
-
488
- time = parse_now("2 fortnights ago")
489
- assert_equal Time.local(2006, 7, 19, 14), time
490
-
491
- time = parse_now("3 weeks ago")
492
- assert_equal Time.local(2006, 7, 26, 14), time
493
-
494
- time = parse_now("2 weekends ago")
495
- assert_equal Time.local(2006, 8, 5), time
496
-
497
- time = parse_now("3 days ago")
498
- assert_equal Time.local(2006, 8, 13, 14), time
499
-
500
- #time = parse_now("1 monday ago")
501
- #assert_equal Time.local(2006, 8, 14, 12), time
502
-
503
- time = parse_now("5 mornings ago")
504
- assert_equal Time.local(2006, 8, 12, 9), time
505
-
506
- time = parse_now("7 hours ago")
507
- assert_equal Time.local(2006, 8, 16, 7), time
508
-
509
- time = parse_now("3 minutes ago")
510
- assert_equal Time.local(2006, 8, 16, 13, 57), time
511
-
512
- time = parse_now("20 seconds before now")
513
- assert_equal Time.local(2006, 8, 16, 13, 59, 40), time
514
-
515
- # future
516
-
517
- time = parse_now("3 years from now")
518
- assert_equal Time.local(2009, 8, 16, 14, 0, 0), time
519
-
520
- time = parse_now("6 months hence")
521
- assert_equal Time.local(2007, 2, 16, 14), time
522
-
523
- time = parse_now("3 fortnights hence")
524
- assert_equal Time.local(2006, 9, 27, 14), time
525
-
526
- time = parse_now("1 week from now")
527
- assert_equal Time.local(2006, 8, 23, 14, 0, 0), time
528
-
529
- time = parse_now("1 weekend from now")
530
- assert_equal Time.local(2006, 8, 19), time
531
-
532
- time = parse_now("2 weekends from now")
533
- assert_equal Time.local(2006, 8, 26), time
534
-
535
- time = parse_now("1 day hence")
536
- assert_equal Time.local(2006, 8, 17, 14), time
537
-
538
- time = parse_now("5 mornings hence")
539
- assert_equal Time.local(2006, 8, 21, 9), time
540
-
541
- time = parse_now("1 hour from now")
542
- assert_equal Time.local(2006, 8, 16, 15), time
543
-
544
- time = parse_now("20 minutes hence")
545
- assert_equal Time.local(2006, 8, 16, 14, 20), time
546
-
547
- time = parse_now("20 seconds from now")
548
- assert_equal Time.local(2006, 8, 16, 14, 0, 20), time
549
-
550
- time = Chronic.parse("2 months ago", :now => Time.parse("2007-03-07 23:30"))
551
- assert_equal Time.local(2007, 1, 7, 23, 30), time
552
- end
553
-
554
- def test_parse_guess_p_s_r
555
- time = parse_now("in 3 hours")
556
- assert_equal Time.local(2006, 8, 16, 17), time
557
- end
558
-
559
- def test_parse_guess_s_r_p_a
560
- # past
561
-
562
- time = parse_now("3 years ago tomorrow")
563
- assert_equal Time.local(2003, 8, 17, 12), time
564
-
565
- time = parse_now("3 years ago this friday")
566
- assert_equal Time.local(2003, 8, 18, 12), time
567
-
568
- time = parse_now("3 months ago saturday at 5:00 pm")
569
- assert_equal Time.local(2006, 5, 19, 17), time
570
-
571
- time = parse_now("2 days from this second")
572
- assert_equal Time.local(2006, 8, 18, 14), time
573
-
574
- #time = parse_now("7 hours before tomorrow at midnight")
575
- #assert_equal Time.local(2006, 8, 17, 17), time
576
-
577
- # future
578
- end
579
-
580
- def test_parse_guess_o_r_s_r
581
- time = parse_now("3rd wednesday in november")
582
- assert_equal Time.local(2006, 11, 15, 12), time
583
-
584
- time = parse_now("10th wednesday in november")
585
- assert_equal nil, time
586
-
587
- # time = parse_now("3rd wednesday in 2007")
588
- # assert_equal Time.local(2007, 1, 20, 12), time
589
- end
590
-
591
- def test_parse_guess_o_r_g_r
592
- time = parse_now("3rd month next year")
593
- assert_equal Time.local(2007, 3, 16, 13), time
594
-
595
- time = parse_now("3rd thursday this september")
596
- assert_equal Time.local(2006, 9, 21, 12), time
597
-
598
- time = parse_now("4th day last week")
599
- assert_equal Time.local(2006, 8, 9, 12), time
600
- end
601
-
602
- def test_parse_guess_nonsense
603
- time = parse_now("some stupid nonsense")
604
- assert_equal nil, time
605
-
606
- time = parse_now("Ham Sandwich")
607
- assert_equal nil, time
608
- end
609
-
610
- def test_parse_span
611
- span = parse_now("friday", :guess => false)
612
- assert_equal Time.local(2006, 8, 18), span.begin
613
- assert_equal Time.local(2006, 8, 19), span.end
614
-
615
- span = parse_now("november", :guess => false)
616
- assert_equal Time.local(2006, 11), span.begin
617
- assert_equal Time.local(2006, 12), span.end
618
-
619
- span = Chronic.parse("weekend" , :now => @time_2006_08_16_14_00_00, :guess => false)
620
- assert_equal Time.local(2006, 8, 19), span.begin
621
- assert_equal Time.local(2006, 8, 21), span.end
622
- end
623
-
624
- def test_parse_with_endian_precedence
625
- date = '11/02/2007'
626
-
627
- expect_for_middle_endian = Time.local(2007, 11, 2, 12)
628
- expect_for_little_endian = Time.local(2007, 2, 11, 12)
629
-
630
- # default precedence should be toward middle endianness
631
- assert_equal expect_for_middle_endian, Chronic.parse(date)
632
-
633
- assert_equal expect_for_middle_endian, Chronic.parse(date, :endian_precedence => [:middle, :little])
634
-
635
- assert_equal expect_for_little_endian, Chronic.parse(date, :endian_precedence => [:little, :middle])
636
- end
637
-
638
- def test_parse_words
639
- assert_equal parse_now("33 days from now"), parse_now("thirty-three days from now")
640
- 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")
641
- assert_equal parse_now("may 10th"), parse_now("may tenth")
642
- end
643
-
644
- def test_parse_only_complete_pointers
645
- assert_equal parse_now("eat pasty buns today at 2pm"), @time_2006_08_16_14_00_00
646
- assert_equal parse_now("futuristically speaking today at 2pm"), @time_2006_08_16_14_00_00
647
- assert_equal parse_now("meeting today at 2pm"), @time_2006_08_16_14_00_00
648
- end
649
-
650
- def test_parse_strip_tokens
651
- assert_equal Chronic.strip_tokens("eat pasty buns today at 2pm"), "eat pasty buns"
652
- assert_equal Chronic.strip_tokens("futuristically speaking today at 2pm"), "futuristically speaking"
653
- assert_equal Chronic.strip_tokens("meeting today at 2pm"), "meeting"
654
- end
655
-
656
- def test_am_pm
657
- assert_equal Time.local(2006, 8, 16), parse_now("8/16/2006 at 12am")
658
- assert_equal Time.local(2006, 8, 16, 12), parse_now("8/16/2006 at 12pm")
659
- end
660
-
661
- def test_a_p
662
- assert_equal Time.local(2006, 8, 16, 0, 15), parse_now("8/16/2006 at 12:15a")
663
- assert_equal Time.local(2006, 8, 16, 18, 30), parse_now("8/16/2006 at 6:30p")
664
- end
665
-
666
- def test_argument_validation
667
- assert_raise(Chronic::InvalidArgumentException) do
668
- time = Chronic.parse("may 27", :foo => :bar)
669
- end
670
-
671
- assert_raise(Chronic::InvalidArgumentException) do
672
- time = Chronic.parse("may 27", :context => :bar)
673
- end
674
- end
675
-
676
- def test_seasons
677
- t = parse_now("this spring", :guess => false)
678
- assert_equal Time.local(2007, 3, 20), t.begin
679
- assert_equal Time.local(2007, 6, 20), t.end
680
-
681
- t = parse_now("this winter", :guess => false)
682
- assert_equal Time.local(2006, 12, 22, 23), t.begin
683
- assert_equal Time.local(2007, 3, 19), t.end
684
-
685
- t = parse_now("last spring", :guess => false)
686
- assert_equal Time.local(2006, 3, 20, 23), t.begin
687
- assert_equal Time.local(2006, 6, 20), t.end
688
-
689
- t = parse_now("last winter", :guess => false)
690
- assert_equal Time.local(2005, 12, 22, 23), t.begin
691
- assert_equal Time.local(2006, 3, 19, 23), t.end
692
-
693
- t = parse_now("next spring", :guess => false)
694
- assert_equal Time.local(2007, 3, 20), t.begin
695
- assert_equal Time.local(2007, 6, 20), t.end
696
- end
697
-
698
- # regression
699
-
700
- # def test_partial
701
- # assert_equal '', parse_now("2 hours")
702
- # end
703
-
704
- def test_days_in_november
705
- t1 = Chronic.parse('1st thursday in november', :now => Time.local(2007))
706
- assert_equal Time.local(2007, 11, 1, 12), t1
707
-
708
- t1 = Chronic.parse('1st friday in november', :now => Time.local(2007))
709
- assert_equal Time.local(2007, 11, 2, 12), t1
710
-
711
- t1 = Chronic.parse('1st saturday in november', :now => Time.local(2007))
712
- assert_equal Time.local(2007, 11, 3, 12), t1
713
-
714
- t1 = Chronic.parse('1st sunday in november', :now => Time.local(2007))
715
- assert_equal Time.local(2007, 11, 4, 11), t1
716
-
717
- # Chronic.debug = true
718
- #
719
- # t1 = Chronic.parse('1st monday in november', :now => Time.local(2007))
720
- # assert_equal Time.local(2007, 11, 5, 11), t1
721
- end
722
-
723
- def test_parse_strip_tokens_preserved_case
724
- assert_equal Chronic.strip_tokens("Ham Sandwich"), "Ham Sandwich"
725
- assert_equal Chronic.strip_tokens("Eat a Ham Sandwich tomorrow"), "Eat a Ham Sandwich"
726
- end
727
-
728
- def test_parse_this_past
729
- t = parse_now("this past tuesday")
730
- assert_equal Time.local(2006,8,15, 12), t
731
-
732
- t = parse_now("this past day")
733
- assert_equal Time.local(2006,8,15, 12), t
734
-
735
- t = parse_now("this past hour")
736
- assert_equal Time.local(2006,8,16, 13, 30), t
737
- end
738
-
739
- def test_parse_noon
740
- t = parse_now("noon")
741
- assert_equal Time.local(2006,8,16, 12), t
742
-
743
- t = parse_now("tomorrow at noon")
744
- assert_equal Time.local(2006,8,17, 12), t
745
- end
746
-
747
- def test_parse_before_now
748
- t = parse_now("3 hours before now")
749
- assert_equal Time.local(2006,8,16, 11), t
750
-
751
- t = parse_now("3 days before now")
752
- assert_equal Time.local(2006,8,13, 14), t
753
-
754
- t = parse_now("30 minutes before now")
755
- assert_equal Time.local(2006,8,16, 13,30), t
756
- end
757
-
758
- def test_now
759
- t = parse_now("now")
760
- assert_equal Time.local(2006,8,16,14), t
761
-
762
- t = parse_now("1 hour from now")
763
- assert_equal Time.local(2006,8,16,15), t
764
-
765
- t = parse_now("1 hour before now")
766
- assert_equal Time.local(2006,8,16,13), t
767
- end
768
-
769
- def test_this_last
770
- t = parse_now("this last day")
771
- assert_equal Time.local(2006, 8, 15, 12), t
772
-
773
- t = parse_now("this last hour")
774
- assert_equal Time.local(2006, 8, 16, 13, 30), t
775
- end
776
-
777
- def test_hr_and_hrs
778
- t = parse_now("in 3 hr")
779
- assert_equal Time.local(2006, 8,16,17), t
780
-
781
- t = parse_now("in 3 hrs")
782
- assert_equal Time.local(2006, 8,16,17), t
783
- end
784
-
785
- def test_fractional_times
786
- t = parse_now("in three and a half hours")
787
- assert_equal Time.local(2006, 8,16,17, 30), t
788
-
789
- t = parse_now("in 3.5 hours")
790
- assert_equal Time.local(2006, 8,16,17, 30), t
791
- end
792
-
793
- private
794
- def parse_now(string, options={})
795
- Chronic.parse(string, {:now => TIME_2006_08_16_14_00_00 }.merge(options))
796
- end
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
797
731
  end