chronic 0.6.3 → 0.10.2

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 (67) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -3
  3. data/.travis.yml +8 -0
  4. data/HISTORY.md +76 -0
  5. data/README.md +47 -42
  6. data/Rakefile +30 -12
  7. data/chronic.gemspec +10 -4
  8. data/lib/chronic/date.rb +82 -0
  9. data/lib/chronic/grabber.rb +9 -7
  10. data/lib/chronic/handler.rb +47 -40
  11. data/lib/chronic/handlers.rb +240 -28
  12. data/lib/chronic/numerizer.rb +11 -2
  13. data/lib/chronic/ordinal.rb +28 -23
  14. data/lib/chronic/parser.rb +268 -0
  15. data/lib/chronic/pointer.rb +9 -7
  16. data/lib/chronic/repeater.rb +58 -48
  17. data/lib/chronic/repeaters/repeater_day.rb +4 -3
  18. data/lib/chronic/repeaters/repeater_day_name.rb +5 -4
  19. data/lib/chronic/repeaters/repeater_day_portion.rb +33 -18
  20. data/lib/chronic/repeaters/repeater_fortnight.rb +4 -3
  21. data/lib/chronic/repeaters/repeater_hour.rb +4 -3
  22. data/lib/chronic/repeaters/repeater_minute.rb +4 -3
  23. data/lib/chronic/repeaters/repeater_month.rb +5 -4
  24. data/lib/chronic/repeaters/repeater_month_name.rb +4 -3
  25. data/lib/chronic/repeaters/repeater_season.rb +5 -3
  26. data/lib/chronic/repeaters/repeater_second.rb +4 -3
  27. data/lib/chronic/repeaters/repeater_time.rb +35 -25
  28. data/lib/chronic/repeaters/repeater_week.rb +4 -3
  29. data/lib/chronic/repeaters/repeater_weekday.rb +4 -3
  30. data/lib/chronic/repeaters/repeater_weekend.rb +4 -3
  31. data/lib/chronic/repeaters/repeater_year.rb +6 -5
  32. data/lib/chronic/scalar.rb +40 -68
  33. data/lib/chronic/season.rb +1 -12
  34. data/lib/chronic/separator.rb +142 -23
  35. data/lib/chronic/sign.rb +49 -0
  36. data/lib/chronic/span.rb +2 -2
  37. data/lib/chronic/tag.rb +10 -15
  38. data/lib/chronic/time.rb +40 -0
  39. data/lib/chronic/time_zone.rb +9 -7
  40. data/lib/chronic/token.rb +16 -10
  41. data/lib/chronic.rb +109 -70
  42. data/test/helper.rb +8 -2
  43. data/test/{test_Chronic.rb → test_chronic.rb} +69 -34
  44. data/test/{test_DaylightSavings.rb → test_daylight_savings.rb} +1 -1
  45. data/test/{test_Handler.rb → test_handler.rb} +38 -14
  46. data/test/{test_MiniDate.rb → test_mini_date.rb} +9 -9
  47. data/test/{test_Numerizer.rb → test_numerizer.rb} +16 -2
  48. data/test/test_parsing.rb +392 -22
  49. data/test/{test_RepeaterDayName.rb → test_repeater_day_name.rb} +1 -1
  50. data/test/test_repeater_day_portion.rb +254 -0
  51. data/test/{test_RepeaterFortnight.rb → test_repeater_fortnight.rb} +1 -1
  52. data/test/{test_RepeaterHour.rb → test_repeater_hour.rb} +1 -1
  53. data/test/{test_RepeaterMinute.rb → test_repeater_minute.rb} +1 -1
  54. data/test/{test_RepeaterMonth.rb → test_repeater_month.rb} +1 -1
  55. data/test/{test_RepeaterMonthName.rb → test_repeater_month_name.rb} +1 -1
  56. data/test/{test_RepeaterSeason.rb → test_repeater_season.rb} +1 -1
  57. data/test/{test_RepeaterTime.rb → test_repeater_time.rb} +19 -1
  58. data/test/{test_RepeaterWeek.rb → test_repeater_week.rb} +1 -1
  59. data/test/{test_RepeaterWeekday.rb → test_repeater_weekday.rb} +1 -1
  60. data/test/{test_RepeaterWeekend.rb → test_repeater_weekend.rb} +1 -1
  61. data/test/{test_RepeaterYear.rb → test_repeater_year.rb} +1 -1
  62. data/test/{test_Span.rb → test_span.rb} +2 -2
  63. data/test/{test_Token.rb → test_token.rb} +1 -1
  64. metadata +129 -87
  65. data/.gemtest +0 -0
  66. data/.yardopts +0 -3
  67. data/lib/chronic/chronic.rb +0 -323
