chronic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/README +119 -0
  2. data/lib/chronic.rb +30 -0
  3. data/lib/chronic/chronic.rb +242 -0
  4. data/lib/chronic/grabber.rb +26 -0
  5. data/lib/chronic/handlers.rb +405 -0
  6. data/lib/chronic/ordinal.rb +40 -0
  7. data/lib/chronic/pointer.rb +27 -0
  8. data/lib/chronic/repeater.rb +114 -0
  9. data/lib/chronic/repeaters/repeater_day.rb +40 -0
  10. data/lib/chronic/repeaters/repeater_day_name.rb +41 -0
  11. data/lib/chronic/repeaters/repeater_day_portion.rb +93 -0
  12. data/lib/chronic/repeaters/repeater_fortnight.rb +64 -0
  13. data/lib/chronic/repeaters/repeater_hour.rb +52 -0
  14. data/lib/chronic/repeaters/repeater_minute.rb +21 -0
  15. data/lib/chronic/repeaters/repeater_month.rb +54 -0
  16. data/lib/chronic/repeaters/repeater_month_name.rb +82 -0
  17. data/lib/chronic/repeaters/repeater_season.rb +23 -0
  18. data/lib/chronic/repeaters/repeater_season_name.rb +24 -0
  19. data/lib/chronic/repeaters/repeater_second.rb +34 -0
  20. data/lib/chronic/repeaters/repeater_time.rb +106 -0
  21. data/lib/chronic/repeaters/repeater_week.rb +62 -0
  22. data/lib/chronic/repeaters/repeater_weekend.rb +11 -0
  23. data/lib/chronic/repeaters/repeater_year.rb +55 -0
  24. data/lib/chronic/scalar.rb +74 -0
  25. data/lib/chronic/separator.rb +76 -0
  26. data/test/parse_numbers.rb +50 -0
  27. data/test/suite.rb +9 -0
  28. data/test/test_Chronic.rb +50 -0
  29. data/test/test_Handler.rb +110 -0
  30. data/test/test_RepeaterDayName.rb +52 -0
  31. data/test/test_RepeaterFortnight.rb +63 -0
  32. data/test/test_RepeaterHour.rb +65 -0
  33. data/test/test_RepeaterMonth.rb +47 -0
  34. data/test/test_RepeaterMonthName.rb +57 -0
  35. data/test/test_RepeaterTime.rb +72 -0
  36. data/test/test_RepeaterWeek.rb +63 -0
  37. data/test/test_RepeaterYear.rb +63 -0
  38. data/test/test_Span.rb +24 -0
  39. data/test/test_Token.rb +26 -0
  40. data/test/test_parsing.rb +472 -0
  41. metadata +87 -0
