gitlab-chronic 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.gitlab-ci.yml +14 -0
  4. data/.travis.yml +10 -0
  5. data/Gemfile +3 -0
  6. data/HISTORY.md +252 -0
  7. data/LICENSE +21 -0
  8. data/README.md +188 -0
  9. data/Rakefile +68 -0
  10. data/chronic.gemspec +25 -0
  11. data/lib/chronic.rb +155 -0
  12. data/lib/chronic/date.rb +81 -0
  13. data/lib/chronic/definition.rb +128 -0
  14. data/lib/chronic/dictionary.rb +36 -0
  15. data/lib/chronic/handler.rb +97 -0
  16. data/lib/chronic/handlers.rb +672 -0
  17. data/lib/chronic/mini_date.rb +38 -0
  18. data/lib/chronic/parser.rb +222 -0
  19. data/lib/chronic/repeaters/repeater_day.rb +54 -0
  20. data/lib/chronic/repeaters/repeater_day_name.rb +53 -0
  21. data/lib/chronic/repeaters/repeater_day_portion.rb +109 -0
  22. data/lib/chronic/repeaters/repeater_fortnight.rb +72 -0
  23. data/lib/chronic/repeaters/repeater_hour.rb +59 -0
  24. data/lib/chronic/repeaters/repeater_minute.rb +59 -0
  25. data/lib/chronic/repeaters/repeater_month.rb +80 -0
  26. data/lib/chronic/repeaters/repeater_month_name.rb +95 -0
  27. data/lib/chronic/repeaters/repeater_quarter.rb +59 -0
  28. data/lib/chronic/repeaters/repeater_quarter_name.rb +40 -0
  29. data/lib/chronic/repeaters/repeater_season.rb +111 -0
  30. data/lib/chronic/repeaters/repeater_season_name.rb +43 -0
  31. data/lib/chronic/repeaters/repeater_second.rb +43 -0
  32. data/lib/chronic/repeaters/repeater_time.rb +159 -0
  33. data/lib/chronic/repeaters/repeater_week.rb +76 -0
  34. data/lib/chronic/repeaters/repeater_weekday.rb +86 -0
  35. data/lib/chronic/repeaters/repeater_weekend.rb +67 -0
  36. data/lib/chronic/repeaters/repeater_year.rb +78 -0
  37. data/lib/chronic/season.rb +26 -0
  38. data/lib/chronic/span.rb +31 -0
  39. data/lib/chronic/tag.rb +89 -0
  40. data/lib/chronic/tags/grabber.rb +29 -0
  41. data/lib/chronic/tags/ordinal.rb +52 -0
  42. data/lib/chronic/tags/pointer.rb +28 -0
  43. data/lib/chronic/tags/repeater.rb +160 -0
  44. data/lib/chronic/tags/scalar.rb +89 -0
  45. data/lib/chronic/tags/separator.rb +123 -0
  46. data/lib/chronic/tags/sign.rb +35 -0
  47. data/lib/chronic/tags/time_zone.rb +32 -0
  48. data/lib/chronic/time.rb +40 -0
  49. data/lib/chronic/token.rb +61 -0
  50. data/lib/chronic/tokenizer.rb +38 -0
  51. data/lib/chronic/version.rb +3 -0
  52. data/test/helper.rb +12 -0
  53. data/test/test_chronic.rb +203 -0
  54. data/test/test_daylight_savings.rb +122 -0
  55. data/test/test_handler.rb +128 -0
  56. data/test/test_mini_date.rb +32 -0
  57. data/test/test_parsing.rb +1537 -0
  58. data/test/test_repeater_day_name.rb +51 -0
  59. data/test/test_repeater_day_portion.rb +254 -0
  60. data/test/test_repeater_fortnight.rb +62 -0
  61. data/test/test_repeater_hour.rb +68 -0
  62. data/test/test_repeater_minute.rb +34 -0
  63. data/test/test_repeater_month.rb +50 -0
  64. data/test/test_repeater_month_name.rb +56 -0
  65. data/test/test_repeater_quarter.rb +70 -0
  66. data/test/test_repeater_quarter_name.rb +198 -0
  67. data/test/test_repeater_season.rb +40 -0
  68. data/test/test_repeater_time.rb +88 -0
  69. data/test/test_repeater_week.rb +115 -0
  70. data/test/test_repeater_weekday.rb +55 -0
  71. data/test/test_repeater_weekend.rb +74 -0
  72. data/test/test_repeater_year.rb +69 -0
  73. data/test/test_span.rb +23 -0
  74. data/test/test_token.rb +31 -0
  75. metadata +215 -0