data/test/test_parsing.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class TestParsing < Test::Unit::TestCase
3
+ class TestParsing < TestCase
4
4
  # Wed Aug 16 14:00:00 UTC 2006
5
5
  TIME_2006_08_16_14_00_00 = Time.local(2006, 8, 16, 14, 0, 0, 0)
6
6
 
@@ -8,6 +8,37 @@ class TestParsing < Test::Unit::TestCase
8
8
  @time_2006_08_16_14_00_00 = TIME_2006_08_16_14_00_00
9
9
  end
10
10
 
11
+ def test_handle_generic
12
+ time = Chronic.parse("2012-08-02T13:00:00")
13
+ assert_equal Time.local(2012, 8, 2, 13), time
14
+
15
+ time = Chronic.parse("2012-08-02T13:00:00+01:00")
16
+ assert_equal Time.utc(2012, 8, 2, 12), time
17
+
18
+ time = Chronic.parse("2012-08-02T08:00:00-04:00")
19
+ assert_equal Time.utc(2012, 8, 2, 12), time
20
+
21
+ time = Chronic.parse("2013-08-01T19:30:00.345-07:00")
22
+ time2 = Time.parse("2013-08-01 019:30:00.345-07:00")
23
+ assert_in_delta time, time2, 0.001
24
+
25
+ time = Chronic.parse("2012-08-02T12:00:00Z")
26
+ assert_equal Time.utc(2012, 8, 2, 12), time
27
+
28
+ time = Chronic.parse("2012-01-03 01:00:00.100")
29
+ time2 = Time.parse("2012-01-03 01:00:00.100")
30
+ assert_in_delta time, time2, 0.001
31
+
32
+ time = Chronic.parse("2012-01-03 01:00:00.234567")
33
+ time2 = Time.parse("2012-01-03 01:00:00.234567")
34
+ assert_in_delta time, time2, 0.000001
35
+
36
+ assert_nil Chronic.parse("1/1/32.1")
37
+
38
+ time = Chronic.parse("28th", {:guess => :begin})
39
+ assert_equal Time.new(Time.now.year, Time.now.month, 28), time
40
+ end
41
+
11
42
  def test_handle_rmn_sd
12
43
  time = parse_now("aug 3")
13
44
  assert_equal Time.local(2006, 8, 3, 12), time
@@ -15,9 +46,15 @@ class TestParsing < Test::Unit::TestCase
15
46
  time = parse_now("aug 3", :context => :past)
16
47
  assert_equal Time.local(2006, 8, 3, 12), time
17
48
 
49
+ time = parse_now("aug. 3")
50
+ assert_equal Time.local(2006, 8, 3, 12), time
51
+
18
52
  time = parse_now("aug 20")
19
53
  assert_equal Time.local(2006, 8, 20, 12), time
20
54
 
55
+ time = parse_now("aug-20")
56
+ assert_equal Time.local(2006, 8, 20, 12), time
57
+
21
58
  time = parse_now("aug 20", :context => :future)
22
59
  assert_equal Time.local(2006, 8, 20, 12), time
23
60
 
@@ -35,6 +72,9 @@ class TestParsing < Test::Unit::TestCase
35
72
 
36
73
  time = parse_now("may 28 at 5:32.19pm", :context => :past)
37
74
  assert_equal Time.local(2006, 5, 28, 17, 32, 19), time