@@ -0,0 +1,65 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterHour < 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
+ hours = Chronic::RepeaterHour.new(:hour)
12
+ hours.start = @now
13
+
14
+ next_hour = hours.next(:future)
15
+ assert_equal Time.local(2006, 8, 16, 15), next_hour.begin
16
+ assert_equal Time.local(2006, 8, 16, 16), next_hour.end
17
+
18
+ next_next_hour = hours.next(:future)
19
+ assert_equal Time.local(2006, 8, 16, 16), next_next_hour.begin
20
+ assert_equal Time.local(2006, 8, 16, 17), next_next_hour.end
21
+ end
22
+
23
+ def test_next_past
24
+ hours = Chronic::RepeaterHour.new(:hour)
25
+ hours.start = @now
26
+
27
+ past_hour = hours.next(:past)
28
+ assert_equal Time.local(2006, 8, 16, 13), past_hour.begin
29
+ assert_equal Time.local(2006, 8, 16, 14), past_hour.end
30
+
31
+ past_past_hour = hours.next(:past)
32
+ assert_equal Time.local(2006, 8, 16, 12), past_past_hour.begin
33
+ assert_equal Time.local(2006, 8, 16, 13), past_past_hour.end
34
+ end
35
+
36
+ def test_this
37
+ @now = Time.local(2006, 8, 16, 14, 30)
38
+
39
+ hours = Chronic::RepeaterHour.new(:hour)
40
+ hours.start = @now
41
+
42
+ this_hour = hours.this(:future)
43
+ assert_equal Time.local(2006, 8, 16, 14, 31), this_hour.begin
44
+ assert_equal Time.local(2006, 8, 16, 15), this_hour.end
45
+
46
+ this_hour = hours.this(:past)
47
+ assert_equal Time.local(2006, 8, 16, 14), this_hour.begin
48
+ assert_equal Time.local(2006, 8, 16, 14, 30), this_hour.end
49
+ end
50
+
51
+ def test_offset
52
+ span = Chronic::Span.new(@now, @now + 1)
53
+
54
+ offset_span = Chronic::RepeaterHour.new(:hour).offset(span, 3, :future)
55
+
56
+ assert_equal Time.local(2006, 8, 16, 17), offset_span.begin
57
+ assert_equal Time.local(2006, 8, 16, 17, 0, 1), offset_span.end
58
+
59
+ offset_span = Chronic::RepeaterHour.new(:hour).offset(span, 24, :past)
60
+
61
+ assert_equal Time.local(2006, 8, 15, 14), offset_span.begin
62
+ assert_equal Time.local(2006, 8, 15, 14, 0, 1), offset_span.end
63
+ end
64
+
65
+ end
@@ -0,0 +1,47 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterMonth < 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_offset_by
12
+ # future
13
+
14
+ time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 1, :future)
15
+ assert_equal Time.local(2006, 9, 16, 14), time
16
+
17
+ time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 5, :future)
18
+ assert_equal Time.local(2007, 1, 16, 14), time
19
+
20
+ # past
21
+
22
+ time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 1, :past)
23
+ assert_equal Time.local(2006, 7, 16, 14), time
24
+
25
+ time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 10, :past)
26
+ assert_equal Time.local(2005, 10, 16, 14), time
27
+ end
28
+
29
+ def test_offset
30
+ # future
31
+
32
+ span = Chronic::Span.new(@now, @now + 60)
33
+ offset_span = Chronic::RepeaterMonth.new(:month).offset(span, 1, :future)
34
+
35
+ assert_equal Time.local(2006, 9, 16, 14), offset_span.begin
36
+ assert_equal Time.local(2006, 9, 16, 14, 1), offset_span.end
37
+
38
+ # past
39
+
40
+ span = Chronic::Span.new(@now, @now + 60)
41
+ offset_span = Chronic::RepeaterMonth.new(:month).offset(span, 1, :past)
42
+
43
+ assert_equal Time.local(2006, 7, 16, 14), offset_span.begin
44
+ assert_equal Time.local(2006, 7, 16, 14, 1), offset_span.end
45
+ end
46
+
47
+ end
@@ -0,0 +1,57 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterMonthName < 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
12
+ # future
13
+
14
+ mays = Chronic::RepeaterMonthName.new(:may)
15
+ mays.start = @now
16
+
17
+ next_may = mays.next(:future)
18
+ assert_equal Time.local(2007, 5), next_may.begin
19
+ assert_equal Time.local(2007, 6), next_may.end
20
+
21
+ next_next_may = mays.next(:future)
22
+ assert_equal Time.local(2008, 5), next_next_may.begin
23
+ assert_equal Time.local(2008, 6), next_next_may.end
24
+
25
+ decembers = Chronic::RepeaterMonthName.new(:december)
26
+ decembers.start = @now
27
+
28
+ next_december = decembers.next(:future)
29
+ assert_equal Time.local(2006, 12), next_december.begin
30
+ assert_equal Time.local(2007, 1), next_december.end
31
+
32
+ # past
33
+
34
+ mays = Chronic::RepeaterMonthName.new(:may)
35
+ mays.start = @now
36
+
37
+ assert_equal Time.local(2006, 5), mays.next(:past).begin
38
+ assert_equal Time.local(2005, 5), mays.next(:past).begin
39
+ end
40
+
41
+ def test_this
42
+ octobers = Chronic::RepeaterMonthName.new(:october)
43
+ octobers.start = @now
44
+
45
+ this_october = octobers.this(:future)
46
+ assert_equal Time.local(2006, 10, 1), this_october.begin
47
+ assert_equal Time.local(2006, 11, 1), this_october.end
48
+
49
+ aprils = Chronic::RepeaterMonthName.new(:april)
50
+ aprils.start = @now
51
+
52
+ this_april = aprils.this(:past)
53
+ assert_equal Time.local(2006, 4, 1), this_april.begin
54
+ assert_equal Time.local(2006, 5, 1), this_april.end
55
+ end
56
+
57
+ end
@@ -0,0 +1,72 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterTime < 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
+ t = Chronic::RepeaterTime.new('4:00')
13
+ t.start = @now
14
+
15
+ assert_equal Time.local(2006, 8, 16, 16), t.next(:future).begin
16
+ assert_equal Time.local(2006, 8, 17, 4), t.next(:future).begin
17
+
18
+ t = Chronic::RepeaterTime.new('13:00')
19
+ t.start = @now
20
+
21
+ assert_equal Time.local(2006, 8, 17, 13), t.next(:future).begin
22
+ assert_equal Time.local(2006, 8, 18, 13), t.next(:future).begin
23
+
24
+ t = Chronic::RepeaterTime.new('0400')
25
+ t.start = @now
26
+
27
+ assert_equal Time.local(2006, 8, 17, 4), t.next(:future).begin
28
+ assert_equal Time.local(2006, 8, 18, 4), t.next(:future).begin
29
+ end
30
+
31
+ def test_next_past
32
+ t = Chronic::RepeaterTime.new('4:00')
33
+ t.start = @now
34
+
35
+ assert_equal Time.local(2006, 8, 16, 4), t.next(:past).begin
36
+ assert_equal Time.local(2006, 8, 15, 16), t.next(:past).begin
37
+
38
+ t = Chronic::RepeaterTime.new('13:00')
39
+ t.start = @now
40
+
41
+ assert_equal Time.local(2006, 8, 16, 13), t.next(:past).begin
42
+ assert_equal Time.local(2006, 8, 15, 13), t.next(:past).begin
43
+ end
44
+
45
+ def test_type
46
+ t1 = Chronic::RepeaterTime.new('4')
47
+ assert_equal 14_400, t1.type.time
48
+
49
+ t1 = Chronic::RepeaterTime.new('14')
50
+ assert_equal 50_400, t1.type.time
51
+
52
+ t1 = Chronic::RepeaterTime.new('4:00')
53
+ assert_equal 14_400, t1.type.time
54
+
55
+ t1 = Chronic::RepeaterTime.new('4:30')
56
+ assert_equal 16_200, t1.type.time
57
+
58
+ t1 = Chronic::RepeaterTime.new('1400')
59
+ assert_equal 50_400, t1.type.time
60
+
61
+ t1 = Chronic::RepeaterTime.new('0400')
62
+ assert_equal 14_400, t1.type.time
63
+
64
+ t1 = Chronic::RepeaterTime.new('04')
65
+ assert_equal 14_400, t1.type.time
66
+
67
+ t1 = Chronic::RepeaterTime.new('400')
68
+ assert_equal 14_400, t1.type.time
69
+ end
70
+
71
+
72
+ end
@@ -0,0 +1,63 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterWeek < 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
+ weeks = Chronic::RepeaterWeek.new(:week)
12
+ weeks.start = @now
13
+
14
+ next_week = weeks.next(:future)
15
+ assert_equal Time.local(2006, 8, 20), next_week.begin
16
+ assert_equal Time.local(2006, 8, 27), next_week.end
17
+
18
+ next_next_week = weeks.next(:future)
19
+ assert_equal Time.local(2006, 8, 27), next_next_week.begin
20
+ assert_equal Time.local(2006, 9, 3), next_next_week.end
21
+ end
22
+
23
+ def test_next_past
24
+ weeks = Chronic::RepeaterWeek.new(:week)
25
+ weeks.start = @now
26
+
27
+ last_week = weeks.next(:past)
28
+ assert_equal Time.local(2006, 8, 6), last_week.begin
29
+ assert_equal Time.local(2006, 8, 13), last_week.end
30
+
31
+ last_last_week = weeks.next(:past)
32
+ assert_equal Time.local(2006, 7, 30), last_last_week.begin
33
+ assert_equal Time.local(2006, 8, 6), last_last_week.end
34
+ end
35
+
36
+ def test_this_future
37
+ weeks = Chronic::RepeaterWeek.new(:week)
38
+ weeks.start = @now
39
+
40
+ this_week = weeks.this(:future)
41
+ assert_equal Time.local(2006, 8, 16, 15), this_week.begin
42
+ assert_equal Time.local(2006, 8, 20), this_week.end
43
+ end
44
+
45
+ def test_this_past
46
+ weeks = Chronic::RepeaterWeek.new(:week)
47
+ weeks.start = @now
48
+
49
+ this_week = weeks.this(:past)
50
+ assert_equal Time.local(2006, 8, 13, 0), this_week.begin
51
+ assert_equal Time.local(2006, 8, 16, 14), this_week.end
52
+ end
53
+
54
+ def test_offset
55
+ span = Chronic::Span.new(@now, @now + 1)
56
+
57
+ offset_span = Chronic::RepeaterWeek.new(:week).offset(span, 3, :future)
58
+
59
+ assert_equal Time.local(2006, 9, 6, 14), offset_span.begin
60
+ assert_equal Time.local(2006, 9, 6, 14, 0, 1), offset_span.end
61
+ end
62
+
63
+ 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
@@ -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,472 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestParsing < Test::Unit::TestCase
5
+
6
+ def setup
7
+ # Wed Aug 16 14:00:00 UTC 2006
8
+ @time_2006_08_16_14_00_00 = Time.local(2006, 8, 16, 14, 0, 0, 0)
9
+ @time_2006_08_16_03_00_00 = Time.local(2006, 8, 16, 3, 0, 0, 0)
10
+ Chronic.debug = false
11
+ end
12
+
13
+ def test__parse_guess_dates
14
+ # rm_sd
15
+
16
+ time = Chronic.parse("may 27", :now => @time_2006_08_16_14_00_00)
17
+ assert_equal Time.local(2007, 5, 27, 12), time
18
+
19
+ time = Chronic.parse("may 28", :now => @time_2006_08_16_14_00_00, :context => :past)
20
+ assert_equal Time.local(2006, 5, 28, 12), time
21
+
22
+ time = Chronic.parse("may 28 5pm", :now => @time_2006_08_16_14_00_00, :context => :past)
23
+ assert_equal Time.local(2006, 5, 28, 17), time
24
+
25
+ time = Chronic.parse("may 28 at 5pm", :now => @time_2006_08_16_14_00_00, :context => :past)
26
+ assert_equal Time.local(2006, 5, 28, 17), time
27
+
28
+ # rm_od
29
+
30
+ time = Chronic.parse("may 27th", :now => @time_2006_08_16_14_00_00)
31
+ assert_equal Time.local(2007, 5, 27, 12), time
32
+
33
+ time = Chronic.parse("may 27th", :now => @time_2006_08_16_14_00_00, :context => :past)
34
+ assert_equal Time.local(2006, 5, 27, 12), time
35
+
36
+ time = Chronic.parse("may 27th 5:00 pm", :now => @time_2006_08_16_14_00_00, :context => :past)
37
+ assert_equal Time.local(2006, 5, 27, 17), time
38
+
39
+ time = Chronic.parse("may 27th at 5pm", :now => @time_2006_08_16_14_00_00, :context => :past)
40
+ assert_equal Time.local(2006, 5, 27, 17), time
41
+
42
+ time = Chronic.parse("may 27th at 5", :now => @time_2006_08_16_14_00_00, :ambiguous_time_range => :none)
43
+ assert_equal Time.local(2007, 5, 27, 5), time
44
+
45
+ # rm_sy
46
+
47
+ time = Chronic.parse("June 1979", :now => @time_2006_08_16_14_00_00)
48
+ assert_equal Time.local(1979, 6, 16, 0), time
49
+
50
+ time = Chronic.parse("dec 79", :now => @time_2006_08_16_14_00_00)
51
+ assert_equal Time.local(1979, 12, 16, 12), time
52
+
53
+ # rm_sd_sy
54
+
55
+ time = Chronic.parse("jan 3 2010", :now => @time_2006_08_16_14_00_00)
56
+ assert_equal Time.local(2010, 1, 3, 12), time
57
+
58
+ time = Chronic.parse("jan 3 2010 midnight", :now => @time_2006_08_16_14_00_00)
59
+ assert_equal Time.local(2010, 1, 4, 0), time
60
+
61
+ time = Chronic.parse("jan 3 2010 at midnight", :now => @time_2006_08_16_14_00_00)
62
+ assert_equal Time.local(2010, 1, 4, 0), time
63
+
64
+ time = Chronic.parse("jan 3 2010 at 4", :now => @time_2006_08_16_14_00_00, :ambiguous_time_range => :none)
65
+ assert_equal Time.local(2010, 1, 3, 4), time
66
+
67
+ #time = Chronic.parse("January 12, '00", :now => @time_2006_08_16_14_00_00)
68
+ #assert_equal Time.local(2000, 1, 12, 12), time
69
+
70
+ time = Chronic.parse("may 27 79", :now => @time_2006_08_16_14_00_00)
71
+ assert_equal Time.local(1979, 5, 27, 12), time
72
+
73
+ time = Chronic.parse("may 27 79 4:30", :now => @time_2006_08_16_14_00_00)
74
+ assert_equal Time.local(1979, 5, 27, 16, 30), time
75
+
76
+ time = Chronic.parse("may 27 79 at 4:30", :now => @time_2006_08_16_14_00_00, :ambiguous_time_range => :none)
77
+ assert_equal Time.local(1979, 5, 27, 4, 30), time
78
+
79
+ # sd_rm_sy
80
+
81
+ time = Chronic.parse("3 jan 2010", :now => @time_2006_08_16_14_00_00)
82
+ assert_equal Time.local(2010, 1, 3, 12), time
83
+
84
+ time = Chronic.parse("3 jan 2010 4pm", :now => @time_2006_08_16_14_00_00)
85
+ assert_equal Time.local(2010, 1, 3, 16), time
86
+
87
+ # sm_sd_sy
88
+
89
+ time = Chronic.parse("5/27/1979", :now => @time_2006_08_16_14_00_00)
90
+ assert_equal Time.local(1979, 5, 27, 12), time
91
+
92
+ time = Chronic.parse("5/27/1979 4am", :now => @time_2006_08_16_14_00_00)
93
+ assert_equal Time.local(1979, 5, 27, 4), time
94
+
95
+ # sd_sm_sy
96
+
97
+ time = Chronic.parse("27/5/1979", :now => @time_2006_08_16_14_00_00)
98
+ assert_equal Time.local(1979, 5, 27, 12), time
99
+
100
+ time = Chronic.parse("27/5/1979 @ 0700", :now => @time_2006_08_16_14_00_00)
101
+ assert_equal Time.local(1979, 5, 27, 7), time
102
+
103
+ # sm_sy
104
+
105
+ time = Chronic.parse("05/06", :now => @time_2006_08_16_14_00_00)
106
+ assert_equal Time.local(2006, 5, 16, 12), time
107
+
108
+ time = Chronic.parse("12/06", :now => @time_2006_08_16_14_00_00)
109
+ assert_equal Time.local(2006, 12, 16, 12), time
110
+
111
+ time = Chronic.parse("13/06", :now => @time_2006_08_16_14_00_00)
112
+ assert_equal nil, time
113
+
114
+ # sy_sm_sd
115
+
116
+ time = Chronic.parse("2000-1-1", :now => @time_2006_08_16_14_00_00)
117
+ assert_equal Time.local(2000, 1, 1, 12), time
118
+
119
+ time = Chronic.parse("2006-08-20", :now => @time_2006_08_16_14_00_00)
120
+ assert_equal Time.local(2006, 8, 20, 12), time
121
+
122
+ time = Chronic.parse("2006-08-20 7pm", :now => @time_2006_08_16_14_00_00)
123
+ assert_equal Time.local(2006, 8, 20, 19), time
124
+
125
+ time = Chronic.parse("2006-08-20 03:00", :now => @time_2006_08_16_14_00_00)
126
+ assert_equal Time.local(2006, 8, 20, 3), time
127
+
128
+ # rm_sd_rt
129
+
130
+ #time = Chronic.parse("jan 5 13:00", :now => @time_2006_08_16_14_00_00)
131
+ #assert_equal Time.local(2007, 1, 5, 13), time
132
+
133
+ # due to limitations of the Time class, these don't work
134
+
135
+ time = Chronic.parse("may 40", :now => @time_2006_08_16_14_00_00)
136
+ assert_equal nil, time
137
+
138
+ time = Chronic.parse("may 27 40", :now => @time_2006_08_16_14_00_00)
139
+ assert_equal nil, time
140
+
141
+ time = Chronic.parse("1800-08-20", :now => @time_2006_08_16_14_00_00)
142
+ assert_equal nil, time
143
+ end
144
+
145
+ def test_parse_guess_r
146
+ time = Chronic.parse("friday", :now => @time_2006_08_16_14_00_00)
147
+ assert_equal Time.local(2006, 8, 18, 12), time
148
+
149
+ time = Chronic.parse("5", :now => @time_2006_08_16_14_00_00)
150
+ assert_equal Time.local(2006, 8, 16, 17), time
151
+
152
+ time = Chronic.parse("5", :now => @time_2006_08_16_03_00_00, :ambiguous_time_range => :none)
153
+ assert_equal Time.local(2006, 8, 16, 5), time
154
+
155
+ time = Chronic.parse("13:00", :now => @time_2006_08_16_14_00_00)
156
+ assert_equal Time.local(2006, 8, 17, 13), time
157
+
158
+ time = Chronic.parse("13:45", :now => @time_2006_08_16_14_00_00)
159
+ assert_equal Time.local(2006, 8, 17, 13, 45), time
160
+
161
+ time = Chronic.parse("november", :now => @time_2006_08_16_14_00_00)
162
+ assert_equal Time.local(2006, 11, 16), time
163
+ end
164
+
165
+ def test_parse_guess_rr
166
+ time = Chronic.parse("friday 13:00", :now => @time_2006_08_16_14_00_00)
167
+ assert_equal Time.local(2006, 8, 18, 13), time
168
+
169
+ time = Chronic.parse("monday 4:00", :now => @time_2006_08_16_14_00_00)
170
+ assert_equal Time.local(2006, 8, 21, 16), time
171
+
172
+ time = Chronic.parse("sat 4:00", :now => @time_2006_08_16_14_00_00, :ambiguous_time_range => :none)
173
+ assert_equal Time.local(2006, 8, 19, 4), time
174
+
175
+ time = Chronic.parse("sunday 4:20", :now => @time_2006_08_16_14_00_00, :ambiguous_time_range => :none)
176
+ assert_equal Time.local(2006, 8, 20, 4, 20), time
177
+
178
+ time = Chronic.parse("4 pm", :now => @time_2006_08_16_14_00_00)
179
+ assert_equal Time.local(2006, 8, 16, 16), time
180
+
181
+ time = Chronic.parse("4 am", :now => @time_2006_08_16_14_00_00, :ambiguous_time_range => :none)
182
+ assert_equal Time.local(2006, 8, 16, 4), time
183
+
184
+ time = Chronic.parse("4:00 in the morning", :now => @time_2006_08_16_14_00_00)
185
+ assert_equal Time.local(2006, 8, 16, 4), time
186
+
187
+ #time = Chronic.parse("november 4", :now => @time_2006_08_16_14_00_00)
188
+ #assert_equal Time.local(2006, 11, 4, 12), time
189
+ end
190
+
191
+ def test_parse_guess_rrr
192
+ time = Chronic.parse("friday 1 pm", :now => @time_2006_08_16_14_00_00)
193
+ assert_equal Time.local(2006, 8, 18, 13), time
194
+
195
+ time = Chronic.parse("friday 11 at night", :now => @time_2006_08_16_14_00_00)
196
+ assert_equal Time.local(2006, 8, 18, 23), time
197
+
198
+ time = Chronic.parse("friday 11 in the evening", :now => @time_2006_08_16_14_00_00)
199
+ assert_equal Time.local(2006, 8, 18, 23), time
200
+
201
+ time = Chronic.parse("sunday 6am", :now => @time_2006_08_16_14_00_00)
202
+ assert_equal Time.local(2006, 8, 20, 6), time
203
+ end
204
+
205
+ def test_parse_guess_gr
206
+ # year
207
+
208
+ time = Chronic.parse("this year", :now => @time_2006_08_16_14_00_00)
209
+ assert_equal Time.local(2006, 10, 24, 12, 30), time
210
+
211
+ time = Chronic.parse("this year", :now => @time_2006_08_16_14_00_00, :context => :past)
212
+ assert_equal Time.local(2006, 4, 24, 12, 30), time
213
+
214
+ # month
215
+
216
+ time = Chronic.parse("this month", :now => @time_2006_08_16_14_00_00)
217
+ assert_equal Time.local(2006, 8, 24, 12), time
218
+
219
+ time = Chronic.parse("this month", :now => @time_2006_08_16_14_00_00, :context => :past)
220
+ assert_equal Time.local(2006, 8, 8, 12), time
221
+
222
+ # month name
223
+
224
+ time = Chronic.parse("last november", :now => @time_2006_08_16_14_00_00)
225
+ assert_equal Time.local(2005, 11, 16), time
226
+
227
+ # fortnight
228
+
229
+ time = Chronic.parse("this fortnight", :now => @time_2006_08_16_14_00_00)
230
+ assert_equal Time.local(2006, 8, 21, 19, 30), time
231
+
232
+ time = Chronic.parse("this fortnight", :now => @time_2006_08_16_14_00_00, :context => :past)
233
+ assert_equal Time.local(2006, 8, 14, 19), time
234
+
235
+ # week
236
+
237
+ time = Chronic.parse("this week", :now => @time_2006_08_16_14_00_00)
238
+ assert_equal Time.local(2006, 8, 18, 7, 30), time
239
+
240
+ time = Chronic.parse("this week", :now => @time_2006_08_16_14_00_00, :context => :past)
241
+ assert_equal Time.local(2006, 8, 14, 19), time
242
+
243
+ # day
244
+
245
+ time = Chronic.parse("this day", :now => @time_2006_08_16_14_00_00)
246
+ assert_equal Time.local(2006, 8, 16, 19, 30), time
247
+
248
+ time = Chronic.parse("this day", :now => @time_2006_08_16_14_00_00, :context => :past)
249
+ assert_equal Time.local(2006, 8, 16, 7), time
250
+
251
+ time = Chronic.parse("today", :now => @time_2006_08_16_14_00_00)
252
+ assert_equal Time.local(2006, 8, 16, 19, 30), time
253
+
254
+ time = Chronic.parse("yesterday", :now => @time_2006_08_16_14_00_00)
255
+ assert_equal Time.local(2006, 8, 15, 12), time
256
+
257
+ time = Chronic.parse("tomorrow", :now => @time_2006_08_16_14_00_00)
258
+ assert_equal Time.local(2006, 8, 17, 12), time
259
+
260
+ # day name
261
+
262
+ time = Chronic.parse("this tuesday", :now => @time_2006_08_16_14_00_00)
263
+ assert_equal Time.local(2006, 8, 22, 12), time
264
+
265
+ time = Chronic.parse("next tuesday", :now => @time_2006_08_16_14_00_00)
266
+ assert_equal Time.local(2006, 8, 22, 12), time
267
+
268
+ time = Chronic.parse("last tuesday", :now => @time_2006_08_16_14_00_00)
269
+ assert_equal Time.local(2006, 8, 15, 12), time
270
+
271
+ time = Chronic.parse("this wed", :now => @time_2006_08_16_14_00_00)
272
+ assert_equal Time.local(2006, 8, 23, 12), time
273
+
274
+ time = Chronic.parse("next wed", :now => @time_2006_08_16_14_00_00)
275
+ assert_equal Time.local(2006, 8, 23, 12), time
276
+
277
+ time = Chronic.parse("last wed", :now => @time_2006_08_16_14_00_00)
278
+ assert_equal Time.local(2006, 8, 9, 12), time
279
+
280
+ # day portion
281
+
282
+ time = Chronic.parse("this morning", :now => @time_2006_08_16_14_00_00)
283
+ assert_equal Time.local(2006, 8, 16, 9), time
284
+
285
+ time = Chronic.parse("tonight", :now => @time_2006_08_16_14_00_00)
286
+ assert_equal Time.local(2006, 8, 16, 22), time
287
+
288
+ # second
289
+
290
+ time = Chronic.parse("this second", :now => @time_2006_08_16_14_00_00)
291
+ assert_equal Time.local(2006, 8, 16, 14), time
292
+
293
+ time = Chronic.parse("this second", :now => @time_2006_08_16_14_00_00, :context => :past)
294
+ assert_equal Time.local(2006, 8, 16, 14), time
295
+
296
+ time = Chronic.parse("next second", :now => @time_2006_08_16_14_00_00)
297
+ assert_equal Time.local(2006, 8, 16, 14, 0, 1), time
298
+
299
+ time = Chronic.parse("last second", :now => @time_2006_08_16_14_00_00)
300
+ assert_equal Time.local(2006, 8, 16, 13, 59, 59), time
301
+ end
302
+
303
+ def test_parse_guess_grr
304
+ time = Chronic.parse("yesterday at 4:00", :now => @time_2006_08_16_14_00_00)
305
+ assert_equal Time.local(2006, 8, 15, 16), time
306
+
307
+ time = Chronic.parse("yesterday at 4:00", :now => @time_2006_08_16_14_00_00, :ambiguous_time_range => :none)
308
+ assert_equal Time.local(2006, 8, 15, 4), time
309
+
310
+ time = Chronic.parse("last friday at 4:00", :now => @time_2006_08_16_14_00_00)
311
+ assert_equal Time.local(2006, 8, 11, 16), time
312
+
313
+ time = Chronic.parse("next wed 4:00", :now => @time_2006_08_16_14_00_00)
314
+ assert_equal Time.local(2006, 8, 23, 16), time
315
+
316
+ time = Chronic.parse("yesterday afternoon", :now => @time_2006_08_16_14_00_00)
317
+ assert_equal Time.local(2006, 8, 15, 15), time
318
+
319
+ time = Chronic.parse("last week tuesday", :now => @time_2006_08_16_14_00_00)
320
+ assert_equal Time.local(2006, 8, 8, 12), time
321
+ end
322
+
323
+ def test_parse_guess_grrr
324
+ time = Chronic.parse("yesterday at 4:00pm", :now => @time_2006_08_16_14_00_00)
325
+ assert_equal Time.local(2006, 8, 15, 16), time
326
+ end
327
+
328
+ def test_parse_guess_rgr
329
+ time = Chronic.parse("afternoon yesterday", :now => @time_2006_08_16_14_00_00)
330
+ assert_equal Time.local(2006, 8, 15, 15), time
331
+
332
+ time = Chronic.parse("tuesday last week", :now => @time_2006_08_16_14_00_00)
333
+ assert_equal Time.local(2006, 8, 8, 12), time
334
+ end
335
+
336
+ def test_parse_guess_s_r_p
337
+ # past
338
+
339
+ time = Chronic.parse("3 years ago", :now => @time_2006_08_16_14_00_00)
340
+ assert_equal Time.local(2003, 8, 16, 14, 30, 30), time
341
+
342
+ time = Chronic.parse("1 month ago", :now => @time_2006_08_16_14_00_00)
343
+ assert_equal Time.local(2006, 7, 16, 14, 30, 30), time
344
+
345
+ time = Chronic.parse("1 fortnight ago", :now => @time_2006_08_16_14_00_00)
346
+ assert_equal Time.local(2006, 8, 2, 14, 30, 30), time
347
+
348
+ time = Chronic.parse("2 fortnights ago", :now => @time_2006_08_16_14_00_00)
349
+ assert_equal Time.local(2006, 7, 19, 14, 30, 30), time
350
+
351
+ time = Chronic.parse("3 weeks ago", :now => @time_2006_08_16_14_00_00)
352
+ assert_equal Time.local(2006, 7, 26, 14, 30, 30), time
353
+
354
+ time = Chronic.parse("3 days ago", :now => @time_2006_08_16_14_00_00)
355
+ assert_equal Time.local(2006, 8, 13, 14, 0, 30), time
356
+
357
+ #time = Chronic.parse("1 monday ago", :now => @time_2006_08_16_14_00_00)
358
+ #assert_equal Time.local(2006, 8, 14, 12), time
359
+
360
+ time = Chronic.parse("5 mornings ago", :now => @time_2006_08_16_14_00_00)
361
+ assert_equal Time.local(2006, 8, 12, 9), time
362
+
363
+ time = Chronic.parse("7 hours ago", :now => @time_2006_08_16_14_00_00)
364
+ assert_equal Time.local(2006, 8, 16, 7, 0, 30), time
365
+
366
+ time = Chronic.parse("3 minutes ago", :now => @time_2006_08_16_14_00_00)
367
+ assert_equal Time.local(2006, 8, 16, 13, 57), time
368
+
369
+ time = Chronic.parse("20 seconds before now", :now => @time_2006_08_16_14_00_00)
370
+ assert_equal Time.local(2006, 8, 16, 13, 59, 40), time
371
+
372
+ # future
373
+
374
+ time = Chronic.parse("3 years from now", :now => @time_2006_08_16_14_00_00)
375
+ assert_equal Time.local(2009, 8, 16, 14, 30, 30), time
376
+
377
+ time = Chronic.parse("6 months hence", :now => @time_2006_08_16_14_00_00)
378
+ assert_equal Time.local(2007, 2, 16, 14, 30, 30), time
379
+
380
+ time = Chronic.parse("3 fortnights hence", :now => @time_2006_08_16_14_00_00)
381
+ assert_equal Time.local(2006, 9, 27, 14, 30, 30), time
382
+
383
+ time = Chronic.parse("1 week from now", :now => @time_2006_08_16_14_00_00)
384
+ assert_equal Time.local(2006, 8, 23, 14, 30, 30), time
385
+
386
+ time = Chronic.parse("1 day hence", :now => @time_2006_08_16_14_00_00)
387
+ assert_equal Time.local(2006, 8, 17, 14, 0, 30), time
388
+
389
+ time = Chronic.parse("5 mornings hence", :now => @time_2006_08_16_14_00_00)
390
+ assert_equal Time.local(2006, 8, 21, 9), time
391
+
392
+ time = Chronic.parse("1 hour from now", :now => @time_2006_08_16_14_00_00)
393
+ assert_equal Time.local(2006, 8, 16, 15, 0, 30), time
394
+
395
+ time = Chronic.parse("20 minutes hence", :now => @time_2006_08_16_14_00_00)
396
+ assert_equal Time.local(2006, 8, 16, 14, 20), time
397
+
398
+ time = Chronic.parse("20 seconds from now", :now => @time_2006_08_16_14_00_00)
399
+ assert_equal Time.local(2006, 8, 16, 14, 0, 20), time
400
+ end
401
+
402
+ def test_parse_guess_p_s_r
403
+ time = Chronic.parse("in 3 hours", :now => @time_2006_08_16_14_00_00)
404
+ assert_equal Time.local(2006, 8, 16, 17, 0, 30), time
405
+ end
406
+
407
+ def test_parse_guess_s_r_p_a
408
+ # past
409
+
410
+ time = Chronic.parse("3 years ago tomorrow", :now => @time_2006_08_16_14_00_00)
411
+ assert_equal Time.local(2003, 8, 17, 12), time
412
+
413
+ time = Chronic.parse("3 years ago this friday", :now => @time_2006_08_16_14_00_00)
414
+ assert_equal Time.local(2003, 8, 18, 12), time
415
+
416
+ time = Chronic.parse("3 months ago saturday at 5:00 pm", :now => @time_2006_08_16_14_00_00)
417
+ assert_equal Time.local(2006, 5, 19, 17), time
418
+
419
+ time = Chronic.parse("2 days from this second", :now => @time_2006_08_16_14_00_00)
420
+ assert_equal Time.local(2006, 8, 18, 14), time
421
+
422
+ time = Chronic.parse("7 hours before tomorrow at midnight", :now => @time_2006_08_16_14_00_00)
423
+ assert_equal Time.local(2006, 8, 17, 17), time
424
+
425
+ # future
426
+ end
427
+
428
+ def test_parse_guess_o_r_s_r
429
+ time = Chronic.parse("3rd wednesday in november", :now => @time_2006_08_16_14_00_00)
430
+ assert_equal Time.local(2006, 11, 15, 12), time
431
+
432
+ time = Chronic.parse("10th wednesday in november", :now => @time_2006_08_16_14_00_00)
433
+ assert_equal nil, time
434
+ end
435
+
436
+ def test_parse_guess_o_r_g_r
437
+ time = Chronic.parse("3rd month next year", :now => @time_2006_08_16_14_00_00)
438
+ assert_equal Time.local(2007, 3, 16, 12, 30), time
439
+
440
+ time = Chronic.parse("3rd thursday this september", :now => @time_2006_08_16_14_00_00)
441
+ assert_equal Time.local(2006, 9, 21, 12), time
442
+
443
+ time = Chronic.parse("4th day last week", :now => @time_2006_08_16_14_00_00)
444
+ assert_equal Time.local(2006, 8, 9, 12), time
445
+ end
446
+
447
+ def test_parse_guess_nonsense
448
+ time = Chronic.parse("some stupid nonsense", :now => @time_2006_08_16_14_00_00)
449
+ assert_equal nil, time
450
+ end
451
+
452
+ def test_parse_span
453
+ span = Chronic.parse("friday", :now => @time_2006_08_16_14_00_00, :guess => false)
454
+ assert_equal Time.local(2006, 8, 18), span.begin
455
+ assert_equal Time.local(2006, 8, 19), span.end
456
+
457
+ span = Chronic.parse("november", :now => @time_2006_08_16_14_00_00, :guess => false)
458
+ assert_equal Time.local(2006, 11), span.begin
459
+ assert_equal Time.local(2006, 12), span.end
460
+ end
461
+
462
+ def test_argument_validation
463
+ assert_raise(Chronic::InvalidArgumentException) do
464
+ time = Chronic.parse("may 27", :foo => :bar)
465
+ end
466
+
467
+ assert_raise(Chronic::InvalidArgumentException) do
468
+ time = Chronic.parse("may 27", :context => :bar)
469
+ end
470
+ end
471
+
472
+ end