@@ -0,0 +1,122 @@
1
+ require 'helper'
2
+
3
+ class TestDaylightSavings < TestCase
4
+
5
+ def setup
6
+ @begin_daylight_savings = Time.local(2008, 3, 9, 5, 0, 0, 0)
7
+ @end_daylight_savings = Time.local(2008, 11, 2, 5, 0, 0, 0)
8
+ end
9
+
10
+ def test_begin_past
11
+ # ambiguous - resolve to last night
12
+ t = Chronic::RepeaterTime.new('900')
13
+ t.start = @begin_daylight_savings
14
+ assert_equal Time.local(2008, 3, 8, 21), t.next(:past).begin
15
+
16
+ # ambiguous - resolve to this afternoon
17
+ t = Chronic::RepeaterTime.new('900')
18
+ t.start = Time.local(2008, 3, 9, 22, 0, 0, 0)
19
+ assert_equal Time.local(2008, 3, 9, 21), t.next(:past).begin
20
+
21
+ # ambiguous - resolve to this morning
22
+ t = Chronic::RepeaterTime.new('400')
23
+ t.start = @begin_daylight_savings
24
+ assert_equal Time.local(2008, 3, 9, 4), t.next(:past).begin
25
+
26
+ # unambiguous - resolve to today
27
+ t = Chronic::RepeaterTime.new('0400')
28
+ t.start = @begin_daylight_savings
29
+ assert_equal Time.local(2008, 3, 9, 4), t.next(:past).begin
30
+
31
+ # unambiguous - resolve to yesterday
32
+ t = Chronic::RepeaterTime.new('1300')
33
+ t.start = @begin_daylight_savings
34
+ assert_equal Time.local(2008, 3, 8, 13), t.next(:past).begin
35
+ end
36
+
37
+ def test_begin_future
38
+ # ambiguous - resolve to this morning
39
+ t = Chronic::RepeaterTime.new('900')
40
+ t.start = @begin_daylight_savings
41
+ assert_equal Time.local(2008, 3, 9, 9), t.next(:future).begin
42
+
43
+ # ambiguous - resolve to this afternoon
44
+ t = Chronic::RepeaterTime.new('900')
45
+ t.start = Time.local(2008, 3, 9, 13, 0, 0, 0)
46
+ assert_equal Time.local(2008, 3, 9, 21), t.next(:future).begin
47
+
48
+ # ambiguous - resolve to tomorrow
49
+ t = Chronic::RepeaterTime.new('900')
50
+ t.start = Time.local(2008, 3, 9, 22, 0, 0, 0)
51
+ assert_equal Time.local(2008, 3, 10, 9), t.next(:future).begin
52
+
53
+ # unambiguous - resolve to today
54
+ t = Chronic::RepeaterTime.new('0900')
55
+ t.start = @begin_daylight_savings
56
+ assert_equal Time.local(2008, 3, 9, 9), t.next(:future).begin
57
+
58
+ # unambiguous - resolve to tomorrow
59
+ t = Chronic::RepeaterTime.new('0400')
60
+ t.start = @begin_daylight_savings
61
+ assert_equal Time.local(2008, 3, 10, 4), t.next(:future).begin
62
+ end
63
+
64
+ def test_end_past
65
+ # ambiguous - resolve to last night
66
+ t = Chronic::RepeaterTime.new('900')
67
+ t.start = @end_daylight_savings
68
+ assert_equal Time.local(2008, 11, 1, 21), t.next(:past).begin
69
+
70
+ # ambiguous - resolve to this afternoon
71
+ t = Chronic::RepeaterTime.new('900')
72
+ t.start = Time.local(2008, 11, 2, 22, 0, 0, 0)
73
+ assert_equal Time.local(2008, 11, 2, 21), t.next(:past).begin
74
+
75
+ # ambiguous - resolve to this morning
76
+ t = Chronic::RepeaterTime.new('400')
77
+ t.start = @end_daylight_savings
78
+ assert_equal Time.local(2008, 11, 2, 4), t.next(:past).begin
79
+
80
+ # unambiguous - resolve to today
81
+ t = Chronic::RepeaterTime.new('0400')
82
+ t.start = @end_daylight_savings
83
+ assert_equal Time.local(2008, 11, 2, 4), t.next(:past).begin
84
+
85
+ # unambiguous - resolve to yesterday
86
+ t = Chronic::RepeaterTime.new('1300')
87
+ t.start = @end_daylight_savings
88
+ assert_equal Time.local(2008, 11, 1, 13), t.next(:past).begin
89
+
90
+ # unambiguous - resolve to yesterday
91
+ t = Chronic::RepeaterTime.new('1300')
92
+ t.start = @end_daylight_savings + 60 * 60 * 24 # Advance one day
93
+ assert_equal Time.local(2008, 11, 2, 13), t.next(:past).begin
94
+ end
95
+
96
+ def test_end_future
97
+ # ambiguous - resolve to this morning
98
+ t = Chronic::RepeaterTime.new('900')
99
+ t.start = @end_daylight_savings
100
+ assert_equal Time.local(2008, 11, 2, 9), t.next(:future).begin
101
+
102
+ # ambiguous - resolve to this afternoon
103
+ t = Chronic::RepeaterTime.new('900')
104
+ t.start = Time.local(2008, 11, 2, 13, 0, 0, 0)
105
+ assert_equal Time.local(2008, 11, 2, 21), t.next(:future).begin
106
+
107
+ # ambiguous - resolve to tomorrow
108
+ t = Chronic::RepeaterTime.new('900')
109
+ t.start = Time.local(2008, 11, 2, 22, 0, 0, 0)
110
+ assert_equal Time.local(2008, 11, 3, 9), t.next(:future).begin
111
+
112
+ # unambiguous - resolve to today
113
+ t = Chronic::RepeaterTime.new('0900')
114
+ t.start = @end_daylight_savings
115
+ assert_equal Time.local(2008, 11, 2, 9), t.next(:future).begin
116
+
117
+ # unambiguous - resolve to tomorrow
118
+ t = Chronic::RepeaterTime.new('0400')
119
+ t.start = @end_daylight_savings
120
+ assert_equal Time.local(2008, 11, 3, 4), t.next(:future).begin
121
+ end
122
+ end
@@ -0,0 +1,128 @@
1
+ require 'helper'
2
+
3
+ class TestHandler < TestCase
4
+
5
+ def setup
6
+ # Wed Aug 16 14:00:00 UTC 2006
7
+ @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
8
+ end
9
+
10
+ def definitions
11
+ @definitions ||= Chronic::SpanDictionary.new.definitions
12
+ end
13
+
14
+ def test_handler_class_1
15
+ handler = Chronic::Handler.new([:repeater], :handler)
16
+
17
+ tokens = [Chronic::Token.new('friday')]
18
+ tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
19
+
20
+ assert handler.match(tokens, definitions)
21
+
22
+ tokens << Chronic::Token.new('afternoon')
23
+ tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
24
+
25
+ assert !handler.match(tokens, definitions)
26
+ end
27
+
28
+ def test_handler_class_2
29
+ handler = Chronic::Handler.new([:repeater, :repeater?], :handler)
30
+
31
+ tokens = [Chronic::Token.new('friday')]
32
+ tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
33
+
34
+ assert handler.match(tokens, definitions)
35
+
36
+ tokens << Chronic::Token.new('afternoon')
37
+ tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
38
+
39
+ assert handler.match(tokens, definitions)
40
+
41
+ tokens << Chronic::Token.new('afternoon')
42
+ tokens[2].tag(Chronic::RepeaterDayPortion.new(:afternoon))
43
+
44
+ assert !handler.match(tokens, definitions)
45
+ end
46
+
47
+ def test_handler_class_3
48
+ handler = Chronic::Handler.new([:repeater, 'time?'], :handler)
49
+
50
+ tokens = [Chronic::Token.new('friday')]
51
+ tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
52
+
53
+ assert handler.match(tokens, definitions)
54
+
55
+ tokens << Chronic::Token.new('afternoon')
56
+ tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
57
+
58
+ assert !handler.match(tokens, definitions)
59
+ end
60
+
61
+ def test_handler_class_4
62
+ handler = Chronic::Handler.new([:repeater_month_name, :scalar_day, 'time?'], :handler)
63
+
64
+ tokens = [Chronic::Token.new('may')]
65
+ tokens[0].tag(Chronic::RepeaterMonthName.new(:may))
66
+
67
+ assert !handler.match(tokens, definitions)
68
+
69
+ tokens << Chronic::Token.new('27')
70
+ tokens[1].tag(Chronic::ScalarDay.new(27))
71
+
72
+ assert handler.match(tokens, definitions)
73
+ end
74
+
75
+ def test_handler_class_5
76
+ handler = Chronic::Handler.new([:repeater, 'time?'], :handler)
77
+
78
+ tokens = [Chronic::Token.new('friday')]
79
+ tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
80
+
81
+ assert handler.match(tokens, definitions)
82
+
83
+ tokens << Chronic::Token.new('5:00')
84
+ tokens[1].tag(Chronic::RepeaterTime.new('5:00'))
85
+
86
+ assert handler.match(tokens, definitions)
87
+
88
+ tokens << Chronic::Token.new('pm')
89
+ tokens[2].tag(Chronic::RepeaterDayPortion.new(:pm))
90
+
91
+ assert handler.match(tokens, definitions)
92
+ end
93
+
94
+ def test_handler_class_6
95
+ handler = Chronic::Handler.new([:scalar, :repeater, :pointer], :handler)
96
+
97
+ tokens = [Chronic::Token.new('3'),
98
+ Chronic::Token.new('years'),
99
+ Chronic::Token.new('past')]
100
+
101
+ tokens[0].tag(Chronic::Scalar.new(3))
102
+ tokens[1].tag(Chronic::RepeaterYear.new(:year))
103
+ tokens[2].tag(Chronic::Pointer.new(:past))
104
+
105
+ assert handler.match(tokens, definitions)
106
+ end
107
+
108
+ def test_handler_class_7
109
+ handler = Chronic::Handler.new([[:separator_on, :separator_at], :scalar], :handler)
110
+
111
+ tokens = [Chronic::Token.new('at'),
112
+ Chronic::Token.new('14')]
113
+
114
+ tokens[0].tag(Chronic::SeparatorAt.new('at'))
115
+ tokens[1].tag(Chronic::Scalar.new(14))
116
+
117
+ assert handler.match(tokens, definitions)
118
+
119
+ tokens = [Chronic::Token.new('on'),
120
+ Chronic::Token.new('15')]
121
+
122
+ tokens[0].tag(Chronic::SeparatorOn.new('on'))
123
+ tokens[1].tag(Chronic::Scalar.new(15))
124
+
125
+ assert handler.match(tokens, definitions)
126
+ end
127
+
128
+ end
@@ -0,0 +1,32 @@
1
+ require 'helper'
2
+
3
+ class TestMiniDate < TestCase
4
+ def test_valid_month
5
+ assert_raises(ArgumentError){ Chronic::MiniDate.new(0,12) }
6
+ assert_raises(ArgumentError){ Chronic::MiniDate.new(13,1) }
7
+ end
8
+
9
+ def test_is_between
10
+ m=Chronic::MiniDate.new(3,2)
11
+ assert m.is_between?(Chronic::MiniDate.new(2,4), Chronic::MiniDate.new(4,7))
12
+ assert !m.is_between?(Chronic::MiniDate.new(1,5), Chronic::MiniDate.new(2,7))
13
+
14
+ #There was a hang if date tested is in december and outside the testing range
15
+ m=Chronic::MiniDate.new(12,24)
16
+ assert !m.is_between?(Chronic::MiniDate.new(10,1), Chronic::MiniDate.new(12,21))
17
+ end
18
+
19
+ def test_is_between_short_range
20
+ m=Chronic::MiniDate.new(5,10)
21
+ assert m.is_between?(Chronic::MiniDate.new(5,3), Chronic::MiniDate.new(5,12))
22
+ assert !m.is_between?(Chronic::MiniDate.new(5,11), Chronic::MiniDate.new(5,15))
23
+ end
24
+
25
+ def test_is_between_wrapping_range
26
+ m=Chronic::MiniDate.new(1,1)
27
+ assert m.is_between?(Chronic::MiniDate.new(11,11), Chronic::MiniDate.new(2,2))
28
+ m=Chronic::MiniDate.new(12,12)
29
+ assert m.is_between?(Chronic::MiniDate.new(11,11), Chronic::MiniDate.new(1,5))
30
+ end
31
+
32
+ end
@@ -0,0 +1,1537 @@
1
+ require 'helper'
2
+
3
+ class TestParsing < TestCase
4
+ # Wed Aug 16 14:00:00 UTC 2006
5
+ TIME_2006_08_16_14_00_00 = Time.local(2006, 8, 16, 14, 0, 0, 0)
6
+
7
+ def setup
8
+ @time_2006_08_16_14_00_00 = TIME_2006_08_16_14_00_00
9
+ end
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("2013-08-01T19:30:00.34-07:00")
26
+ time2 = Time.parse("2013-08-01T19:30:00.34-07:00")
27
+ assert_in_delta time, time2, 0.001
28
+
29
+ time = Chronic.parse("2013-08-01T19:30:00.3456789-07:00")
30
+ time2 = Time.parse("2013-08-01T19:30:00.3456789-07:00")
31
+ assert_in_delta time, time2, 0.001
32
+
33
+ time = Chronic.parse("2012-08-02T12:00:00Z")
34
+ assert_equal Time.utc(2012, 8, 2, 12), time
35
+
36
+ time = Chronic.parse("2012-01-03 01:00:00.100")
37
+ time2 = Time.parse("2012-01-03 01:00:00.100")
38
+ assert_in_delta time, time2, 0.001
39
+
40
+ time = Chronic.parse("2012-01-03 01:00:00.234567")
41
+ time2 = Time.parse("2012-01-03 01:00:00.234567")
42
+ assert_in_delta time, time2, 0.000001
43
+
44
+ assert_nil Chronic.parse("1/1/32.1")
45
+
46
+ time = Chronic.parse("2020-11-01 00:00:00")
47
+ time2 = Time.parse("2020-11-01 00:00:00")
48
+ assert_in_delta time, time2, 0.000001
49
+
50
+ time = Chronic.parse("28th", {:guess => :begin})
51
+ assert_equal Time.new(Time.now.year, Time.now.month, 28), time
52
+ end
53
+
54
+ def test_handle_rmn_sd
55
+ time = parse_now("aug 3")
56
+ assert_equal Time.local(2007, 8, 3, 12), time
57
+
58
+ time = parse_now("aug 3", :context => :past)
59
+ assert_equal Time.local(2006, 8, 3, 12), time
60
+
61
+ time = parse_now("aug. 3")
62
+ assert_equal Time.local(2007, 8, 3, 12), time
63
+
64
+ time = parse_now("aug 20")
65
+ assert_equal Time.local(2006, 8, 20, 12), time
66
+
67
+ time = parse_now("aug-20")
68
+ assert_equal Time.local(2006, 8, 20, 12), time
69
+
70
+ time = parse_now("aug 20", :context => :future)
71
+ assert_equal Time.local(2006, 8, 20, 12), time
72
+
73
+ time = parse_now("may 27")
74
+ assert_equal Time.local(2007, 5, 27, 12), time
75
+
76
+ time = parse_now("may 28", :context => :past)
77
+ assert_equal Time.local(2006, 5, 28, 12), time
78
+
79
+ time = parse_now("may 28 5pm", :context => :past)
80
+ assert_equal Time.local(2006, 5, 28, 17), time
81
+
82
+ time = parse_now("may 28 at 5pm", :context => :past)
83
+ assert_equal Time.local(2006, 5, 28, 17), time
84
+
85
+ time = parse_now("may 28 at 5:32.19pm", :context => :past)
86
+ assert_equal Time.local(2006, 5, 28, 17, 32, 19), time
87
+
88
+ time = parse_now("may 28 at 5:32:19.764")
89
+ assert_in_delta Time.local(2007, 5, 28, 17, 32, 19, 764000), time, 0.001
90
+ end
91
+
92
+ def test_handle_rmn_sd_on
93
+ time = parse_now("5pm on may 28")
94
+ assert_equal Time.local(2007, 5, 28, 17), time
95
+
96
+ time = parse_now("5pm may 28")
97
+ assert_equal Time.local(2007, 5, 28, 17), time
98
+
99
+ time = parse_now("5 on may 28", :ambiguous_time_range => :none)
100
+ assert_equal Time.local(2007, 5, 28, 05), time
101
+ end
102
+
103
+ def test_handle_rmn_od
104
+ time = parse_now("may 27th")
105
+ assert_equal Time.local(2007, 5, 27, 12), time
106
+
107
+ time = parse_now("may 27th", :context => :past)
108
+ assert_equal Time.local(2006, 5, 27, 12), time
109
+
110
+ time = parse_now("may 27th 5:00 pm", :context => :past)
111
+ assert_equal Time.local(2006, 5, 27, 17), time
112
+
113
+ time = parse_now("may 27th at 5pm", :context => :past)
114
+ assert_equal Time.local(2006, 5, 27, 17), time
115
+
116
+ time = parse_now("may 27th at 5", :ambiguous_time_range => :none)
117
+ assert_equal Time.local(2007, 5, 27, 5), time
118
+ end
119
+
120
+ def test_handle_od_rm
121
+ time = parse_now("fifteenth of this month")
122
+ assert_equal Time.local(2007, 8, 15, 12), time
123
+ end
124
+
125
+ def test_handle_od_rmn
126
+ time = parse_now("22nd February")
127
+ assert_equal Time.local(2007, 2, 22, 12), time
128
+
129
+ time = parse_now("31st of may at 6:30pm")
130
+ assert_equal Time.local(2007, 5, 31, 18, 30), time
131
+
132
+ time = parse_now("11th december 8am")
133
+ assert_equal Time.local(2006, 12, 11, 8), time
134
+ end
135
+
136
+ def test_handle_sy_rmn_od
137
+ time = parse_now("2009 May 22nd")
138
+ assert_equal Time.local(2009, 05, 22, 12), time
139
+ end
140
+
141
+ def test_handle_sd_rmn
142
+ time = parse_now("22 February")
143
+ assert_equal Time.local(2007, 2, 22, 12), time
144
+
145
+ time = parse_now("22 feb")
146
+ assert_equal Time.local(2007, 2, 22, 12), time
147
+
148
+ time = parse_now("22-feb")
149
+ assert_equal Time.local(2007, 2, 22, 12), time
150
+
151
+ time = parse_now("31 of may at 6:30pm")
152
+ assert_equal Time.local(2007, 5, 31, 18, 30), time
153
+
154
+ time = parse_now("11 december 8am")
155
+ assert_equal Time.local(2006, 12, 11, 8), time
156
+ end
157
+
158
+ def test_handle_rmn_od_on
159
+ time = parse_now("5:00 pm may 27th", :context => :past)
160
+ assert_equal Time.local(2006, 5, 27, 17), time
161
+
162
+ time = parse_now("05:00 pm may 27th", :context => :past)
163
+ assert_equal Time.local(2006, 5, 27, 17), time
164
+
165
+ time = parse_now("5pm on may 27th", :context => :past)
166
+ assert_equal Time.local(2006, 5, 27, 17), time
167
+
168
+ time = parse_now("5 on may 27th", :ambiguous_time_range => :none)
169
+ assert_equal Time.local(2007, 5, 27, 5), time
170
+ end
171
+
172
+ def test_handle_rmn_sy
173
+ time = parse_now("may 97")
174
+ assert_equal Time.local(1997, 5, 16, 12), time
175
+
176
+ time = parse_now("may 33", :ambiguous_year_future_bias => 10)
177
+ assert_equal Time.local(2033, 5, 16, 12), time
178
+
179
+ time = parse_now("may 32")
180
+ assert_equal Time.local(2032, 5, 16, 12, 0, 0), time
181
+
182
+ time = parse_now("may '01")
183
+ assert_equal Time.local(2001, 5, 16, 12, 0, 0), time
184
+ end
185
+
186
+ def test_handle_rdn_rmn_sd_t_tz_sy
187
+ time = parse_now("Mon Apr 02 17:00:00 PDT 2007")
188
+ assert_equal 1175558400, time.to_i
189
+ end
190
+
191
+ def test_handle_sy_sm_sd_t_tz
192
+ time = parse_now("2011-07-03 22:11:35 +0100")
193
+ assert_equal 1309727495, time.to_i
194
+
195
+ time = parse_now("2011-07-03 22:11:35 +01:00")
196
+ assert_equal 1309727495, time.to_i
197
+
198
+ time = parse_now("2011-07-03 16:11:35 -05:00")
199
+ assert_equal 1309727495, time.to_i
200
+
201
+ time = parse_now("2011-07-03 21:11:35 UTC")
202
+ assert_equal 1309727495, time.to_i
203
+
204
+ time = parse_now("2011-07-03 21:11:35.362 UTC")
205
+ assert_in_delta 1309727495.362, time.to_f, 0.001
206
+ end
207
+
208
+ def test_handle_rmn_sd_sy
209
+ time = parse_now("November 18, 2010")
210
+ assert_equal Time.local(2010, 11, 18, 12), time
211
+
212
+ time = parse_now("Jan 1,2010")
213
+ assert_equal Time.local(2010, 1, 1, 12), time
214
+
215
+ time = parse_now("February 14, 2004")
216
+ assert_equal Time.local(2004, 2, 14, 12), time
217
+
218
+ time = parse_now("jan 3 2010")
219
+ assert_equal Time.local(2010, 1, 3, 12), time
220
+
221
+ time = parse_now("jan 3 2010 midnight")
222
+ assert_equal Time.local(2010, 1, 4, 0), time
223
+
224
+ time = parse_now("jan 3 2010 at midnight")
225
+ assert_equal Time.local(2010, 1, 4, 0), time
226
+
227
+ time = parse_now("jan 3 2010 at 4", :ambiguous_time_range => :none)
228
+ assert_equal Time.local(2010, 1, 3, 4), time
229
+
230
+ time = parse_now("may 27, 1979")
231
+ assert_equal Time.local(1979, 5, 27, 12), time
232
+
233
+ time = parse_now("may 27 79")
234
+ assert_equal Time.local(1979, 5, 27, 12), time
235
+
236
+ time = parse_now("may 27 79 4:30")
237
+ assert_equal Time.local(1979, 5, 27, 16, 30), time
238
+
239
+ time = parse_now("may 27 79 at 4:30", :ambiguous_time_range => :none)
240
+ assert_equal Time.local(1979, 5, 27, 4, 30), time
241
+
242
+ time = parse_now("may 27 32")
243
+ assert_equal Time.local(2032, 5, 27, 12, 0, 0), time
244
+
245
+ time = parse_now("oct 5 2012 1045pm")
246
+ assert_equal Time.local(2012, 10, 5, 22, 45), time
247
+ end
248
+
249
+ def test_handle_rmn_od_sy
250
+ time = parse_now("may 1st 01")
251
+ assert_equal Time.local(2001, 5, 1, 12), time
252
+
253
+ time = parse_now("November 18th 2010")
254
+ assert_equal Time.local(2010, 11, 18, 12), time
255
+
256
+ time = parse_now("November 18th, 2010")
257
+ assert_equal Time.local(2010, 11, 18, 12), time
258
+
259
+ time = parse_now("November 18th 2010 midnight")
260
+ assert_equal Time.local(2010, 11, 19, 0), time
261
+
262
+ time = parse_now("November 18th 2010 at midnight")
263
+ assert_equal Time.local(2010, 11, 19, 0), time
264
+
265
+ time = parse_now("November 18th 2010 at 4")
266
+ assert_equal Time.local(2010, 11, 18, 16), time
267
+
268
+ time = parse_now("November 18th 2010 at 4", :ambiguous_time_range => :none)
269
+ assert_equal Time.local(2010, 11, 18, 4), time
270
+
271
+ time = parse_now("March 30th, 1979")
272
+ assert_equal Time.local(1979, 3, 30, 12), time
273
+
274
+ time = parse_now("March 30th 79")
275
+ assert_equal Time.local(1979, 3, 30, 12), time
276
+
277
+ time = parse_now("March 30th 79 4:30")
278
+ assert_equal Time.local(1979, 3, 30, 16, 30), time
279
+
280
+ time = parse_now("March 30th 79 at 4:30", :ambiguous_time_range => :none)
281
+ assert_equal Time.local(1979, 3, 30, 4, 30), time
282
+ end
283
+
284
+ def test_handle_od_rmn_sy
285
+ time = parse_now("22nd February 2012")
286
+ assert_equal Time.local(2012, 2, 22, 12), time
287
+
288
+ time = parse_now("11th december 79")
289
+ assert_equal Time.local(1979, 12, 11, 12), time
290
+ end
291
+
292
+ def test_handle_sd_rmn_sy
293
+ time = parse_now("3 jan 2010")
294
+ assert_equal Time.local(2010, 1, 3, 12), time
295
+
296
+ time = parse_now("3 jan 2010 4pm")
297
+ assert_equal Time.local(2010, 1, 3, 16), time
298
+
299
+ time = parse_now("27 Oct 2006 7:30pm")
300
+ assert_equal Time.local(2006, 10, 27, 19, 30), time
301
+
302
+ time = parse_now("3 jan 10")
303
+ assert_equal Time.local(2010, 1, 3, 12), time
304
+
305
+ time = parse_now("3 jan 10", :endian_precedence => :little)
306
+ assert_equal Time.local(2010, 1, 3, 12), time
307
+
308
+ time = parse_now("3 jan 10", :endian_precedence => :middle)
309
+ assert_equal Time.local(2010, 1, 3, 12), time
310
+ end
311
+
312
+ def test_handle_sm_sd_sy
313
+ time = parse_now("5/27/1979")
314
+ assert_equal Time.local(1979, 5, 27, 12), time
315
+
316
+ time = parse_now("5/27/1979 4am")
317
+ assert_equal Time.local(1979, 5, 27, 4), time
318
+
319
+ time = parse_now("7/12/11")
320
+ assert_equal Time.local(2011, 7, 12, 12), time
321
+
322
+ time = parse_now("7/12/11", :endian_precedence => :little)
323
+ assert_equal Time.local(2011, 12, 7, 12), time
324
+
325
+ time = parse_now("9/19/2011 6:05:57 PM")
326
+ assert_equal Time.local(2011, 9, 19, 18, 05, 57), time
327
+
328
+ # month day overflows
329
+ time = parse_now("30/2/2000")
330
+ assert_nil time
331
+
332
+ time = parse_now("2013-03-12 17:00", :context => :past)
333
+ assert_equal Time.local(2013, 3, 12, 17, 0, 0), time
334
+ end
335
+
336
+ def test_handle_sd_sm_sy
337
+ time = parse_now("27/5/1979")
338
+ assert_equal Time.local(1979, 5, 27, 12), time
339
+
340
+ time = parse_now("27/5/1979 @ 0700")
341
+ assert_equal Time.local(1979, 5, 27, 7), time
342
+
343
+ time = parse_now("03/18/2012 09:26 pm")
344
+ assert_equal Time.local(2012, 3, 18, 21, 26), time
345
+
346
+ time = parse_now("30.07.2013 16:34:22")
347
+ assert_equal Time.local(2013, 7, 30, 16, 34, 22), time
348
+
349
+ time = parse_now("09.08.2013")
350
+ assert_equal Time.local(2013, 8, 9, 12), time
351
+
352
+ time = parse_now("9.8.2013")
353
+ assert_equal Time.local(2013, 8, 9, 12), time
354
+
355
+ time = parse_now("30-07-2013 21:53:49")
356
+ assert_equal Time.local(2013, 7, 30, 21, 53, 49), time
357
+ end
358
+
359
+ def test_handle_sy_sm_sd
360
+ time = parse_now("2000-1-1")
361
+ assert_equal Time.local(2000, 1, 1, 12), time
362
+
363
+ time = parse_now("2006-08-20")
364
+ assert_equal Time.local(2006, 8, 20, 12), time
365
+
366
+ time = parse_now("2006-08-20 7pm")
367
+ assert_equal Time.local(2006, 8, 20, 19), time
368
+
369
+ time = parse_now("2006-08-20 03:00")
370
+ assert_equal Time.local(2006, 8, 20, 3), time
371
+
372
+ time = parse_now("2006-08-20 03:30:30")
373
+ assert_equal Time.local(2006, 8, 20, 3, 30, 30), time
374
+
375
+ time = parse_now("2006-08-20 15:30:30")
376
+ assert_equal Time.local(2006, 8, 20, 15, 30, 30), time
377
+
378
+ time = parse_now("2006-08-20 15:30.30")
379
+ assert_equal Time.local(2006, 8, 20, 15, 30, 30), time
380
+
381
+ time = parse_now("2006-08-20 15:30:30:000536")
382
+ assert_in_delta Time.local(2006, 8, 20, 15, 30, 30, 536), time, 0.000001
383
+
384
+ time = parse_now("1902-08-20")
385
+ assert_equal Time.local(1902, 8, 20, 12, 0, 0), time
386
+
387
+ time = parse_now("2013.07.30 11:45:23")
388
+ assert_equal Time.local(2013, 7, 30, 11, 45, 23), time
389
+
390
+ time = parse_now("2013.08.09")
391
+ assert_equal Time.local(2013, 8, 9, 12, 0, 0), time
392
+
393
+ # exif date time original
394
+ time = parse_now("2012:05:25 22:06:50")
395
+ assert_equal Time.local(2012, 5, 25, 22, 6, 50), time
396
+ end
397
+
398
+ def test_handle_sm_sd
399
+ time = parse_now("05/06")
400
+ assert_equal Time.local(2007, 5, 6, 12), time
401
+
402
+ time = parse_now("05/06", :endian_precedence => [:little, :medium])
403
+ assert_equal Time.local(2007, 6, 5, 12), time
404
+
405
+ time = parse_now("05/06 6:05:57 PM")
406
+ assert_equal Time.local(2007, 5, 6, 18, 05, 57), time
407
+
408
+ time = parse_now("05/06 6:05:57 PM", :endian_precedence => [:little, :medium])
409
+ assert_equal Time.local(2007, 6, 5, 18, 05, 57), time
410
+
411
+ time = parse_now("13/09")
412
+ assert_equal Time.local(2006, 9, 13, 12), time
413
+
414
+ # future
415
+ time = parse_now("05/06") # future is default context
416
+ assert_equal Time.local(2007, 5, 6, 12), time
417
+
418
+ time = parse_now("1/13", :context => :future)
419
+ assert_equal Time.local(2007, 1, 13, 12), time
420
+
421
+ time = parse_now("3/13", :context => :none)
422
+ assert_equal Time.local(2006, 3, 13, 12), time
423
+
424
+ time = parse_now("12/1", :context => :past)
425
+ assert_equal Time.local(2005, 12, 1, 12), time
426
+
427
+ time = parse_now("12/1", :context => :future)
428
+ assert_equal Time.local(2006, 12, 1, 12), time
429
+
430
+ time = parse_now("12/1", :context => :none)
431
+ assert_equal Time.local(2006, 12, 1, 12), time
432
+
433
+ time = parse_now("8/1", :context => :past)
434
+ assert_equal Time.local(2006, 8, 1, 12), time
435
+
436
+ time = parse_now("8/1", :context => :future)
437
+ assert_equal Time.local(2007, 8, 1, 12), time
438
+
439
+ time = parse_now("8/1", :context => :none)
440
+ assert_equal Time.local(2006, 8, 1, 12), time
441
+
442
+ time = parse_now("1/1", :context => :past)
443
+ assert_equal Time.local(2006, 1, 1, 12), time
444
+
445
+ time = parse_now("1/1", :context => :future)
446
+ assert_equal Time.local(2007, 1, 1, 12), time
447
+
448
+ time = parse_now("1/1", :context => :none)
449
+ assert_equal Time.local(2006, 1, 1, 12), time
450
+ end
451
+
452
+ # def test_handle_sm_sy
453
+ # time = parse_now("05/06")
454
+ # assert_equal Time.local(2006, 5, 16, 12), time
455
+ #
456
+ # time = parse_now("12/06")
457
+ # assert_equal Time.local(2006, 12, 16, 12), time
458
+ #
459
+ # time = parse_now("13/06")
460
+ # assert_nil time
461
+ # end
462
+
463
+ def test_handle_sy_sm
464
+ time = parse_now("2012-06")
465
+ assert_equal Time.local(2012, 06, 16), time
466
+
467
+ time = parse_now("2013/12")
468
+ assert_equal Time.local(2013, 12, 16, 12, 0), time
469
+ end
470
+
471
+ def test_handle_r
472
+ time = parse_now("9am on Saturday")
473
+ assert_equal Time.local(2006, 8, 19, 9), time
474
+
475
+ time = parse_now("on Tuesday")
476
+ assert_equal Time.local(2006, 8, 22, 12), time
477
+
478
+ time = parse_now("1:00:00 PM")
479
+ assert_equal Time.local(2006, 8, 16, 13), time
480
+
481
+ time = parse_now("01:00:00 PM")
482
+ assert_equal Time.local(2006, 8, 16, 13), time
483
+
484
+ time = parse_now("today at 02:00:00", :hours24 => false)
485
+ assert_equal Time.local(2006, 8, 16, 14), time
486
+
487
+ time = parse_now("today at 02:00:00 AM", :hours24 => false)
488
+ assert_equal Time.local(2006, 8, 16, 2), time
489
+
490
+ time = parse_now("today at 3:00:00", :hours24 => true)
491
+ assert_equal Time.local(2006, 8, 16, 3), time
492
+
493
+ time = parse_now("today at 03:00:00", :hours24 => true)
494
+ assert_equal Time.local(2006, 8, 16, 3), time
495
+
496
+ time = parse_now("tomorrow at 4a.m.")
497
+ assert_equal Time.local(2006, 8, 17, 4), time
498
+ end
499
+
500
+ def test_handle_r_g_r
501
+ end
502
+
503
+ def test_handle_srp
504
+ end
505
+
506
+ def test_handle_s_r_p
507
+ end
508
+
509
+ def test_handle_p_s_r
510
+ end
511
+
512
+ def test_handle_s_r_p_a
513
+ time1 = parse_now("two days ago 0:0:0am")
514
+ time2 = parse_now("two days ago 00:00:00am")
515
+ assert_equal time1, time2
516
+ end
517
+
518
+ def test_handle_orr
519
+ time = parse_now("5th tuesday in january")
520
+ assert_equal Time.local(2007, 01, 30, 12), time
521
+
522
+ time = parse_now("5th tuesday in february")
523
+ assert_nil time
524
+
525
+ %W(jan feb march april may june july aug sep oct nov dec).each_with_index do |month, index|
526
+ time = parse_now("5th tuesday in #{month}")
527
+
528
+ if time then
529
+ assert_equal time.month, index+1
530
+ end
531
+ end
532
+ end
533
+
534
+ def test_handle_o_r_s_r
535
+ time = parse_now("3rd wednesday in november")
536
+ assert_equal Time.local(2006, 11, 15, 12), time
537
+
538
+ time = parse_now("10th wednesday in november")
539
+ assert_nil time
540
+
541
+ # time = parse_now("3rd wednesday in 2007")
542
+ # assert_equal Time.local(2007, 1, 20, 12), time
543
+ end
544
+
545
+ def test_handle_o_r_g_r
546
+ time = parse_now("3rd month next year", :guess => false)
547
+ assert_equal Time.local(2007, 3), time.begin
548
+
549
+ time = parse_now("3rd month next year", :guess => false)
550
+ assert_equal Time.local(2007, 3, 1), time.begin
551
+
552
+ time = parse_now("3rd thursday this september")
553
+ assert_equal Time.local(2006, 9, 21, 12), time
554
+
555
+ now = Time.parse("1/10/2010")
556
+ time = parse_now("3rd thursday this november", :now => now)
557
+ assert_equal Time.local(2010, 11, 18, 12), time
558
+
559
+ time = parse_now("4th day last week")
560
+ assert_equal Time.local(2006, 8, 9, 12), time
561
+ end
562
+
563
+ def test_handle_sm_rmn_sy
564
+ time = parse_now('30-Mar-11')
565
+ assert_equal Time.local(2011, 3, 30, 12), time
566
+
567
+ time = parse_now('31-Aug-12')
568
+ assert_equal Time.local(2012, 8, 31), time
569
+ end
570
+
571
+ # end of testing handlers
572
+
573
+ def test_parse_guess_r
574
+ time = parse_now("friday")
575
+ assert_equal Time.local(2006, 8, 18, 12), time
576
+
577
+ time = parse_now("tue")
578
+ assert_equal Time.local(2006, 8, 22, 12), time
579
+
580
+ time = parse_now("5")
581
+ assert_equal Time.local(2006, 8, 16, 17), time
582
+
583
+ time = Chronic.parse("5", :now => Time.local(2006, 8, 16, 3, 0, 0, 0), :ambiguous_time_range => :none)
584
+ assert_equal Time.local(2006, 8, 16, 5), time
585
+
586
+ time = parse_now("13:00")
587
+ assert_equal Time.local(2006, 8, 17, 13), time
588
+
589
+ time = parse_now("13:45")
590
+ assert_equal Time.local(2006, 8, 17, 13, 45), time
591
+
592
+ time = parse_now("1:01pm")
593
+ assert_equal Time.local(2006, 8, 16, 13, 01), time
594
+
595
+ time = parse_now("2:01pm")
596
+ assert_equal Time.local(2006, 8, 16, 14, 01), time
597
+
598
+ time = parse_now("november")
599
+ assert_equal Time.local(2006, 11, 16), time
600
+ end
601
+
602
+ def test_parse_guess_rr
603
+ time = parse_now("friday 13:00")
604
+ assert_equal Time.local(2006, 8, 18, 13), time
605
+
606
+ time = parse_now("monday 4:00")
607
+ assert_equal Time.local(2006, 8, 21, 16), time
608
+
609
+ time = parse_now("sat 4:00", :ambiguous_time_range => :none)
610
+ assert_equal Time.local(2006, 8, 19, 4), time
611
+
612
+ time = parse_now("sunday 4:20", :ambiguous_time_range => :none)
613
+ assert_equal Time.local(2006, 8, 20, 4, 20), time
614
+
615
+ time = parse_now("4 pm")
616
+ assert_equal Time.local(2006, 8, 16, 16), time
617
+
618
+ time = parse_now("4 am", :ambiguous_time_range => :none)
619
+ assert_equal Time.local(2006, 8, 16, 4), time
620
+
621
+ time = parse_now("12 pm")
622
+ assert_equal Time.local(2006, 8, 16, 12), time
623
+
624
+ time = parse_now("12:01 pm")
625
+ assert_equal Time.local(2006, 8, 16, 12, 1), time
626
+
627
+ time = parse_now("12:01 am")
628
+ assert_equal Time.local(2006, 8, 16, 0, 1), time
629
+
630
+ time = parse_now("12 am")
631
+ assert_equal Time.local(2006, 8, 16), time
632
+
633
+ time = parse_now("4:00 in the morning")
634
+ assert_equal Time.local(2006, 8, 16, 4), time
635
+
636
+ time = parse_now("0:10")
637
+ assert_equal Time.local(2006, 8, 17, 0, 10), time
638
+
639
+ time = parse_now("november 4")
640
+ assert_equal Time.local(2006, 11, 4, 12), time
641
+
642
+ time = parse_now("aug 24")
643
+ assert_equal Time.local(2006, 8, 24, 12), time
644
+ end
645
+
646
+ def test_parse_guess_rrr
647
+ time = parse_now("friday 1 pm")
648
+ assert_equal Time.local(2006, 8, 18, 13), time
649
+
650
+ time = parse_now("friday 11 at night")
651
+ assert_equal Time.local(2006, 8, 18, 23), time
652
+
653
+ time = parse_now("friday 11 in the evening")
654
+ assert_equal Time.local(2006, 8, 18, 23), time
655
+
656
+ time = parse_now("sunday 6am")
657
+ assert_equal Time.local(2006, 8, 20, 6), time
658
+
659
+ time = parse_now("friday evening at 7")
660
+ assert_equal Time.local(2006, 8, 18, 19), time
661
+ end
662
+
663
+ def test_parse_guess_gr
664
+ # year
665
+
666
+ time = parse_now("this year", :guess => false)
667
+ assert_equal Time.local(2006, 8, 17), time.begin
668
+
669
+ time = parse_now("this year", :context => :past, :guess => false)
670
+ assert_equal Time.local(2006, 1, 1), time.begin
671
+
672
+ # month
673
+
674
+ time = parse_now("this month")
675
+ assert_equal Time.local(2006, 8, 24, 12), time
676
+
677
+ time = parse_now("this month", :context => :past)
678
+ assert_equal Time.local(2006, 8, 8, 12), time
679
+
680
+ time = Chronic.parse("next month", :now => Time.local(2006, 11, 15))
681
+ assert_equal Time.local(2006, 12, 16, 12), time
682
+
683
+ # month name
684
+
685
+ time = parse_now("last november")
686
+ assert_equal Time.local(2005, 11, 16), time
687
+
688
+ # fortnight
689
+
690
+ time = parse_now("this fortnight")
691
+ assert_equal Time.local(2006, 8, 21, 19, 30), time
692
+
693
+ time = parse_now("this fortnight", :context => :past)
694
+ assert_equal Time.local(2006, 8, 14, 19), time
695
+
696
+ # week
697
+
698
+ time = parse_now("this week")
699
+ assert_equal Time.local(2006, 8, 18, 7, 30), time
700
+
701
+ time = parse_now("this week", :context => :past)
702
+ assert_equal Time.local(2006, 8, 14, 19), time
703
+
704
+ time = parse_now("this week", :context => :past, :guess => :begin)
705
+ assert_equal Time.local(2006, 8, 13), time
706
+
707
+ time = parse_now("this week", :context => :past, :guess => :begin, :week_start => :monday)
708
+ assert_equal Time.local(2006, 8, 14), time
709
+
710
+ # weekend
711
+
712
+ time = parse_now("this weekend")
713
+ assert_equal Time.local(2006, 8, 20), time
714
+
715
+ time = parse_now("this weekend", :context => :past)
716
+ assert_equal Time.local(2006, 8, 13), time
717
+
718
+ time = parse_now("last weekend")
719
+ assert_equal Time.local(2006, 8, 13), time
720
+
721
+ # day
722
+
723
+ time = parse_now("this day")
724
+ assert_equal Time.local(2006, 8, 16, 19), time
725
+
726
+ time = parse_now("this day", :context => :past)
727
+ assert_equal Time.local(2006, 8, 16, 7), time
728
+
729
+ time = parse_now("today")
730
+ assert_equal Time.local(2006, 8, 16, 19), time
731
+
732
+ time = parse_now("yesterday")
733
+ assert_equal Time.local(2006, 8, 15, 12), time
734
+
735
+ now = Time.parse("2011-05-27 23:10") # after 11pm
736
+ time = parse_now("yesterday", :now => now)
737
+ assert_equal Time.local(2011, 05, 26, 12), time
738
+
739
+ time = parse_now("tomorrow")
740
+ assert_equal Time.local(2006, 8, 17, 12), time
741
+
742
+ # day name
743
+
744
+ time = parse_now("this tuesday")
745
+ assert_equal Time.local(2006, 8, 22, 12), time
746
+
747
+ time = parse_now("next tuesday")
748
+ assert_equal Time.local(2006, 8, 22, 12), time
749
+
750
+ time = parse_now("last tuesday")
751
+ assert_equal Time.local(2006, 8, 15, 12), time
752
+
753
+ time = parse_now("this wed")
754
+ assert_equal Time.local(2006, 8, 23, 12), time
755
+
756
+ time = parse_now("next wed")
757
+ assert_equal Time.local(2006, 8, 23, 12), time
758
+
759
+ time = parse_now("last wed")
760
+ assert_equal Time.local(2006, 8, 9, 12), time
761
+
762
+ monday = Time.local(2006, 8, 21, 12)
763
+ assert_equal monday, parse_now("mon")
764
+ assert_equal monday, parse_now("mun")
765
+
766
+ tuesday = Time.local(2006, 8, 22, 12)
767
+ assert_equal tuesday, parse_now("tue")
768
+ assert_equal tuesday, parse_now("tus")
769
+
770
+ wednesday = Time.local(2006, 8, 23, 12)
771
+ assert_equal wednesday, parse_now("wed")
772
+ assert_equal wednesday, parse_now("wenns")
773
+
774
+ thursday = Time.local(2006, 8, 17, 12)
775
+ assert_equal thursday, parse_now("thu")
776
+ assert_equal thursday, parse_now("thur")
777
+
778
+ friday = Time.local(2006, 8, 18, 12)
779
+ assert_equal friday, parse_now("fri")
780
+ assert_equal friday, parse_now("fry")
781
+
782
+ saturday = Time.local(2006, 8, 19, 12)
783
+ assert_equal saturday, parse_now("sat")
784
+ assert_equal saturday, parse_now("satterday")
785
+
786
+ sunday = Time.local(2006, 8, 20, 12)
787
+ assert_equal sunday, parse_now("sun")
788
+ assert_equal sunday, parse_now("sum")
789
+
790
+ # day portion
791
+
792
+ time = parse_now("this morning")
793
+ assert_equal Time.local(2006, 8, 16, 9), time
794
+
795
+ time = parse_now("tonight")
796
+ assert_equal Time.local(2006, 8, 16, 22), time
797
+
798
+ # hour
799
+
800
+ time = parse_now("next hr")
801
+ assert_equal Time.local(2006, 8, 16, 15, 30, 0), time
802
+
803
+ time = parse_now("next hrs")
804
+ assert_equal Time.local(2006, 8, 16, 15, 30, 0), time
805
+
806
+ # minute
807
+
808
+ time = parse_now("next min")
809
+ assert_equal Time.local(2006, 8, 16, 14, 1, 30), time
810
+
811
+ time = parse_now("next mins")
812
+ assert_equal Time.local(2006, 8, 16, 14, 1, 30), time
813
+
814
+ time = parse_now("next minute")
815
+ assert_equal Time.local(2006, 8, 16, 14, 1, 30), time
816
+
817
+ # second
818
+
819
+ time = parse_now("next sec")
820
+ assert_equal Time.local(2006, 8, 16, 14, 0, 1), time
821
+
822
+ time = parse_now("next secs")
823
+ assert_equal Time.local(2006, 8, 16, 14, 0, 1), time
824
+
825
+ time = parse_now("this second")
826
+ assert_equal Time.local(2006, 8, 16, 14), time
827
+
828
+ time = parse_now("this second", :context => :past)
829
+ assert_equal Time.local(2006, 8, 16, 14), time
830
+
831
+ time = parse_now("next second")
832
+ assert_equal Time.local(2006, 8, 16, 14, 0, 1), time
833
+
834
+ time = parse_now("last second")
835
+ assert_equal Time.local(2006, 8, 16, 13, 59, 59), time
836
+ end
837
+
838
+ def test_parse_guess_grr
839
+ time = parse_now("yesterday at 4:00")
840
+ assert_equal Time.local(2006, 8, 15, 16), time
841
+
842
+ time = parse_now("today at 9:00")
843
+ assert_equal Time.local(2006, 8, 16, 9), time
844
+
845
+ time = parse_now("today at 2100")
846
+ assert_equal Time.local(2006, 8, 16, 21), time
847
+
848
+ time = parse_now("this day at 0900")
849
+ assert_equal Time.local(2006, 8, 16, 9), time
850
+
851
+ time = parse_now("tomorrow at 0900")
852
+ assert_equal Time.local(2006, 8, 17, 9), time
853
+
854
+ time = parse_now("yesterday at 4:00", :ambiguous_time_range => :none)
855
+ assert_equal Time.local(2006, 8, 15, 4), time
856
+
857
+ time = parse_now("last friday at 4:00")
858
+ assert_equal Time.local(2006, 8, 11, 16), time
859
+
860
+ time = parse_now("next wed 4:00")
861
+ assert_equal Time.local(2006, 8, 23, 16), time
862
+
863
+ time = parse_now("yesterday afternoon")
864
+ assert_equal Time.local(2006, 8, 15, 15), time
865
+
866
+ time = parse_now("last week tuesday")
867
+ assert_equal Time.local(2006, 8, 8, 12), time
868
+
869
+ time = parse_now("tonight at 7")
870
+ assert_equal Time.local(2006, 8, 16, 19), time
871
+
872
+ time = parse_now("tonight 7")
873
+ assert_equal Time.local(2006, 8, 16, 19), time
874
+
875
+ time = parse_now("7 tonight")
876
+ assert_equal Time.local(2006, 8, 16, 19), time
877
+ end
878
+
879
+ def test_parse_guess_grrr
880
+ time = parse_now("today at 6:00pm")
881
+ assert_equal Time.local(2006, 8, 16, 18), time
882
+
883
+ time = parse_now("today at 6:00am")
884
+ assert_equal Time.local(2006, 8, 16, 6), time
885
+
886
+ time = parse_now("this day 1800")
887
+ assert_equal Time.local(2006, 8, 16, 18), time
888
+
889
+ time = parse_now("yesterday at 4:00pm")
890
+ assert_equal Time.local(2006, 8, 15, 16), time
891
+
892
+ time = parse_now("tomorrow evening at 7")
893
+ assert_equal Time.local(2006, 8, 17, 19), time
894
+
895
+ time = parse_now("tomorrow morning at 5:30")
896
+ assert_equal Time.local(2006, 8, 17, 5, 30), time
897
+
898
+ time = parse_now("next monday at 12:01 am")
899
+ assert_equal Time.local(2006, 8, 21, 00, 1), time
900
+
901
+ time = parse_now("next monday at 12:01 pm")
902
+ assert_equal Time.local(2006, 8, 21, 12, 1), time
903
+
904
+ # with context
905
+ time = parse_now("sunday at 8:15pm", :context => :past)
906
+ assert_equal Time.local(2006, 8, 13, 20, 15), time
907
+ end
908
+
909
+ def test_parse_guess_rgr
910
+ time = parse_now("afternoon yesterday")
911
+ assert_equal Time.local(2006, 8, 15, 15), time
912
+
913
+ time = parse_now("tuesday last week")
914
+ assert_equal Time.local(2006, 8, 8, 12), time
915
+ end
916
+
917
+ def test_parse_guess_a_ago
918
+ time = parse_now("AN hour ago")
919
+ assert_equal Time.local(2006, 8, 16, 13), time
920
+
921
+ time = parse_now("A day ago")
922
+ assert_equal Time.local(2006, 8, 15, 14), time
923
+
924
+ time = parse_now("a month ago")
925
+ assert_equal Time.local(2006, 7, 16, 14), time
926
+
927
+ time = parse_now("a year ago")
928
+ assert_equal Time.local(2005, 8, 16, 14), time
929
+ end
930
+
931
+ def test_parse_guess_s_r_p
932
+ # past
933
+
934
+ time = parse_now("3 years ago")
935
+ assert_equal Time.local(2003, 8, 16, 14), time
936
+
937
+ time = parse_now("1 month ago")
938
+ assert_equal Time.local(2006, 7, 16, 14), time
939
+
940
+ time = parse_now("1 fortnight ago")
941
+ assert_equal Time.local(2006, 8, 2, 14), time
942
+
943
+ time = parse_now("2 fortnights ago")
944
+ assert_equal Time.local(2006, 7, 19, 14), time
945
+
946
+ time = parse_now("3 weeks ago")
947
+ assert_equal Time.local(2006, 7, 26, 14), time
948
+
949
+ time = parse_now("2 weekends ago")
950
+ assert_equal Time.local(2006, 8, 5), time
951
+
952
+ time = parse_now("3 days ago")
953
+ assert_equal Time.local(2006, 8, 13, 14), time
954
+
955
+ #time = parse_now("1 monday ago")
956
+ #assert_equal Time.local(2006, 8, 14, 12), time
957
+
958
+ time = parse_now("5 mornings ago")
959
+ assert_equal Time.local(2006, 8, 12, 9), time
960
+
961
+ time = parse_now("7 hours ago")
962
+ assert_equal Time.local(2006, 8, 16, 7), time
963
+
964
+ time = parse_now("3 minutes ago")
965
+ assert_equal Time.local(2006, 8, 16, 13, 57), time
966
+
967
+ time = parse_now("20 seconds before now")
968
+ assert_equal Time.local(2006, 8, 16, 13, 59, 40), time
969
+
970
+ # future
971
+
972
+ time = parse_now("3 years from now")
973
+ assert_equal Time.local(2009, 8, 16, 14, 0, 0), time
974
+
975
+ time = parse_now("6 months hence")
976
+ assert_equal Time.local(2007, 2, 16, 14), time
977
+
978
+ time = parse_now("3 fortnights hence")
979
+ assert_equal Time.local(2006, 9, 27, 14), time
980
+
981
+ time = parse_now("1 week from now")
982
+ assert_equal Time.local(2006, 8, 23, 14, 0, 0), time
983
+
984
+ time = parse_now("1 weekend from now")
985
+ assert_equal Time.local(2006, 8, 19), time
986
+
987
+ time = parse_now("2 weekends from now")
988
+ assert_equal Time.local(2006, 8, 26), time
989
+
990
+ time = parse_now("1 day hence")
991
+ assert_equal Time.local(2006, 8, 17, 14), time
992
+
993
+ time = parse_now("5 mornings hence")
994
+ assert_equal Time.local(2006, 8, 21, 9), time
995
+
996
+ time = parse_now("1 hour from now")
997
+ assert_equal Time.local(2006, 8, 16, 15), time
998
+
999
+ time = parse_now("20 minutes hence")
1000
+ assert_equal Time.local(2006, 8, 16, 14, 20), time
1001
+
1002
+ time = parse_now("20 seconds from now")
1003
+ assert_equal Time.local(2006, 8, 16, 14, 0, 20), time
1004
+
1005
+ time = Chronic.parse("2 months ago", :now => Time.parse("2007-03-07 23:30"))
1006
+ assert_equal Time.local(2007, 1, 7, 23, 30), time
1007
+
1008
+ # Two repeaters
1009
+ time = parse_now("25 minutes and 20 seconds from now")
1010
+ assert_equal Time.local(2006, 8, 16, 14, 25, 20), time
1011
+
1012
+ time = parse_now("24 hours and 20 minutes from now")
1013
+ assert_equal Time.local(2006, 8, 17, 14, 20, 0), time
1014
+
1015
+ time = parse_now("24 hours 20 minutes from now")
1016
+ assert_equal Time.local(2006, 8, 17, 14, 20, 0), time
1017
+ end
1018
+
1019
+ def test_parse_guess_p_s_r
1020
+ time = parse_now("in 3 hours")
1021
+ assert_equal Time.local(2006, 8, 16, 17), time
1022
+ end
1023
+
1024
+ def test_parse_guess_s_r_p_a
1025
+ # past
1026
+
1027
+ time = parse_now("3 years ago tomorrow")
1028
+ assert_equal Time.local(2003, 8, 17, 12), time
1029
+
1030
+ time = parse_now("3 years ago this friday")
1031
+ assert_equal Time.local(2003, 8, 18, 12), time
1032
+
1033
+ time = parse_now("3 months ago saturday at 5:00 pm")
1034
+ assert_equal Time.local(2006, 5, 19, 17), time
1035
+
1036
+ time = parse_now("2 days from this second")
1037
+ assert_equal Time.local(2006, 8, 18, 14), time
1038
+
1039
+ time = parse_now("7 hours before tomorrow at midnight")
1040
+ assert_equal Time.local(2006, 8, 17, 17), time
1041
+
1042
+ # future
1043
+ end
1044
+
1045
+ def test_parse_guess_rmn_s_r_p
1046
+ time = parse_now("september 3 years ago", :guess => :start)
1047
+ assert_equal Time.local(2003, 9), time
1048
+ end
1049
+
1050
+ def test_parse_guess_o_r_g_r
1051
+ time = parse_now("3rd month next year", :guess => false)
1052
+ assert_equal Time.local(2007, 3), time.begin
1053
+
1054
+ time = parse_now("3rd month next year", :guess => false)
1055
+ assert_equal Time.local(2007, 3, 1), time.begin
1056
+
1057
+ time = parse_now("3rd thursday this september")
1058
+ assert_equal Time.local(2006, 9, 21, 12), time
1059
+
1060
+ now = Time.parse("1/10/2010")
1061
+ time = parse_now("3rd thursday this november", :now => now)
1062
+ assert_equal Time.local(2010, 11, 18, 12), time
1063
+
1064
+ time = parse_now("4th day last week")
1065
+ assert_equal Time.local(2006, 8, 9, 12), time
1066
+ end
1067
+
1068
+ def test_parse_guess_nonsense
1069
+ time = parse_now("some stupid nonsense")
1070
+ assert_nil time
1071
+
1072
+ time = parse_now("Ham Sandwich")
1073
+ assert_nil time
1074
+
1075
+ time = parse_now("t")
1076
+ assert_nil time
1077
+ end
1078
+
1079
+ def test_parse_span
1080
+ span = parse_now("friday", :guess => false)
1081
+ assert_equal Time.local(2006, 8, 18), span.begin
1082
+ assert_equal Time.local(2006, 8, 19), span.end
1083
+
1084
+ span = parse_now("november", :guess => false)
1085
+ assert_equal Time.local(2006, 11), span.begin
1086
+ assert_equal Time.local(2006, 12), span.end
1087
+
1088
+ span = Chronic.parse("weekend" , :now => @time_2006_08_16_14_00_00, :guess => false)
1089
+ assert_equal Time.local(2006, 8, 19), span.begin
1090
+ assert_equal Time.local(2006, 8, 21), span.end
1091
+ end
1092
+
1093
+ def test_parse_with_endian_precedence
1094
+ date = '11/02/2007'
1095
+
1096
+ expect_for_middle_endian = Time.local(2007, 11, 2, 12)
1097
+ expect_for_little_endian = Time.local(2007, 2, 11, 12)
1098
+
1099
+ # default precedence should be toward middle endianness
1100
+ assert_equal expect_for_middle_endian, Chronic.parse(date)
1101
+
1102
+ assert_equal expect_for_middle_endian, Chronic.parse(date, :endian_precedence => [:middle, :little])
1103
+
1104
+ assert_equal expect_for_little_endian, Chronic.parse(date, :endian_precedence => [:little, :middle])
1105
+ end
1106
+
1107
+ def test_parse_words
1108
+ assert_equal parse_now("33 days from now"), parse_now("thirty-three days from now")
1109
+ 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")
1110
+ assert_equal parse_now("may 10th"), parse_now("may tenth")
1111
+ assert_equal parse_now("second monday in january"), parse_now("2nd monday in january")
1112
+ end
1113
+
1114
+ def test_relative_to_an_hour_before
1115
+ # example prenormalization "10 to 2" becomes "10 minutes past 2"
1116
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 to 2")
1117
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 till 2")
1118
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 prior to 2")
1119
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 before 2")
1120
+
1121
+ # uses the current hour, so 2006-08-16 13:50:00, not 14:50
1122
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 to")
1123
+ assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 till")
1124
+
1125
+ assert_equal Time.local(2006, 8, 16, 15, 45), parse_now("quarter to 4")
1126
+ end
1127
+
1128
+ def test_relative_to_an_hour_after
1129
+ # not nil
1130
+ assert_equal Time.local(2006, 8, 16, 14, 10), parse_now("10 after 2")
1131
+ assert_equal Time.local(2006, 8, 16, 14, 10), parse_now("10 past 2")
1132
+ assert_equal Time.local(2006, 8, 16, 14, 30), parse_now("half past 2")
1133
+ end
1134
+
1135
+ def test_parse_only_complete_pointers
1136
+ assert_equal parse_now("eat pasty buns today at 2pm"), @time_2006_08_16_14_00_00
1137
+ assert_equal parse_now("futuristically speaking today at 2pm"), @time_2006_08_16_14_00_00
1138
+ assert_equal parse_now("meeting today at 2pm"), @time_2006_08_16_14_00_00
1139
+ end
1140
+
1141
+ def test_am_pm
1142
+ assert_equal Time.local(2006, 8, 16), parse_now("8/16/2006 at 12am")
1143
+ assert_equal Time.local(2006, 8, 16, 12), parse_now("8/16/2006 at 12pm")
1144
+ end
1145
+
1146
+ def test_a_p
1147
+ assert_equal Time.local(2006, 8, 16, 0, 15), parse_now("8/16/2006 at 12:15a")
1148
+ assert_equal Time.local(2006, 8, 16, 18, 30), parse_now("8/16/2006 at 6:30p")
1149
+ end
1150
+
1151
+ def test_seasons
1152
+ t = parse_now("this spring", :guess => false)
1153
+ assert_equal Time.local(2007, 3, 20), t.begin
1154
+ assert_equal Time.local(2007, 6, 20), t.end
1155
+
1156
+ t = parse_now("this winter", :guess => false)
1157
+ assert_equal Time.local(2006, 12, 22), t.begin
1158
+ assert_equal Time.local(2007, 3, 19), t.end
1159
+
1160
+ t = parse_now("last spring", :guess => false)
1161
+ assert_equal Time.local(2006, 3, 20), t.begin
1162
+ assert_equal Time.local(2006, 6, 20), t.end
1163
+
1164
+ t = parse_now("last winter", :guess => false)
1165
+ assert_equal Time.local(2005, 12, 22), t.begin
1166
+ assert_equal Time.local(2006, 3, 19), t.end
1167
+
1168
+ t = parse_now("next spring", :guess => false)
1169
+ assert_equal Time.local(2007, 3, 20), t.begin
1170
+ assert_equal Time.local(2007, 6, 20), t.end
1171
+ end
1172
+
1173
+ def test_quarters
1174
+ time = parse_now("this quarter", :guess => false)
1175
+ assert_equal Time.local(2006, 7, 1), time.begin
1176
+ assert_equal Time.local(2006, 10, 1), time.end
1177
+
1178
+ time = parse_now("next quarter", :guess => false)
1179
+ assert_equal Time.local(2006, 10, 1), time.begin
1180
+ assert_equal Time.local(2007, 1, 1), time.end
1181
+
1182
+ time = parse_now("last quarter", :guess => false)
1183
+ assert_equal Time.local(2006, 4, 1), time.begin
1184
+ assert_equal Time.local(2006, 7, 1), time.end
1185
+ end
1186
+
1187
+ def test_quarters_srp
1188
+ time = parse_now("1 quarter ago", :guess => false)
1189
+ assert_equal Time.local(2006, 4, 1), time.begin
1190
+ assert_equal Time.local(2006, 7, 1), time.end
1191
+
1192
+ time = parse_now("2 quarters ago", :guess => false)
1193
+ assert_equal Time.local(2006, 1, 1), time.begin
1194
+ assert_equal Time.local(2006, 4, 1), time.end
1195
+
1196
+ time = parse_now("1 quarter from now", :guess => false)
1197
+ assert_equal Time.local(2006, 10, 1), time.begin
1198
+ assert_equal Time.local(2007, 1, 1), time.end
1199
+ end
1200
+
1201
+ def test_quarters_named
1202
+ ["Q1", "first quarter", "1st quarter"].each do |string|
1203
+ time = parse_now(string, :guess => false, :context => :none)
1204
+ assert_equal Time.local(2006, 1, 1), time.begin
1205
+ assert_equal Time.local(2006, 4, 1), time.end
1206
+
1207
+ time = parse_now(string, :guess => false, :context => :future)
1208
+ assert_equal Time.local(2007, 1, 1), time.begin
1209
+ assert_equal Time.local(2007, 4, 1), time.end
1210
+
1211
+ time = parse_now(string, :guess => false, :context => :past)
1212
+ assert_equal Time.local(2006, 1, 1), time.begin
1213
+ assert_equal Time.local(2006, 4, 1), time.end
1214
+
1215
+ time = parse_now("#{string} 2005", :guess => false)
1216
+ assert_equal Time.local(2005, 1, 1), time.begin
1217
+ assert_equal Time.local(2005, 4, 1), time.end
1218
+
1219
+ time = parse_now("2005 #{string}", :guess => false)
1220
+ assert_equal Time.local(2005, 1, 1), time.begin
1221
+ assert_equal Time.local(2005, 4, 1), time.end
1222
+
1223
+ time = parse_now("#{string} this year", :guess => false)
1224
+ assert_equal Time.local(2006, 1, 1), time.begin
1225
+ assert_equal Time.local(2006, 4, 1), time.end
1226
+
1227
+ time = parse_now("this year #{string}", :guess => false)
1228
+ assert_equal Time.local(2006, 1, 1), time.begin
1229
+ assert_equal Time.local(2006, 4, 1), time.end
1230
+
1231
+ time = parse_now("#{string} next year", :guess => false)
1232
+ assert_equal Time.local(2007, 1, 1), time.begin
1233
+ assert_equal Time.local(2007, 4, 1), time.end
1234
+
1235
+ time = parse_now("next year #{string}", :guess => false)
1236
+ assert_equal Time.local(2007, 1, 1), time.begin
1237
+ assert_equal Time.local(2007, 4, 1), time.end
1238
+
1239
+ time = parse_now("this #{string}", :guess => false, context: :none)
1240
+ assert_equal Time.local(2006, 1, 1), time.begin
1241
+ assert_equal Time.local(2006, 4, 1), time.end
1242
+
1243
+ time = parse_now("last #{string}", :guess => false, context: :none)
1244
+ assert_equal Time.local(2006, 1, 1), time.begin
1245
+ assert_equal Time.local(2006, 4, 1), time.end
1246
+
1247
+ time = parse_now("next #{string}", :guess => false, context: :none)
1248
+ assert_equal Time.local(2007, 1, 1), time.begin
1249
+ assert_equal Time.local(2007, 4, 1), time.end
1250
+ end
1251
+
1252
+ ["Q2", "second quarter", "2nd quarter"].each do |string|
1253
+ time = parse_now(string, :guess => false, :context => :none)
1254
+ assert_equal Time.local(2006, 4, 1), time.begin
1255
+ assert_equal Time.local(2006, 7, 1), time.end
1256
+
1257
+ time = parse_now(string, :guess => false, :context => :future)
1258
+ assert_equal Time.local(2007, 4, 1), time.begin
1259
+ assert_equal Time.local(2007, 7, 1), time.end
1260
+
1261
+ time = parse_now(string, :guess => false, :context => :past)
1262
+ assert_equal Time.local(2006, 4, 1), time.begin
1263
+ assert_equal Time.local(2006, 7, 1), time.end
1264
+
1265
+ time = parse_now("#{string} 2005", :guess => false)
1266
+ assert_equal Time.local(2005, 4, 1), time.begin
1267
+ assert_equal Time.local(2005, 7, 1), time.end
1268
+
1269
+ time = parse_now("2005 #{string}", :guess => false)
1270
+ assert_equal Time.local(2005, 4, 1), time.begin
1271
+ assert_equal Time.local(2005, 7, 1), time.end
1272
+
1273
+ time = parse_now("#{string} this year", :guess => false)
1274
+ assert_equal Time.local(2006, 4, 1), time.begin
1275
+ assert_equal Time.local(2006, 7, 1), time.end
1276
+
1277
+ time = parse_now("this year #{string}", :guess => false)
1278
+ assert_equal Time.local(2006, 4, 1), time.begin
1279
+ assert_equal Time.local(2006, 7, 1), time.end
1280
+
1281
+ time = parse_now("#{string} next year", :guess => false)
1282
+ assert_equal Time.local(2007, 4, 1), time.begin
1283
+ assert_equal Time.local(2007, 7, 1), time.end
1284
+
1285
+ time = parse_now("next year #{string}", :guess => false)
1286
+ assert_equal Time.local(2007, 4, 1), time.begin
1287
+ assert_equal Time.local(2007, 7, 1), time.end
1288
+
1289
+ time = parse_now("this #{string}", :guess => false, context: :none)
1290
+ assert_equal Time.local(2006, 4, 1), time.begin
1291
+ assert_equal Time.local(2006, 7, 1), time.end
1292
+
1293
+ time = parse_now("last #{string}", :guess => false, context: :none)
1294
+ assert_equal Time.local(2006, 4, 1), time.begin
1295
+ assert_equal Time.local(2006, 7, 1), time.end
1296
+
1297
+ time = parse_now("next #{string}", :guess => false, context: :none)
1298
+ assert_equal Time.local(2007, 4, 1), time.begin
1299
+ assert_equal Time.local(2007, 7, 1), time.end
1300
+ end
1301
+
1302
+ ["Q3", "third quarter", "3rd quarter"].each do |string|
1303
+ time = parse_now(string, :guess => false, :context => :none)
1304
+ assert_equal Time.local(2006, 7, 1), time.begin
1305
+ assert_equal Time.local(2006, 10, 1), time.end
1306
+
1307
+ time = parse_now(string, :guess => false, :context => :future)
1308
+ assert_equal Time.local(2007, 7, 1), time.begin
1309
+ assert_equal Time.local(2007, 10, 1), time.end
1310
+
1311
+ time = parse_now(string, :guess => false, :context => :past)
1312
+ assert_equal Time.local(2005, 7, 1), time.begin
1313
+ assert_equal Time.local(2005, 10, 1), time.end
1314
+
1315
+ time = parse_now("#{string} 2005", :guess => false)
1316
+ assert_equal Time.local(2005, 7, 1), time.begin
1317
+ assert_equal Time.local(2005, 10, 1), time.end
1318
+
1319
+ time = parse_now("2005 #{string}", :guess => false)
1320
+ assert_equal Time.local(2005, 7, 1), time.begin
1321
+ assert_equal Time.local(2005, 10, 1), time.end
1322
+
1323
+ time = parse_now("#{string} this year", :guess => false)
1324
+ assert_equal Time.local(2006, 7, 1), time.begin
1325
+ assert_equal Time.local(2006, 10, 1), time.end
1326
+
1327
+ time = parse_now("this year #{string}", :guess => false)
1328
+ assert_equal Time.local(2006, 7, 1), time.begin
1329
+ assert_equal Time.local(2006, 10, 1), time.end
1330
+
1331
+ time = parse_now("#{string} next year", :guess => false)
1332
+ assert_equal Time.local(2007, 7, 1), time.begin
1333
+ assert_equal Time.local(2007, 10, 1), time.end
1334
+
1335
+ time = parse_now("next year #{string}", :guess => false)
1336
+ assert_equal Time.local(2007, 7, 1), time.begin
1337
+ assert_equal Time.local(2007, 10, 1), time.end
1338
+
1339
+ time = parse_now("this #{string}", :guess => false, context: :none)
1340
+ assert_equal Time.local(2006, 7, 1), time.begin
1341
+ assert_equal Time.local(2006, 10, 1), time.end
1342
+
1343
+ time = parse_now("last #{string}", :guess => false, context: :none)
1344
+ assert_equal Time.local(2005, 7, 1), time.begin
1345
+ assert_equal Time.local(2005, 10, 1), time.end
1346
+
1347
+ time = parse_now("next #{string}", :guess => false, context: :none)
1348
+ assert_equal Time.local(2007, 7, 1), time.begin
1349
+ assert_equal Time.local(2007, 10, 1), time.end
1350
+ end
1351
+
1352
+ ["Q4", "fourth quarter", "4th quarter"].each do |string|
1353
+ time = parse_now(string, :guess => false, :context => :none)
1354
+ assert_equal Time.local(2006, 10, 1), time.begin
1355
+ assert_equal Time.local(2007, 1, 1), time.end
1356
+
1357
+ time = parse_now(string, :guess => false, :context => :future)
1358
+ assert_equal Time.local(2006, 10, 1), time.begin
1359
+ assert_equal Time.local(2007, 1, 1), time.end
1360
+
1361
+ time = parse_now(string, :guess => false, :context => :past)
1362
+ assert_equal Time.local(2005, 10, 1), time.begin
1363
+ assert_equal Time.local(2006, 1, 1), time.end
1364
+
1365
+ time = parse_now("#{string} 2005", :guess => false)
1366
+ assert_equal Time.local(2005, 10, 1), time.begin
1367
+ assert_equal Time.local(2006, 1, 1), time.end
1368
+
1369
+ time = parse_now("2005 #{string}", :guess => false)
1370
+ assert_equal Time.local(2005, 10, 1), time.begin
1371
+ assert_equal Time.local(2006, 1, 1), time.end
1372
+
1373
+ time = parse_now("#{string} this year", :guess => false)
1374
+ assert_equal Time.local(2006, 10, 1), time.begin
1375
+ assert_equal Time.local(2007, 1, 1), time.end
1376
+
1377
+ time = parse_now("this year #{string}", :guess => false)
1378
+ assert_equal Time.local(2006, 10, 1), time.begin
1379
+ assert_equal Time.local(2007, 1, 1), time.end
1380
+
1381
+ time = parse_now("#{string} next year", :guess => false)
1382
+ assert_equal Time.local(2007, 10, 1), time.begin
1383
+ assert_equal Time.local(2008, 1, 1), time.end
1384
+
1385
+ time = parse_now("next year #{string}", :guess => false)
1386
+ assert_equal Time.local(2007, 10, 1), time.begin
1387
+ assert_equal Time.local(2008, 1, 1), time.end
1388
+
1389
+ time = parse_now("this #{string}", :guess => false, context: :none)
1390
+ assert_equal Time.local(2006, 10, 1), time.begin
1391
+ assert_equal Time.local(2007, 1, 1), time.end
1392
+
1393
+ time = parse_now("last #{string}", :guess => false, context: :none)
1394
+ assert_equal Time.local(2005, 10, 1), time.begin
1395
+ assert_equal Time.local(2006, 1, 1), time.end
1396
+
1397
+ time = parse_now("next #{string}", :guess => false, context: :none)
1398
+ assert_equal Time.local(2006, 10, 1), time.begin
1399
+ assert_equal Time.local(2007, 1, 1), time.end
1400
+ end
1401
+ end
1402
+
1403
+ # regression
1404
+
1405
+ # def test_partial
1406
+ # assert_equal '', parse_now("2 hours")
1407
+ # end
1408
+
1409
+ def test_days_in_november
1410
+ t1 = Chronic.parse('1st thursday in november', :now => Time.local(2007))
1411
+ assert_equal Time.local(2007, 11, 1, 12), t1
1412
+
1413
+ t1 = Chronic.parse('1st friday in november', :now => Time.local(2007))
1414
+ assert_equal Time.local(2007, 11, 2, 12), t1
1415
+
1416
+ t1 = Chronic.parse('1st saturday in november', :now => Time.local(2007))
1417
+ assert_equal Time.local(2007, 11, 3, 12), t1
1418
+
1419
+ # t1 = Chronic.parse('1st sunday in november', :now => Time.local(2007))
1420
+ # assert_equal Time.local(2007, 11, 4, 12), t1
1421
+
1422
+ # Chronic.debug = true
1423
+ #
1424
+ # t1 = Chronic.parse('1st monday in november', :now => Time.local(2007))
1425
+ # assert_equal Time.local(2007, 11, 5, 11), t1
1426
+ end
1427
+
1428
+ def test_now_changes
1429
+ t1 = Chronic.parse("now")
1430
+ sleep 0.1
1431
+ t2 = Chronic.parse("now")
1432
+ refute_equal t1, t2
1433
+ end
1434
+
1435
+ def test_noon
1436
+ t1 = Chronic.parse('2011-01-01 at noon', :ambiguous_time_range => :none)
1437
+ assert_equal Time.local(2011, 1, 1, 12, 0), t1
1438
+ end
1439
+
1440
+ def test_handle_rdn_rmn_sd
1441
+ time = parse_now("Thu Aug 10")
1442
+ assert_equal Time.local(2006, 8, 10, 12), time
1443
+
1444
+ time = parse_now("Thursday July 31")
1445
+ assert_equal Time.local(2006, 7, 31, 12), time
1446
+
1447
+ time = parse_now("Thursday December 31")
1448
+ assert_equal Time.local(2006, 12, 31, 12), time
1449
+ end
1450
+
1451
+ def test_handle_rdn_rmn_sd_rt
1452
+ time = parse_now("Thu Aug 10 4pm")
1453
+ assert_equal Time.local(2006, 8, 10, 16), time
1454
+
1455
+ time = parse_now("Thu Aug 10 at 4pm")
1456
+ assert_equal Time.local(2006, 8, 10, 16), time
1457
+ end
1458
+
1459
+ def test_handle_rdn_rmn_od_rt
1460
+ time = parse_now("Thu Aug 10th at 4pm")
1461
+ assert_equal Time.local(2006, 8, 10, 16), time
1462
+ end
1463
+
1464
+ def test_handle_rdn_od_rt
1465
+ time = parse_now("Thu 17th at 4pm")
1466
+ assert_equal Time.local(2006, 8, 17, 16), time
1467
+
1468
+ time = parse_now("Thu 16th at 4pm")
1469
+ assert_equal Time.local(2006, 8, 16, 16), time
1470
+
1471
+ time = parse_now("Thu 1st at 4pm")
1472
+ assert_equal Time.local(2006, 9, 1, 16), time
1473
+
1474
+ time = parse_now("Thu 1st at 4pm", :context => :past)
1475
+ assert_equal Time.local(2006, 8, 1, 16), time
1476
+ end
1477
+
1478
+ def test_handle_rdn_od
1479
+ time = parse_now("Thu 17th")
1480
+ assert_equal Time.local(2006, 8, 17, 12), time
1481
+ end
1482
+
1483
+ def test_handle_rdn_rmn_sd_sy
1484
+ time = parse_now("Thu Aug 10 2006")
1485
+ assert_equal Time.local(2006, 8, 10, 12), time
1486
+
1487
+ time = parse_now("Thursday July 31 2006")
1488
+ assert_equal Time.local(2006, 7, 31, 12), time
1489
+
1490
+ time = parse_now("Thursday December 31 2006")
1491
+ assert_equal Time.local(2006, 12, 31, 12), time
1492
+
1493
+ time = parse_now("Thursday December 30 2006")
1494
+ assert_equal Time.local(2006, 12, 30, 12), time
1495
+ end
1496
+
1497
+ def test_handle_rdn_rmn_od
1498
+ time = parse_now("Thu Aug 10th")
1499
+ assert_equal Time.local(2006, 8, 10, 12), time
1500
+
1501
+ time = parse_now("Thursday July 31st")
1502
+ assert_equal Time.local(2006, 7, 31, 12), time
1503
+
1504
+ time = parse_now("Thursday December 31st")
1505
+ assert_equal Time.local(2006, 12, 31, 12), time
1506
+ end
1507
+
1508
+ def test_handle_rdn_rmn_od_sy
1509
+ time = parse_now("Thu Aug 10th 2005")
1510
+ assert_equal Time.local(2005, 8, 10, 12), time
1511
+
1512
+ time = parse_now("Thursday July 31st 2005")
1513
+ assert_equal Time.local(2005, 7, 31, 12), time
1514
+
1515
+ time = parse_now("Thursday December 31st 2005")
1516
+ assert_equal Time.local(2005, 12, 31, 12), time
1517
+
1518
+ time = parse_now("Thursday December 30th 2005")
1519
+ assert_equal Time.local(2005, 12, 30, 12), time
1520
+ end
1521
+
1522
+ def test_normalizing_day_portions
1523
+ assert_equal pre_normalize("8:00 pm February 11"), pre_normalize("8:00 p.m. February 11")
1524
+ end
1525
+
1526
+ def test_normalizing_time_of_day_phrases
1527
+ assert_equal pre_normalize("midday February 11"), pre_normalize("12:00 p.m. February 11")
1528
+ end
1529
+
1530
+ private
1531
+ def parse_now(string, options={})
1532
+ Chronic.parse(string, {:now => TIME_2006_08_16_14_00_00 }.merge(options))
1533
+ end
1534
+ def pre_normalize(s)
1535
+ Chronic::Parser.new.pre_normalize s
1536
+ end
1537
+ end