75
+
76
+ time = parse_now("may 28 at 5:32:19.764")
77
+ assert_in_delta Time.local(2007, 5, 28, 17, 32, 19, 764000), time, 0.001
38
78
  end
39
79
 
40
80
  def test_handle_rmn_sd_on
@@ -65,6 +105,11 @@ class TestParsing < Test::Unit::TestCase
65
105
  assert_equal Time.local(2007, 5, 27, 5), time
66
106
  end
67
107
 
108
+ def test_handle_od_rm
109
+ time = parse_now("fifteenth of this month")
110
+ assert_equal Time.local(2006, 8, 15, 12), time
111
+ end
112
+
68
113
  def test_handle_od_rmn
69
114
  time = parse_now("22nd February")
70
115
  assert_equal Time.local(2007, 2, 22, 12), time
@@ -76,6 +121,28 @@ class TestParsing < Test::Unit::TestCase
76
121
  assert_equal Time.local(2006, 12, 11, 8), time
77
122
  end
78
123
 
124
+ def test_handle_sy_rmn_od
125
+ time = parse_now("2009 May 22nd")
126
+ assert_equal Time.local(2009, 05, 22, 12), time
127
+ end
128
+
129
+ def test_handle_sd_rmn
130
+ time = parse_now("22 February")
131
+ assert_equal Time.local(2007, 2, 22, 12), time
132
+
133
+ time = parse_now("22 feb")
134
+ assert_equal Time.local(2007, 2, 22, 12), time
135
+
136
+ time = parse_now("22-feb")
137
+ assert_equal Time.local(2007, 2, 22, 12), time
138
+
139
+ time = parse_now("31 of may at 6:30pm")
140
+ assert_equal Time.local(2007, 5, 31, 18, 30), time
141
+
142
+ time = parse_now("11 december 8am")
143
+ assert_equal Time.local(2006, 12, 11, 8), time
144
+ end
145
+
79
146
  def test_handle_rmn_od_on
80
147
  time = parse_now("5:00 pm may 27th", :context => :past)
81
148
  assert_equal Time.local(2006, 5, 27, 17), time
@@ -113,8 +180,14 @@ class TestParsing < Test::Unit::TestCase
113
180
  time = parse_now("2011-07-03 22:11:35 +01:00")
114
181
  assert_equal 1309727495, time.to_i
115
182
 
183
+ time = parse_now("2011-07-03 16:11:35 -05:00")
184
+ assert_equal 1309727495, time.to_i
185
+
116
186
  time = parse_now("2011-07-03 21:11:35 UTC")
117
187
  assert_equal 1309727495, time.to_i
188
+
189
+ time = parse_now("2011-07-03 21:11:35.362 UTC")
190
+ assert_in_delta 1309727495.362, time.to_f, 0.001
118
191
  end
119
192
 
120
193
  def test_handle_rmn_sd_sy
@@ -153,6 +226,9 @@ class TestParsing < Test::Unit::TestCase
153
226
 
154
227
  time = parse_now("may 27 32")
155
228
  assert_equal Time.local(2032, 5, 27, 12, 0, 0), time
229
+
230
+ time = parse_now("oct 5 2012 1045pm")
231
+ assert_equal Time.local(2012, 10, 5, 22, 45), time
156
232
  end
157
233
 
158
234
  def test_handle_rmn_od_sy
@@ -222,9 +298,15 @@ class TestParsing < Test::Unit::TestCase
222
298
  time = parse_now("7/12/11", :endian_precedence => :little)
223
299
  assert_equal Time.local(2011, 12, 7, 12), time
224
300
 
301
+ time = parse_now("9/19/2011 6:05:57 PM")
302
+ assert_equal Time.local(2011, 9, 19, 18, 05, 57), time
303
+
225
304
  # month day overflows
226
305
  time = parse_now("30/2/2000")
227
306
  assert_nil time
307
+
308
+ time = parse_now("2013-03-12 17:00", :context => :past)
309
+ assert_equal Time.local(2013, 3, 12, 17, 0, 0), time
228
310
  end
229
311
 
230
312
  def test_handle_sd_sm_sy
@@ -233,6 +315,18 @@ class TestParsing < Test::Unit::TestCase
233
315
 
234
316
  time = parse_now("27/5/1979 @ 0700")
235
317
  assert_equal Time.local(1979, 5, 27, 7), time
318
+
319
+ time = parse_now("03/18/2012 09:26 pm")
320
+ assert_equal Time.local(2012, 3, 18, 21, 26), time
321
+
322
+ time = parse_now("30.07.2013 16:34:22")
323
+ assert_equal Time.local(2013, 7, 30, 16, 34, 22), time
324
+
325
+ time = parse_now("09.08.2013")
326
+ assert_equal Time.local(2013, 8, 9, 12), time
327
+
328
+ time = parse_now("30-07-2013 21:53:49")
329
+ assert_equal Time.local(2013, 7, 30, 21, 53, 49), time
236
330
  end
237
331
 
238
332
  def test_handle_sy_sm_sd
@@ -257,22 +351,96 @@ class TestParsing < Test::Unit::TestCase
257
351
  time = parse_now("2006-08-20 15:30.30")
258
352
  assert_equal Time.local(2006, 8, 20, 15, 30, 30), time
259
353
 
354
+ time = parse_now("2006-08-20 15:30:30:000536")
355
+ assert_in_delta Time.local(2006, 8, 20, 15, 30, 30, 536), time, 0.000001
356
+
260
357
  time = parse_now("1902-08-20")
261
358
  assert_equal Time.local(1902, 8, 20, 12, 0, 0), time
359
+
360
+ time = parse_now("2013.07.30 11:45:23")
361
+ assert_equal Time.local(2013, 7, 30, 11, 45, 23), time
362
+
363
+ time = parse_now("2013.08.09")
364
+ assert_equal Time.local(2013, 8, 9, 12, 0, 0), time
365
+
366
+ # exif date time original
367
+ time = parse_now("2012:05:25 22:06:50")
368
+ assert_equal Time.local(2012, 5, 25, 22, 6, 50), time
262
369
  end
263
370
 
264
- def test_handle_sm_sy
371
+ def test_handle_sm_sd
265
372
  time = parse_now("05/06")
266
- assert_equal Time.local(2006, 5, 16, 12), time
373
+ assert_equal Time.local(2007, 5, 6, 12), time
267
374
 
268
- time = parse_now("12/06")
269
- assert_equal Time.local(2006, 12, 16, 12), time
375
+ time = parse_now("05/06", :endian_precedence => [:little, :medium])
376
+ assert_equal Time.local(2007, 6, 5, 12), time
270
377
 
271
- time = parse_now("13/06")
272
- assert_equal nil, time
378
+ time = parse_now("05/06 6:05:57 PM")
379
+ assert_equal Time.local(2007, 5, 6, 18, 05, 57), time
380
+
381
+ time = parse_now("05/06 6:05:57 PM", :endian_precedence => [:little, :medium])
382
+ assert_equal Time.local(2007, 6, 5, 18, 05, 57), time
383
+
384
+ time = parse_now("13/09")
385
+ assert_equal Time.local(2006, 9, 13, 12), time
386
+
387
+ # future
388
+ time = parse_now("05/06") # future is default context
389
+ assert_equal Time.local(2007, 5, 6, 12), time
390
+
391
+ time = parse_now("1/13", :context => :future)
392
+ assert_equal Time.local(2007, 1, 13, 12), time
393
+
394
+ time = parse_now("3/13", :context => :none)
395
+ assert_equal Time.local(2006, 3, 13, 12), time
396
+ end
397
+
398
+ # def test_handle_sm_sy
399
+ # time = parse_now("05/06")
400
+ # assert_equal Time.local(2006, 5, 16, 12), time
401
+ #
402
+ # time = parse_now("12/06")
403
+ # assert_equal Time.local(2006, 12, 16, 12), time
404
+ #
405
+ # time = parse_now("13/06")
406
+ # assert_equal nil, time
407
+ # end
408
+
409
+ def test_handle_sy_sm
410
+ time = parse_now("2012-06")
411
+ assert_equal Time.local(2012, 06, 16), time
412
+
413
+ time = parse_now("2013/12")
414
+ assert_equal Time.local(2013, 12, 16, 12, 0), time
273
415
  end
274
416
 
275
417
  def test_handle_r
418
+ time = parse_now("9am on Saturday")
419
+ assert_equal Time.local(2006, 8, 19, 9), time
420
+
421
+ time = parse_now("on Tuesday")
422
+ assert_equal Time.local(2006, 8, 22, 12), time
423
+
424
+ time = parse_now("1:00:00 PM")
425
+ assert_equal Time.local(2006, 8, 16, 13), time
426
+
427
+ time = parse_now("01:00:00 PM")
428
+ assert_equal Time.local(2006, 8, 16, 13), time
429
+
430
+ time = parse_now("today at 02:00:00", :hours24 => false)
431
+ assert_equal Time.local(2006, 8, 16, 14), time
432
+
433
+ time = parse_now("today at 02:00:00 AM", :hours24 => false)
434
+ assert_equal Time.local(2006, 8, 16, 2), time
435
+
436
+ time = parse_now("today at 3:00:00", :hours24 => true)
437
+ assert_equal Time.local(2006, 8, 16, 3), time
438
+
439
+ time = parse_now("today at 03:00:00", :hours24 => true)
440
+ assert_equal Time.local(2006, 8, 16, 3), time
441
+
442
+ time = parse_now("tomorrow at 4a.m.")
443
+ assert_equal Time.local(2006, 8, 17, 4), time
276
444
  end
277
445
 
278
446
  def test_handle_r_g_r
@@ -288,9 +456,25 @@ class TestParsing < Test::Unit::TestCase
288
456
  end
289
457
 
290
458
  def test_handle_s_r_p_a
459
+ time1 = parse_now("two days ago 0:0:0am")
460
+ time2 = parse_now("two days ago 00:00:00am")
461
+ assert_equal time1, time2
291
462
  end
292
463
 
293
464
  def test_handle_orr
465
+ time = parse_now("5th tuesday in january")
466
+ assert_equal Time.local(2007, 01, 30, 12), time
467
+
468
+ time = parse_now("5th tuesday in february")
469
+ assert_equal nil, time
470
+
471
+ %W(jan feb march april may june july aug sep oct nov dec).each_with_index do |month, index|
472
+ time = parse_now("5th tuesday in #{month}")
473
+
474
+ if time then
475
+ assert_equal time.month, index+1
476
+ end
477
+ end
294
478
  end
295
479
 
296
480
  def test_handle_o_r_s_r
@@ -322,6 +506,14 @@ class TestParsing < Test::Unit::TestCase
322
506
  assert_equal Time.local(2006, 8, 9, 12), time
323
507
  end
324
508
 
509
+ def test_handle_sm_rmn_sy
510
+ time = parse_now('30-Mar-11')
511
+ assert_equal Time.local(2011, 3, 30, 12), time
512
+
513
+ time = parse_now('31-Aug-12')
514
+ assert_equal Time.local(2012, 8, 31), time
515
+ end
516
+
325
517
  # end of testing handlers
326
518
 
327
519
  def test_parse_guess_r
@@ -417,11 +609,11 @@ class TestParsing < Test::Unit::TestCase
417
609
  def test_parse_guess_gr
418
610
  # year
419
611
 
420
- time = parse_now("this year")
421
- assert_equal Time.local(2006, 10, 24, 12, 30), time
612
+ time = parse_now("this year", :guess => false)
613
+ assert_equal Time.local(2006, 8, 17), time.begin
422
614
 
423
- time = parse_now("this year", :context => :past)
424
- assert_equal Time.local(2006, 4, 24, 12, 30), time
615
+ time = parse_now("this year", :context => :past, :guess => false)
616
+ assert_equal Time.local(2006, 1, 1), time.begin
425
617
 
426
618
  # month
427
619
 
@@ -507,6 +699,34 @@ class TestParsing < Test::Unit::TestCase
507
699
  time = parse_now("last wed")
508
700
  assert_equal Time.local(2006, 8, 9, 12), time
509
701
 
702
+ monday = Time.local(2006, 8, 21, 12)
703
+ assert_equal monday, parse_now("mon")
704
+ assert_equal monday, parse_now("mun")
705
+
706
+ tuesday = Time.local(2006, 8, 22, 12)
707
+ assert_equal tuesday, parse_now("tue")
708
+ assert_equal tuesday, parse_now("tus")
709
+
710
+ wednesday = Time.local(2006, 8, 23, 12)
711
+ assert_equal wednesday, parse_now("wed")
712
+ assert_equal wednesday, parse_now("wenns")
713
+
714
+ thursday = Time.local(2006, 8, 17, 12)
715
+ assert_equal thursday, parse_now("thu")
716
+ assert_equal thursday, parse_now("thur")
717
+
718
+ friday = Time.local(2006, 8, 18, 12)
719
+ assert_equal friday, parse_now("fri")
720
+ assert_equal friday, parse_now("fry")
721
+
722
+ saturday = Time.local(2006, 8, 19, 12)
723
+ assert_equal saturday, parse_now("sat")
724
+ assert_equal saturday, parse_now("satterday")
725
+
726
+ sunday = Time.local(2006, 8, 20, 12)
727
+ assert_equal sunday, parse_now("sun")
728
+ assert_equal sunday, parse_now("sum")
729
+
510
730
  # day portion
511
731
 
512
732
  time = parse_now("this morning")
@@ -515,13 +735,33 @@ class TestParsing < Test::Unit::TestCase
515
735
  time = parse_now("tonight")
516
736
  assert_equal Time.local(2006, 8, 16, 22), time
517
737
 
738
+ # hour
739
+
740
+ time = parse_now("next hr")
741
+ assert_equal Time.local(2006, 8, 16, 15, 30, 0), time
742
+
743
+ time = parse_now("next hrs")
744
+ assert_equal Time.local(2006, 8, 16, 15, 30, 0), time
745
+
518
746
  # minute
519
747
 
748
+ time = parse_now("next min")
749
+ assert_equal Time.local(2006, 8, 16, 14, 1, 30), time
750
+
751
+ time = parse_now("next mins")
752
+ assert_equal Time.local(2006, 8, 16, 14, 1, 30), time
753
+
520
754
  time = parse_now("next minute")
521
755
  assert_equal Time.local(2006, 8, 16, 14, 1, 30), time
522
756
 
523
757
  # second
524
758
 
759
+ time = parse_now("next sec")
760
+ assert_equal Time.local(2006, 8, 16, 14, 0, 1), time
761
+
762
+ time = parse_now("next secs")
763
+ assert_equal Time.local(2006, 8, 16, 14, 0, 1), time
764
+
525
765
  time = parse_now("this second")
526
766
  assert_equal Time.local(2006, 8, 16, 14), time
527
767
 
@@ -614,6 +854,20 @@ class TestParsing < Test::Unit::TestCase
614
854
  assert_equal Time.local(2006, 8, 8, 12), time
615
855
  end
616
856
 
857
+ def test_parse_guess_a_ago
858
+ time = parse_now("AN hour ago")
859
+ assert_equal Time.local(2006, 8, 16, 13), time
860
+
861
+ time = parse_now("A day ago")
862
+ assert_equal Time.local(2006, 8, 15, 14), time
863
+
864
+ time = parse_now("a month ago")
865
+ assert_equal Time.local(2006, 7, 16, 14), time
866
+
867
+ time = parse_now("a year ago")
868
+ assert_equal Time.local(2005, 8, 16, 14), time
869
+ end
870
+
617
871
  def test_parse_guess_s_r_p
618
872
  # past
619
873
 
@@ -690,6 +944,16 @@ class TestParsing < Test::Unit::TestCase
690
944
 
691
945
  time = Chronic.parse("2 months ago", :now => Time.parse("2007-03-07 23:30"))
692
946
  assert_equal Time.local(2007, 1, 7, 23, 30), time
947
+
948
+ # Two repeaters
949
+ time = parse_now("25 minutes and 20 seconds from now")
950
+ assert_equal Time.local(2006, 8, 16, 14, 25, 20), time
951
+
952
+ time = parse_now("24 hours and 20 minutes from now")
953
+ assert_equal Time.local(2006, 8, 17, 14, 20, 0), time
954
+
955
+ time = parse_now("24 hours 20 minutes from now")
956
+ assert_equal Time.local(2006, 8, 17, 14, 20, 0), time
693
957
  end
694
958
 
695
959
  def test_parse_guess_p_s_r
@@ -776,6 +1040,28 @@ class TestParsing < Test::Unit::TestCase
776
1040
  assert_equal parse_now("33 days from now"), parse_now("thirty-three days from now")
777
1041
  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")
778
1042
  assert_equal parse_now("may 10th"), parse_now("may tenth")
1043
+ assert_equal parse_now("second monday in january"), parse_now("2nd monday in january")
1044
+ end
1045
+
1046
+ def test_relative_to_an_hour_before
1047
+ # example prenormalization "10 to 2" becomes "10 minutes past 2"
1048
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 to 2")
1049
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 till 2")
1050
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 prior to 2")
1051
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 before 2")
1052
+
1053
+ # uses the current hour, so 2006-08-16 13:50:00, not 14:50
1054
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 to")
1055
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 till")
1056
+
1057
+ assert_equal Time.local(2006, 8, 16, 15, 45), parse_now("quarter to 4")
1058
+ end
1059
+
1060
+ def test_relative_to_an_hour_after
1061
+ # not nil
1062
+ assert_equal Time.local(2006, 8, 16, 14, 10), parse_now("10 after 2")
1063
+ assert_equal Time.local(2006, 8, 16, 14, 10), parse_now("10 past 2")
1064
+ assert_equal Time.local(2006, 8, 16, 14, 30), parse_now("half past 2")
779
1065
  end
780
1066
 
781
1067
  def test_parse_only_complete_pointers
@@ -794,16 +1080,6 @@ class TestParsing < Test::Unit::TestCase
794
1080
  assert_equal Time.local(2006, 8, 16, 18, 30), parse_now("8/16/2006 at 6:30p")
795
1081
  end
796
1082
 
797
- def test_argument_validation
798
- assert_raise(ArgumentError) do
799
- time = Chronic.parse("may 27", :foo => :bar)
800
- end
801
-
802
- assert_raise(ArgumentError) do
803
- time = Chronic.parse("may 27", :context => :bar)
804
- end
805
- end
806
-
807
1083
  def test_seasons
808
1084
  t = parse_now("this spring", :guess => false)
809
1085
  assert_equal Time.local(2007, 3, 20), t.begin
@@ -855,11 +1131,105 @@ class TestParsing < Test::Unit::TestCase
855
1131
  t1 = Chronic.parse("now")
856
1132
  sleep 0.1
857
1133
  t2 = Chronic.parse("now")
858
- assert_not_equal t1, t2
1134
+ refute_equal t1, t2
1135
+ end
1136
+
1137
+ def test_noon
1138
+ t1 = Chronic.parse('2011-01-01 at noon', :ambiguous_time_range => :none)
1139
+ assert_equal Time.local(2011, 1, 1, 12, 0), t1
1140
+ end
1141
+
1142
+ def test_handle_rdn_rmn_sd
1143
+ time = parse_now("Thu Aug 10")
1144
+ assert_equal Time.local(2006, 8, 10, 12), time
1145
+
1146
+ time = parse_now("Thursday July 31")
1147
+ assert_equal Time.local(2006, 7, 31, 12), time
1148
+
1149
+ time = parse_now("Thursday December 31")
1150
+ assert_equal Time.local(2006, 12, 31, 12), time
1151
+ end
1152
+
1153
+ def test_handle_rdn_rmn_sd_rt
1154
+ time = parse_now("Thu Aug 10 4pm")
1155
+ assert_equal Time.local(2006, 8, 10, 16), time
1156
+
1157
+ time = parse_now("Thu Aug 10 at 4pm")
1158
+ assert_equal Time.local(2006, 8, 10, 16), time
1159
+ end
1160
+
1161
+ def test_handle_rdn_rmn_od_rt
1162
+ time = parse_now("Thu Aug 10th at 4pm")
1163
+ assert_equal Time.local(2006, 8, 10, 16), time
1164
+ end
1165
+
1166
+ def test_handle_rdn_od_rt
1167
+ time = parse_now("Thu 17th at 4pm")
1168
+ assert_equal Time.local(2006, 8, 17, 16), time
1169
+
1170
+ time = parse_now("Thu 16th at 4pm")
1171
+ assert_equal Time.local(2006, 8, 16, 16), time
1172
+
1173
+ time = parse_now("Thu 1st at 4pm")
1174
+ assert_equal Time.local(2006, 9, 1, 16), time
1175
+
1176
+ time = parse_now("Thu 1st at 4pm", :context => :past)
1177
+ assert_equal Time.local(2006, 8, 1, 16), time
1178
+ end
1179
+
1180
+ def test_handle_rdn_od
1181
+ time = parse_now("Thu 17th")
1182
+ assert_equal Time.local(2006, 8, 17, 12), time
1183
+ end
1184
+
1185
+ def test_handle_rdn_rmn_sd_sy
1186
+ time = parse_now("Thu Aug 10 2006")
1187
+ assert_equal Time.local(2006, 8, 10, 12), time
1188
+
1189
+ time = parse_now("Thursday July 31 2006")
1190
+ assert_equal Time.local(2006, 7, 31, 12), time
1191
+
1192
+ time = parse_now("Thursday December 31 2006")
1193
+ assert_equal Time.local(2006, 12, 31, 12), time
1194
+
1195
+ time = parse_now("Thursday December 30 2006")
1196
+ assert_equal Time.local(2006, 12, 30, 12), time
1197
+ end
1198
+
1199
+ def test_handle_rdn_rmn_od
1200
+ time = parse_now("Thu Aug 10th")
1201
+ assert_equal Time.local(2006, 8, 10, 12), time
1202
+
1203
+ time = parse_now("Thursday July 31st")
1204
+ assert_equal Time.local(2006, 7, 31, 12), time
1205
+
1206
+ time = parse_now("Thursday December 31st")
1207
+ assert_equal Time.local(2006, 12, 31, 12), time
1208
+ end
1209
+
1210
+ def test_handle_rdn_rmn_od_sy
1211
+ time = parse_now("Thu Aug 10th 2005")
1212
+ assert_equal Time.local(2005, 8, 10, 12), time
1213
+
1214
+ time = parse_now("Thursday July 31st 2005")
1215
+ assert_equal Time.local(2005, 7, 31, 12), time
1216
+
1217
+ time = parse_now("Thursday December 31st 2005")
1218
+ assert_equal Time.local(2005, 12, 31, 12), time
1219
+
1220
+ time = parse_now("Thursday December 30th 2005")
1221
+ assert_equal Time.local(2005, 12, 30, 12), time
1222
+ end
1223
+
1224
+ def test_normalizing_day_portions
1225
+ assert_equal pre_normalize("8:00 pm February 11"), pre_normalize("8:00 p.m. February 11")
859
1226
  end
860
1227
 
861
1228
  private
862
1229
  def parse_now(string, options={})
863
1230
  Chronic.parse(string, {:now => TIME_2006_08_16_14_00_00 }.merge(options))
864
1231
  end
1232
+ def pre_normalize(s)
1233
+ Chronic::Parser.new.pre_normalize s
1234
+ end
865
1235
  end
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class TestRepeaterDayName < Test::Unit::TestCase
3
+ class TestRepeaterDayName < TestCase
4
4
 
5
5
  def setup
6
6
  @now = Time.local(2006, 8, 16, 14, 0, 0, 0)