rizwanreza-chronic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/README.rdoc +188 -0
  2. data/lib/chronic.rb +57 -0
  3. data/lib/chronic/blunt.rb +234 -0
  4. data/lib/chronic/chronic.rb +326 -0
  5. data/lib/chronic/grabber.rb +26 -0
  6. data/lib/chronic/handlers.rb +549 -0
  7. data/lib/chronic/ordinal.rb +39 -0
  8. data/lib/chronic/pointer.rb +29 -0
  9. data/lib/chronic/repeater.rb +139 -0
  10. data/lib/chronic/repeaters/repeater_day.rb +52 -0
  11. data/lib/chronic/repeaters/repeater_day_name.rb +53 -0
  12. data/lib/chronic/repeaters/repeater_day_portion.rb +94 -0
  13. data/lib/chronic/repeaters/repeater_decade.rb +23 -0
  14. data/lib/chronic/repeaters/repeater_fortnight.rb +70 -0
  15. data/lib/chronic/repeaters/repeater_hour.rb +58 -0
  16. data/lib/chronic/repeaters/repeater_minute.rb +57 -0
  17. data/lib/chronic/repeaters/repeater_month.rb +66 -0
  18. data/lib/chronic/repeaters/repeater_month_name.rb +98 -0
  19. data/lib/chronic/repeaters/repeater_season.rb +150 -0
  20. data/lib/chronic/repeaters/repeater_season_name.rb +45 -0
  21. data/lib/chronic/repeaters/repeater_second.rb +41 -0
  22. data/lib/chronic/repeaters/repeater_time.rb +124 -0
  23. data/lib/chronic/repeaters/repeater_week.rb +73 -0
  24. data/lib/chronic/repeaters/repeater_weekday.rb +77 -0
  25. data/lib/chronic/repeaters/repeater_weekend.rb +65 -0
  26. data/lib/chronic/repeaters/repeater_year.rb +64 -0
  27. data/lib/chronic/scalar.rb +76 -0
  28. data/lib/chronic/separator.rb +91 -0
  29. data/lib/chronic/time_zone.rb +26 -0
  30. data/lib/core_ext/object.rb +7 -0
  31. data/lib/core_ext/time.rb +74 -0
  32. data/lib/numerizer/numerizer.rb +98 -0
  33. data/test/test_Chronic.rb +75 -0
  34. data/test/test_DaylightSavings.rb +119 -0
  35. data/test/test_Handler.rb +110 -0
  36. data/test/test_Numerizer.rb +54 -0
  37. data/test/test_RepeaterDayName.rb +52 -0
  38. data/test/test_RepeaterDecade.rb +46 -0
  39. data/test/test_RepeaterFortnight.rb +63 -0
  40. data/test/test_RepeaterHour.rb +68 -0
  41. data/test/test_RepeaterMinute.rb +35 -0
  42. data/test/test_RepeaterMonth.rb +47 -0
  43. data/test/test_RepeaterMonthName.rb +57 -0
  44. data/test/test_RepeaterSeason.rb +43 -0
  45. data/test/test_RepeaterTime.rb +72 -0
  46. data/test/test_RepeaterWeek.rb +63 -0
  47. data/test/test_RepeaterWeekday.rb +56 -0
  48. data/test/test_RepeaterWeekend.rb +75 -0
  49. data/test/test_RepeaterYear.rb +63 -0
  50. data/test/test_Span.rb +33 -0
  51. data/test/test_Time.rb +50 -0
  52. data/test/test_Token.rb +26 -0
  53. data/test/test_parsing.rb +809 -0
  54. metadata +118 -0
@@ -0,0 +1,75 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterWeekend < Test::Unit::TestCase
5
+
6
+ def setup
7
+ # Wed Aug 16 14:00:00 2006
8
+ @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
9
+ end
10
+
11
+ def test_next_future
12
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
13
+ weekend.start = @now
14
+
15
+ next_weekend = weekend.next(:future)
16
+ assert_equal Time.local(2006, 8, 19), next_weekend.begin
17
+ assert_equal Time.local(2006, 8, 21), next_weekend.end
18
+ end
19
+
20
+ def test_next_past
21
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
22
+ weekend.start = @now
23
+
24
+ next_weekend = weekend.next(:past)
25
+ assert_equal Time.local(2006, 8, 12), next_weekend.begin
26
+ assert_equal Time.local(2006, 8, 14), next_weekend.end
27
+ end
28
+
29
+ def test_this_future
30
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
31
+ weekend.start = @now
32
+
33
+ next_weekend = weekend.this(:future)
34
+ assert_equal Time.local(2006, 8, 19), next_weekend.begin
35
+ assert_equal Time.local(2006, 8, 21), next_weekend.end
36
+ end
37
+
38
+ def test_this_past
39
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
40
+ weekend.start = @now
41
+
42
+ next_weekend = weekend.this(:past)
43
+ assert_equal Time.local(2006, 8, 12), next_weekend.begin
44
+ assert_equal Time.local(2006, 8, 14), next_weekend.end
45
+ end
46
+
47
+ def test_this_none
48
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
49
+ weekend.start = @now
50
+
51
+ next_weekend = weekend.this(:future)
52
+ assert_equal Time.local(2006, 8, 19), next_weekend.begin
53
+ assert_equal Time.local(2006, 8, 21), next_weekend.end
54
+ end
55
+
56
+ def test_offset
57
+ span = Chronic::Span.new(@now, @now + 1)
58
+
59
+ offset_span = Chronic::RepeaterWeekend.new(:weekend).offset(span, 3, :future)
60
+
61
+ assert_equal Time.local(2006, 9, 2), offset_span.begin
62
+ assert_equal Time.local(2006, 9, 2, 0, 0, 1), offset_span.end
63
+
64
+ offset_span = Chronic::RepeaterWeekend.new(:weekend).offset(span, 1, :past)
65
+
66
+ assert_equal Time.local(2006, 8, 12), offset_span.begin
67
+ assert_equal Time.local(2006, 8, 12, 0, 0, 1), offset_span.end
68
+
69
+ offset_span = Chronic::RepeaterWeekend.new(:weekend).offset(span, 0, :future)
70
+
71
+ assert_equal Time.local(2006, 8, 12), offset_span.begin
72
+ assert_equal Time.local(2006, 8, 12, 0, 0, 1), offset_span.end
73
+ end
74
+
75
+ end
@@ -0,0 +1,63 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestRepeaterYear < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
8
+ end
9
+
10
+ def test_next_future
11
+ years = Chronic::RepeaterYear.new(:year)
12
+ years.start = @now
13
+
14
+ next_year = years.next(:future)
15
+ assert_equal Time.local(2007, 1, 1), next_year.begin
16
+ assert_equal Time.local(2008, 1, 1), next_year.end
17
+
18
+ next_next_year = years.next(:future)
19
+ assert_equal Time.local(2008, 1, 1), next_next_year.begin
20
+ assert_equal Time.local(2009, 1, 1), next_next_year.end
21
+ end
22
+
23
+ def test_next_past
24
+ years = Chronic::RepeaterYear.new(:year)
25
+ years.start = @now
26
+
27
+ last_year = years.next(:past)
28
+ assert_equal Time.local(2005, 1, 1), last_year.begin
29
+ assert_equal Time.local(2006, 1, 1), last_year.end
30
+
31
+ last_last_year = years.next(:past)
32
+ assert_equal Time.local(2004, 1, 1), last_last_year.begin
33
+ assert_equal Time.local(2005, 1, 1), last_last_year.end
34
+ end
35
+
36
+ def test_this
37
+ years = Chronic::RepeaterYear.new(:year)
38
+ years.start = @now
39
+
40
+ this_year = years.this(:future)
41
+ assert_equal Time.local(2006, 8, 17), this_year.begin
42
+ assert_equal Time.local(2007, 1, 1), this_year.end
43
+
44
+ this_year = years.this(:past)
45
+ assert_equal Time.local(2006, 1, 1), this_year.begin
46
+ assert_equal Time.local(2006, 8, 16), this_year.end
47
+ end
48
+
49
+ def test_offset
50
+ span = Chronic::Span.new(@now, @now + 1)
51
+
52
+ offset_span = Chronic::RepeaterYear.new(:year).offset(span, 3, :future)
53
+
54
+ assert_equal Time.local(2009, 8, 16, 14), offset_span.begin
55
+ assert_equal Time.local(2009, 8, 16, 14, 0, 1), offset_span.end
56
+
57
+ offset_span = Chronic::RepeaterYear.new(:year).offset(span, 10, :past)
58
+
59
+ assert_equal Time.local(1996, 8, 16, 14), offset_span.begin
60
+ assert_equal Time.local(1996, 8, 16, 14, 0, 1), offset_span.end
61
+ end
62
+
63
+ end
data/test/test_Span.rb ADDED
@@ -0,0 +1,33 @@
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
+ def test_span_exclusive
25
+ s = Chronic::Span.new(1, 4)
26
+ assert s.include?(3)
27
+ assert !s.include?(4)
28
+ t = Chronic::Span.new(Time.local(1980, 03, 01, 0), Time.local(1980, 04, 01, 0))
29
+ assert t.include?(Time.local(1980, 03, 31, 0))
30
+ assert !t.include?(Time.local(1980, 04, 01, 0))
31
+ end
32
+
33
+ end
data/test/test_Time.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestTime < Test::Unit::TestCase
5
+
6
+ def setup
7
+ end
8
+
9
+ def test_normal
10
+ assert_equal Time.local(2006, 1, 2, 0, 0, 0), Time.construct(2006, 1, 2, 0, 0, 0)
11
+ assert_equal Time.local(2006, 1, 2, 3, 0, 0), Time.construct(2006, 1, 2, 3, 0, 0)
12
+ assert_equal Time.local(2006, 1, 2, 3, 4, 0), Time.construct(2006, 1, 2, 3, 4, 0)
13
+ assert_equal Time.local(2006, 1, 2, 3, 4, 5), Time.construct(2006, 1, 2, 3, 4, 5)
14
+ end
15
+
16
+ def test_second_overflow
17
+ assert_equal Time.local(2006, 1, 1, 0, 1, 30), Time.construct(2006, 1, 1, 0, 0, 90)
18
+ assert_equal Time.local(2006, 1, 1, 0, 5, 0), Time.construct(2006, 1, 1, 0, 0, 300)
19
+ end
20
+
21
+ def test_minute_overflow
22
+ assert_equal Time.local(2006, 1, 1, 1, 30), Time.construct(2006, 1, 1, 0, 90)
23
+ assert_equal Time.local(2006, 1, 1, 5), Time.construct(2006, 1, 1, 0, 300)
24
+ end
25
+
26
+ def test_hour_overflow
27
+ assert_equal Time.local(2006, 1, 2, 12), Time.construct(2006, 1, 1, 36)
28
+ assert_equal Time.local(2006, 1, 7), Time.construct(2006, 1, 1, 144)
29
+ end
30
+
31
+ def test_day_overflow
32
+ assert_equal Time.local(2006, 2, 1), Time.construct(2006, 1, 32)
33
+ assert_equal Time.local(2006, 3, 5), Time.construct(2006, 2, 33)
34
+ assert_equal Time.local(2004, 3, 4), Time.construct(2004, 2, 33)
35
+ assert_equal Time.local(2000, 3, 5), Time.construct(2000, 2, 33)
36
+
37
+ assert_nothing_raised do
38
+ Time.construct(2006, 1, 56)
39
+ end
40
+
41
+ assert_raise(RuntimeError) do
42
+ Time.construct(2006, 1, 57)
43
+ end
44
+ end
45
+
46
+ def test_month_overflow
47
+ assert_equal Time.local(2006, 1), Time.construct(2005, 13)
48
+ assert_equal Time.local(2005, 12), Time.construct(2000, 72)
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestToken < Test::Unit::TestCase
5
+
6
+ def setup
7
+ # Wed Aug 16 14:00:00 UTC 2006
8
+ @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
9
+ end
10
+
11
+ def test_token
12
+ token = Chronic::Token.new('foo')
13
+ assert_equal 0, token.tags.size
14
+ assert !token.tagged?
15
+ token.tag("mytag")
16
+ assert_equal 1, token.tags.size
17
+ assert token.tagged?
18
+ assert_equal String, token.get_tag(String).class
19
+ token.tag(5)
20
+ assert_equal 2, token.tags.size
21
+ token.untag(String)
22
+ assert_equal 1, token.tags.size
23
+ assert_equal 'foo', token.word
24
+ end
25
+
26
+ end
@@ -0,0 +1,809 @@
1
+ require 'chronic'
2
+ require 'test/unit'
3
+
4
+ class TestParsing < Test::Unit::TestCase
5
+ # Wed Aug 16 14:00:00 UTC 2006
6
+ TIME_2006_08_16_14_00_00 = Time.local(2006, 8, 16, 14, 0, 0, 0)
7
+
8
+ def setup
9
+ @time_2006_08_16_14_00_00 = TIME_2006_08_16_14_00_00
10
+ end
11
+
12
+ def test_parse_guess_dates
13
+ # rm_sd
14
+
15
+ time = parse_now("may 27")
16
+ assert_equal Time.local(2007, 5, 27, 12), time
17
+
18
+ time = parse_now("may 28", :context => :past)
19
+ assert_equal Time.local(2006, 5, 28, 12), time
20
+
21
+ time = parse_now("may 28 5pm", :context => :past)
22
+ assert_equal Time.local(2006, 5, 28, 17), time
23
+
24
+ time = parse_now("may 28 at 5pm", :context => :past)
25
+ assert_equal Time.local(2006, 5, 28, 17), time
26
+
27
+ time = parse_now("may 28 at 5:32.19pm", :context => :past)
28
+ assert_equal Time.local(2006, 5, 28, 17, 32, 19), time
29
+
30
+ # rm_sd_on
31
+
32
+ time = parse_now("5pm on may 28")
33
+ assert_equal Time.local(2007, 5, 28, 17), time
34
+
35
+ time = parse_now("5pm may 28")
36
+ assert_equal Time.local(2007, 5, 28, 17), time
37
+
38
+ time = parse_now("5 on may 28", :ambiguous_time_range => :none)
39
+ assert_equal Time.local(2007, 5, 28, 05), time
40
+
41
+ # rm_od
42
+
43
+ time = parse_now("may 27th")
44
+ assert_equal Time.local(2007, 5, 27, 12), time
45
+
46
+ time = parse_now("may 27th", :context => :past)
47
+ assert_equal Time.local(2006, 5, 27, 12), time
48
+
49
+ time = parse_now("may 27th 5:00 pm", :context => :past)
50
+ assert_equal Time.local(2006, 5, 27, 17), time
51
+
52
+ time = parse_now("may 27th at 5pm", :context => :past)
53
+ assert_equal Time.local(2006, 5, 27, 17), time
54
+
55
+ time = parse_now("may 27th at 5", :ambiguous_time_range => :none)
56
+ assert_equal Time.local(2007, 5, 27, 5), time
57
+
58
+ # rm_od_sy
59
+
60
+ time = parse_now("may 27th 2007")
61
+ assert_equal Time.local(2007, 5, 27, 12), time
62
+
63
+ # rm_od_on
64
+
65
+ time = parse_now("5:00 pm may 27th", :context => :past)
66
+ assert_equal Time.local(2006, 5, 27, 17), time
67
+
68
+ time = parse_now("5pm on may 27th", :context => :past)
69
+ assert_equal Time.local(2006, 5, 27, 17), time
70
+
71
+ time = parse_now("5 on may 27th", :ambiguous_time_range => :none)
72
+ assert_equal Time.local(2007, 5, 27, 5), time
73
+
74
+ # rm_sy
75
+
76
+ time = parse_now("June 1979")
77
+ assert_equal Time.local(1979, 6, 16, 0), time
78
+
79
+ time = parse_now("dec 79")
80
+ assert_equal Time.local(1979, 12, 16, 12), time
81
+
82
+ # rm_sd_sy
83
+
84
+ time = parse_now("jan 3 2010")
85
+ assert_equal Time.local(2010, 1, 3, 12), time
86
+
87
+ time = parse_now("jan 3 2010 midnight")
88
+ assert_equal Time.local(2010, 1, 4, 0), time
89
+
90
+ time = parse_now("jan 3 2010 at midnight")
91
+ assert_equal Time.local(2010, 1, 4, 0), time
92
+
93
+ time = parse_now("jan 3 2010 at 4", :ambiguous_time_range => :none)
94
+ assert_equal Time.local(2010, 1, 3, 4), time
95
+
96
+ #time = parse_now("January 12, '00")
97
+ #assert_equal Time.local(2000, 1, 12, 12), time
98
+
99
+ time = parse_now("may 27, 1979")
100
+ assert_equal Time.local(1979, 5, 27, 12), time
101
+
102
+ time = parse_now("may 27 79")
103
+ assert_equal Time.local(1979, 5, 27, 12), time
104
+
105
+ time = parse_now("may 27 79 4:30")
106
+ assert_equal Time.local(1979, 5, 27, 16, 30), time
107
+
108
+ time = parse_now("may 27 79 at 4:30", :ambiguous_time_range => :none)
109
+ assert_equal Time.local(1979, 5, 27, 4, 30), time
110
+
111
+ # sd_rm_sy
112
+
113
+ time = parse_now("3 jan 2010")
114
+ assert_equal Time.local(2010, 1, 3, 12), time
115
+
116
+ time = parse_now("3 jan 2010 4pm")
117
+ assert_equal Time.local(2010, 1, 3, 16), time
118
+
119
+ time = parse_now("27 Oct 2006 7:30pm")
120
+ assert_equal Time.local(2006, 10, 27, 19, 30), time
121
+
122
+ # sm_sd_sy
123
+
124
+ time = parse_now("5/27/1979")
125
+ assert_equal Time.local(1979, 5, 27, 12), time
126
+
127
+ time = parse_now("5/27/1979 4am")
128
+ assert_equal Time.local(1979, 5, 27, 4), time
129
+
130
+ # sd_sm_sy
131
+
132
+ time = parse_now("27/5/1979")
133
+ assert_equal Time.local(1979, 5, 27, 12), time
134
+
135
+ time = parse_now("27/5/1979 @ 0700")
136
+ assert_equal Time.local(1979, 5, 27, 7), time
137
+
138
+ # sm_sy
139
+
140
+ time = parse_now("05/06")
141
+ assert_equal Time.local(2006, 5, 16, 12), time
142
+
143
+ time = parse_now("12/06")
144
+ assert_equal Time.local(2006, 12, 16, 12), time
145
+
146
+ time = parse_now("13/06")
147
+ assert_equal nil, time
148
+
149
+ # sy_sm_sd
150
+
151
+ time = parse_now("2000-1-1")
152
+ assert_equal Time.local(2000, 1, 1, 12), time
153
+
154
+ time = parse_now("2006-08-20")
155
+ assert_equal Time.local(2006, 8, 20, 12), time
156
+
157
+ time = parse_now("2006-08-20 7pm")
158
+ assert_equal Time.local(2006, 8, 20, 19), time
159
+
160
+ time = parse_now("2006-08-20 03:00")
161
+ assert_equal Time.local(2006, 8, 20, 3), time
162
+
163
+ time = parse_now("2006-08-20 03:30:30")
164
+ assert_equal Time.local(2006, 8, 20, 3, 30, 30), time
165
+
166
+ time = parse_now("2006-08-20 15:30:30")
167
+ assert_equal Time.local(2006, 8, 20, 15, 30, 30), time
168
+
169
+ time = parse_now("2006-08-20 15:30.30")
170
+ assert_equal Time.local(2006, 8, 20, 15, 30, 30), time
171
+
172
+ # rdn_rm_rd_rt_rtz_ry
173
+
174
+ time = parse_now("Mon Apr 02 17:00:00 PDT 2007")
175
+ assert_equal 1175558400, time.to_i
176
+
177
+ now = Time.now
178
+ time = parse_now(now.to_s)
179
+ assert_equal now.to_s, time.to_s
180
+
181
+ # rm_sd_rt
182
+
183
+ time = parse_now("jan 5 13:00")
184
+ assert_equal Time.local(2007, 1, 5, 13), time
185
+
186
+ time = parse_now("40 may")
187
+ assert_equal nil, time
188
+
189
+ time = parse_now("40 may 27")
190
+ assert_equal nil, time
191
+
192
+ time = parse_now("1800-08-20")
193
+ assert_equal Time.local(1800, 8, 20, 12), time
194
+ end
195
+
196
+ def test_parse_guess_r
197
+ time = parse_now("friday")
198
+ assert_equal Time.local(2006, 8, 18, 12), time
199
+
200
+ time = parse_now("tue")
201
+ assert_equal Time.local(2006, 8, 22, 12), time
202
+
203
+ time = parse_now("5")
204
+ assert_equal Time.local(2006, 8, 16, 17), time
205
+
206
+ time = Chronic.parse("5", :now => Time.local(2006, 8, 16, 3, 0, 0, 0), :ambiguous_time_range => :none)
207
+ assert_equal Time.local(2006, 8, 16, 5), time
208
+
209
+ time = parse_now("13:00")
210
+ assert_equal Time.local(2006, 8, 17, 13), time
211
+
212
+ time = parse_now("13:45")
213
+ assert_equal Time.local(2006, 8, 17, 13, 45), time
214
+
215
+ time = parse_now("november")
216
+ assert_equal Time.local(2006, 11, 16), time
217
+ end
218
+
219
+ def test_parse_guess_rr
220
+ time = parse_now("friday 13:00")
221
+ assert_equal Time.local(2006, 8, 18, 13), time
222
+
223
+ time = parse_now("monday 4:00")
224
+ assert_equal Time.local(2006, 8, 21, 16), time
225
+
226
+ time = parse_now("sat 4:00", :ambiguous_time_range => :none)
227
+ assert_equal Time.local(2006, 8, 19, 4), time
228
+
229
+ time = parse_now("sunday 4:20", :ambiguous_time_range => :none)
230
+ assert_equal Time.local(2006, 8, 20, 4, 20), time
231
+
232
+ time = parse_now("4 pm")
233
+ assert_equal Time.local(2006, 8, 16, 16), time
234
+
235
+ time = parse_now("4 am", :ambiguous_time_range => :none)
236
+ assert_equal Time.local(2006, 8, 16, 4), time
237
+
238
+ time = parse_now("12 pm")
239
+ assert_equal Time.local(2006, 8, 16, 12), time
240
+
241
+ time = parse_now("12:01 pm")
242
+ assert_equal Time.local(2006, 8, 16, 12, 1), time
243
+
244
+ time = parse_now("12:01 am")
245
+ assert_equal Time.local(2006, 8, 16, 0, 1), time
246
+
247
+ time = parse_now("12 am")
248
+ assert_equal Time.local(2006, 8, 16), time
249
+
250
+ time = parse_now("4:00 in the morning")
251
+ assert_equal Time.local(2006, 8, 16, 4), time
252
+
253
+ time = parse_now("november 4")
254
+ assert_equal Time.local(2006, 11, 4, 12), time
255
+
256
+ time = parse_now("aug 24")
257
+ assert_equal Time.local(2006, 8, 24, 12), time
258
+ end
259
+
260
+ def test_parse_guess_rrr
261
+ time = parse_now("friday 1 pm")
262
+ assert_equal Time.local(2006, 8, 18, 13), time
263
+
264
+ time = parse_now("friday 11 at night")
265
+ assert_equal Time.local(2006, 8, 18, 23), time
266
+
267
+ time = parse_now("friday 11 in the evening")
268
+ assert_equal Time.local(2006, 8, 18, 23), time
269
+
270
+ time = parse_now("sunday 6am")
271
+ assert_equal Time.local(2006, 8, 20, 6), time
272
+
273
+ time = parse_now("friday evening at 7")
274
+ assert_equal Time.local(2006, 8, 18, 19), time
275
+ end
276
+
277
+ def test_parse_guess_gr
278
+ # year
279
+
280
+ time = parse_now("this year")
281
+ assert_equal Time.local(2006, 10, 24, 12), time
282
+
283
+ time = parse_now("this year", :context => :past)
284
+ assert_equal Time.local(2006, 4, 24, 12), time
285
+
286
+ # month
287
+
288
+ time = parse_now("this month")
289
+ assert_equal Time.local(2006, 8, 24, 12), time
290
+
291
+ time = parse_now("this month", :context => :past)
292
+ assert_equal Time.local(2006, 8, 8, 12), time
293
+
294
+ time = Chronic.parse("next month", :now => Time.local(2006, 11, 15))
295
+ assert_equal Time.local(2006, 12, 16, 12), time
296
+
297
+ # month name
298
+
299
+ time = parse_now("last november")
300
+ assert_equal Time.local(2005, 11, 16), time
301
+
302
+ # fortnight
303
+
304
+ time = parse_now("this fortnight")
305
+ assert_equal Time.local(2006, 8, 21, 19, 30), time
306
+
307
+ time = parse_now("this fortnight", :context => :past)
308
+ assert_equal Time.local(2006, 8, 14, 19), time
309
+
310
+ # week
311
+
312
+ time = parse_now("this week")
313
+ assert_equal Time.local(2006, 8, 18, 7, 30), time
314
+
315
+ time = parse_now("this week", :context => :past)
316
+ assert_equal Time.local(2006, 8, 14, 19), time
317
+
318
+ # weekend
319
+
320
+ time = parse_now("this weekend")
321
+ assert_equal Time.local(2006, 8, 20), time
322
+
323
+ time = parse_now("this weekend", :context => :past)
324
+ assert_equal Time.local(2006, 8, 13), time
325
+
326
+ time = parse_now("last weekend")
327
+ assert_equal Time.local(2006, 8, 13), time
328
+
329
+ # day
330
+
331
+ time = parse_now("this day")
332
+ assert_equal Time.local(2006, 8, 16, 19, 30), time
333
+
334
+ time = parse_now("this day", :context => :past)
335
+ assert_equal Time.local(2006, 8, 16, 7), time
336
+
337
+ time = parse_now("today")
338
+ assert_equal Time.local(2006, 8, 16, 19, 30), time
339
+
340
+ time = parse_now("yesterday")
341
+ assert_equal Time.local(2006, 8, 15, 12), time
342
+
343
+ time = parse_now("tomorrow")
344
+ assert_equal Time.local(2006, 8, 17, 12), time
345
+
346
+ # day name
347
+
348
+ time = parse_now("this tuesday")
349
+ assert_equal Time.local(2006, 8, 22, 12), time
350
+
351
+ time = parse_now("next tuesday")
352
+ assert_equal Time.local(2006, 8, 22, 12), time
353
+
354
+ time = parse_now("last tuesday")
355
+ assert_equal Time.local(2006, 8, 15, 12), time
356
+
357
+ time = parse_now("this wed")
358
+ assert_equal Time.local(2006, 8, 23, 12), time
359
+
360
+ time = parse_now("next wed")
361
+ assert_equal Time.local(2006, 8, 23, 12), time
362
+
363
+ time = parse_now("last wed")
364
+ assert_equal Time.local(2006, 8, 9, 12), time
365
+
366
+ # day portion
367
+
368
+ time = parse_now("this morning")
369
+ assert_equal Time.local(2006, 8, 16, 9), time
370
+
371
+ time = parse_now("tonight")
372
+ assert_equal Time.local(2006, 8, 16, 22), time
373
+
374
+ # minute
375
+
376
+ time = parse_now("next minute")
377
+ assert_equal Time.local(2006, 8, 16, 14, 1, 30), time
378
+
379
+ # second
380
+
381
+ time = parse_now("this second")
382
+ assert_equal Time.local(2006, 8, 16, 14), time
383
+
384
+ time = parse_now("this second", :context => :past)
385
+ assert_equal Time.local(2006, 8, 16, 14), time
386
+
387
+ time = parse_now("next second")
388
+ assert_equal Time.local(2006, 8, 16, 14, 0, 1), time
389
+
390
+ time = parse_now("last second")
391
+ assert_equal Time.local(2006, 8, 16, 13, 59, 59), time
392
+ end
393
+
394
+ def test_parse_guess_grr
395
+ time = parse_now("yesterday at 4:00")
396
+ assert_equal Time.local(2006, 8, 15, 16), time
397
+
398
+ time = parse_now("today at 9:00")
399
+ assert_equal Time.local(2006, 8, 16, 9), time
400
+
401
+ time = parse_now("today at 2100")
402
+ assert_equal Time.local(2006, 8, 16, 21), time
403
+
404
+ time = parse_now("this day at 0900")
405
+ assert_equal Time.local(2006, 8, 16, 9), time
406
+
407
+ time = parse_now("tomorrow at 0900")
408
+ assert_equal Time.local(2006, 8, 17, 9), time
409
+
410
+ time = parse_now("yesterday at 4:00", :ambiguous_time_range => :none)
411
+ assert_equal Time.local(2006, 8, 15, 4), time
412
+
413
+ time = parse_now("last friday at 4:00")
414
+ assert_equal Time.local(2006, 8, 11, 16), time
415
+
416
+ time = parse_now("next wed 4:00")
417
+ assert_equal Time.local(2006, 8, 23, 16), time
418
+
419
+ time = parse_now("yesterday afternoon")
420
+ assert_equal Time.local(2006, 8, 15, 15), time
421
+
422
+ time = parse_now("last week tuesday")
423
+ assert_equal Time.local(2006, 8, 8, 12), time
424
+
425
+ time = parse_now("tonight at 7")
426
+ assert_equal Time.local(2006, 8, 16, 19), time
427
+
428
+ time = parse_now("tonight 7")
429
+ assert_equal Time.local(2006, 8, 16, 19), time
430
+
431
+ time = parse_now("7 tonight")
432
+ assert_equal Time.local(2006, 8, 16, 19), time
433
+ end
434
+
435
+ def test_parse_guess_grrr
436
+ time = parse_now("today at 6:00pm")
437
+ assert_equal Time.local(2006, 8, 16, 18), time
438
+
439
+ time = parse_now("today at 6:00am")
440
+ assert_equal Time.local(2006, 8, 16, 6), time
441
+
442
+ time = parse_now("this day 1800")
443
+ assert_equal Time.local(2006, 8, 16, 18), time
444
+
445
+ time = parse_now("yesterday at 4:00pm")
446
+ assert_equal Time.local(2006, 8, 15, 16), time
447
+
448
+ time = parse_now("tomorrow evening at 7")
449
+ assert_equal Time.local(2006, 8, 17, 19), time
450
+
451
+ time = parse_now("tomorrow morning at 5:30")
452
+ assert_equal Time.local(2006, 8, 17, 5, 30), time
453
+
454
+ time = parse_now("next monday at 12:01 am")
455
+ assert_equal Time.local(2006, 8, 21, 00, 1), time
456
+
457
+ time = parse_now("next monday at 12:01 pm")
458
+ assert_equal Time.local(2006, 8, 21, 12, 1), time
459
+ end
460
+
461
+ def test_parse_guess_rgr
462
+ time = parse_now("afternoon yesterday")
463
+ assert_equal Time.local(2006, 8, 15, 15), time
464
+
465
+ time = parse_now("tuesday last week")
466
+ assert_equal Time.local(2006, 8, 8, 12), time
467
+ end
468
+
469
+ def test_parse_guess_rrgr
470
+ time = parse_now("5pm tomorrow")
471
+ assert_equal Time.local(2006, 8, 17, 17), time
472
+ end
473
+
474
+ def test_parse_guess_s_r_p
475
+ # past
476
+
477
+ time = parse_now("3 years ago")
478
+ assert_equal Time.local(2003, 8, 16, 14), time
479
+
480
+ time = parse_now("1 month ago")
481
+ assert_equal Time.local(2006, 7, 16, 14), time
482
+
483
+ time = parse_now("1 fortnight ago")
484
+ assert_equal Time.local(2006, 8, 2, 14), time
485
+
486
+ time = parse_now("2 fortnights ago")
487
+ assert_equal Time.local(2006, 7, 19, 14), time
488
+
489
+ time = parse_now("3 weeks ago")
490
+ assert_equal Time.local(2006, 7, 26, 14), time
491
+
492
+ time = parse_now("2 weekends ago")
493
+ assert_equal Time.local(2006, 8, 5), time
494
+
495
+ time = parse_now("3 days ago")
496
+ assert_equal Time.local(2006, 8, 13, 14), time
497
+
498
+ #time = parse_now("1 monday ago")
499
+ #assert_equal Time.local(2006, 8, 14, 12), time
500
+
501
+ time = parse_now("5 mornings ago")
502
+ assert_equal Time.local(2006, 8, 12, 9), time
503
+
504
+ time = parse_now("7 hours ago")
505
+ assert_equal Time.local(2006, 8, 16, 7), time
506
+
507
+ time = parse_now("3 minutes ago")
508
+ assert_equal Time.local(2006, 8, 16, 13, 57), time
509
+
510
+ time = parse_now("20 seconds before now")
511
+ assert_equal Time.local(2006, 8, 16, 13, 59, 40), time
512
+
513
+ # future
514
+
515
+ time = parse_now("3 years from now")
516
+ assert_equal Time.local(2009, 8, 16, 14, 0, 0), time
517
+
518
+ time = parse_now("6 months hence")
519
+ assert_equal Time.local(2007, 2, 16, 14), time
520
+
521
+ time = parse_now("3 fortnights hence")
522
+ assert_equal Time.local(2006, 9, 27, 14), time
523
+
524
+ time = parse_now("1 week from now")
525
+ assert_equal Time.local(2006, 8, 23, 14, 0, 0), time
526
+
527
+ time = parse_now("1 weekend from now")
528
+ assert_equal Time.local(2006, 8, 19), time
529
+
530
+ time = parse_now("2 weekends from now")
531
+ assert_equal Time.local(2006, 8, 26), time
532
+
533
+ time = parse_now("1 day hence")
534
+ assert_equal Time.local(2006, 8, 17, 14), time
535
+
536
+ time = parse_now("5 mornings hence")
537
+ assert_equal Time.local(2006, 8, 21, 9), time
538
+
539
+ time = parse_now("1 hour from now")
540
+ assert_equal Time.local(2006, 8, 16, 15), time
541
+
542
+ time = parse_now("20 minutes hence")
543
+ assert_equal Time.local(2006, 8, 16, 14, 20), time
544
+
545
+ time = parse_now("20 seconds from now")
546
+ assert_equal Time.local(2006, 8, 16, 14, 0, 20), time
547
+
548
+ time = Chronic.parse("2 months ago", :now => Time.parse("2007-03-07 23:30"))
549
+ assert_equal Time.local(2007, 1, 7, 23, 30), time
550
+ end
551
+
552
+ def test_parse_guess_p_s_r
553
+ time = parse_now("in 3 hours")
554
+ assert_equal Time.local(2006, 8, 16, 17), time
555
+ end
556
+
557
+ def test_parse_guess_s_r_p_a
558
+ # past
559
+
560
+ time = parse_now("3 years ago tomorrow")
561
+ assert_equal Time.local(2003, 8, 17, 12), time
562
+
563
+ time = parse_now("3 years ago this friday")
564
+ assert_equal Time.local(2003, 8, 18, 12), time
565
+
566
+ time = parse_now("3 months ago saturday at 5:00 pm")
567
+ assert_equal Time.local(2006, 5, 19, 17), time
568
+
569
+ time = parse_now("2 days from this second")
570
+ assert_equal Time.local(2006, 8, 18, 14), time
571
+
572
+ #time = parse_now("7 hours before tomorrow at midnight")
573
+ #assert_equal Time.local(2006, 8, 17, 17), time
574
+
575
+ # future
576
+ end
577
+
578
+ def test_parse_guess_o_r_s_r
579
+ time = parse_now("3rd wednesday in november")
580
+ assert_equal Time.local(2006, 11, 15, 12), time
581
+
582
+ time = parse_now("10th wednesday in november")
583
+ assert_equal nil, time
584
+
585
+ # time = parse_now("3rd wednesday in 2007")
586
+ # assert_equal Time.local(2007, 1, 20, 12), time
587
+ end
588
+
589
+ def test_parse_guess_o_r_g_r
590
+ time = parse_now("3rd month next year")
591
+ assert_equal Time.local(2007, 3, 16, 12), time
592
+
593
+ time = parse_now("3rd thursday this september")
594
+ assert_equal Time.local(2006, 9, 21, 12), time
595
+
596
+ time = parse_now("4th day last week")
597
+ assert_equal Time.local(2006, 8, 9, 12), time
598
+ end
599
+
600
+ def test_parse_guess_nonsense
601
+ time = parse_now("some stupid nonsense")
602
+ assert_equal nil, time
603
+
604
+ time = parse_now("Ham Sandwich")
605
+ assert_equal nil, time
606
+ end
607
+
608
+ def test_parse_span
609
+ span = parse_now("friday", :guess => false)
610
+ assert_equal Time.local(2006, 8, 18), span.begin
611
+ assert_equal Time.local(2006, 8, 19), span.end
612
+
613
+ span = parse_now("november", :guess => false)
614
+ assert_equal Time.local(2006, 11), span.begin
615
+ assert_equal Time.local(2006, 12), span.end
616
+
617
+ span = Chronic.parse("weekend" , :now => @time_2006_08_16_14_00_00, :guess => false)
618
+ assert_equal Time.local(2006, 8, 19), span.begin
619
+ assert_equal Time.local(2006, 8, 21), span.end
620
+ end
621
+
622
+ def test_parse_with_endian_precedence
623
+ date = '11/02/2007'
624
+
625
+ expect_for_middle_endian = Time.local(2007, 11, 2, 12)
626
+ expect_for_little_endian = Time.local(2007, 2, 11, 12)
627
+
628
+ # default precedence should be toward middle endianness
629
+ assert_equal expect_for_middle_endian, Chronic.parse(date)
630
+
631
+ assert_equal expect_for_middle_endian, Chronic.parse(date, :endian_precedence => [:middle, :little])
632
+
633
+ assert_equal expect_for_little_endian, Chronic.parse(date, :endian_precedence => [:little, :middle])
634
+ end
635
+
636
+ def test_parse_words
637
+ assert_equal parse_now("33 days from now"), parse_now("thirty-three days from now")
638
+ 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")
639
+ assert_equal parse_now("may 10th"), parse_now("may tenth")
640
+ end
641
+
642
+ def test_parse_only_complete_pointers
643
+ assert_equal parse_now("eat pasty buns today at 2pm"), @time_2006_08_16_14_00_00
644
+ assert_equal parse_now("futuristically speaking today at 2pm"), @time_2006_08_16_14_00_00
645
+ assert_equal parse_now("meeting today at 2pm"), @time_2006_08_16_14_00_00
646
+ end
647
+
648
+ def test_parse_strip_tokens
649
+ assert_equal Chronic.strip_tokens("eat pasty buns today at 2pm"), "eat pasty buns"
650
+ assert_equal Chronic.strip_tokens("futuristically speaking today at 2pm"), "futuristically speaking"
651
+ assert_equal Chronic.strip_tokens("meeting today at 2pm"), "meeting"
652
+ end
653
+
654
+ def test_am_pm
655
+ assert_equal Time.local(2006, 8, 16), parse_now("8/16/2006 at 12am")
656
+ assert_equal Time.local(2006, 8, 16, 12), parse_now("8/16/2006 at 12pm")
657
+ end
658
+
659
+ def test_a_p
660
+ assert_equal Time.local(2006, 8, 16, 0, 15), parse_now("8/16/2006 at 12:15a")
661
+ assert_equal Time.local(2006, 8, 16, 18, 30), parse_now("8/16/2006 at 6:30p")
662
+ end
663
+
664
+ def test_argument_validation
665
+ assert_raise(Chronic::InvalidArgumentException) do
666
+ time = Chronic.parse("may 27", :foo => :bar)
667
+ end
668
+
669
+ assert_raise(Chronic::InvalidArgumentException) do
670
+ time = Chronic.parse("may 27", :context => :bar)
671
+ end
672
+ end
673
+
674
+ def test_seasons
675
+ t = parse_now("this spring", :guess => false)
676
+ assert_equal Time.local(2007, 3, 20), t.begin
677
+ assert_equal Time.local(2007, 6, 20), t.end
678
+
679
+ t = parse_now("this winter", :guess => false)
680
+ assert_equal Time.local(2006, 12, 22), t.begin
681
+ assert_equal Time.local(2007, 3, 19), t.end
682
+
683
+ t = parse_now("last spring", :guess => false)
684
+ assert_equal Time.local(2006, 3, 20), t.begin
685
+ assert_equal Time.local(2006, 6, 20), t.end
686
+
687
+ t = parse_now("last winter", :guess => false)
688
+ assert_equal Time.local(2005, 12, 22), t.begin
689
+ assert_equal Time.local(2006, 3, 19), t.end
690
+
691
+ t = parse_now("next spring", :guess => false)
692
+ assert_equal Time.local(2007, 3, 20), t.begin
693
+ assert_equal Time.local(2007, 6, 20), t.end
694
+ end
695
+
696
+ # regression
697
+
698
+ # def test_partial
699
+ # assert_equal '', parse_now("2 hours")
700
+ # end
701
+
702
+ def test_days_in_november
703
+ t1 = Chronic.parse('1st thursday in november', :now => Time.local(2007))
704
+ assert_equal Time.local(2007, 11, 1, 12), t1
705
+
706
+ t1 = Chronic.parse('1st friday in november', :now => Time.local(2007))
707
+ assert_equal Time.local(2007, 11, 2, 12), t1
708
+
709
+ t1 = Chronic.parse('1st saturday in november', :now => Time.local(2007))
710
+ assert_equal Time.local(2007, 11, 3, 12), t1
711
+
712
+ t1 = Chronic.parse('1st sunday in november', :now => Time.local(2007))
713
+ assert_equal Time.local(2007, 11, 4, 12), t1
714
+
715
+ # Chronic.debug = true
716
+ #
717
+ # t1 = Chronic.parse('1st monday in november', :now => Time.local(2007))
718
+ # assert_equal Time.local(2007, 11, 5, 11), t1
719
+ end
720
+
721
+ def test_parse_strip_tokens_preserved_case
722
+ assert_equal Chronic.strip_tokens("Ham Sandwich"), "Ham Sandwich"
723
+ assert_equal Chronic.strip_tokens("Eat a Ham Sandwich tomorrow"), "Eat a Ham Sandwich"
724
+ end
725
+
726
+ def test_parse_this_past
727
+ t = parse_now("this past tuesday")
728
+ assert_equal Time.local(2006,8,15, 12), t
729
+
730
+ t = parse_now("this past day")
731
+ assert_equal Time.local(2006,8,15, 12), t
732
+
733
+ t = parse_now("this past hour")
734
+ assert_equal Time.local(2006,8,16, 13, 30), t
735
+ end
736
+
737
+ def test_parse_noon
738
+ t = parse_now("noon")
739
+ assert_equal Time.local(2006,8,16, 12), t
740
+
741
+ t = parse_now("tomorrow at noon")
742
+ assert_equal Time.local(2006,8,17, 12), t
743
+ end
744
+
745
+ def test_parse_before_now
746
+ t = parse_now("3 hours before now")
747
+ assert_equal Time.local(2006,8,16, 11), t
748
+
749
+ t = parse_now("3 days before now")
750
+ assert_equal Time.local(2006,8,13, 14), t
751
+
752
+ t = parse_now("30 minutes before now")
753
+ assert_equal Time.local(2006,8,16, 13,30), t
754
+ end
755
+
756
+ def test_now
757
+ t = parse_now("now")
758
+ assert_equal Time.local(2006,8,16,14), t
759
+
760
+ t = parse_now("1 hour from now")
761
+ assert_equal Time.local(2006,8,16,15), t
762
+
763
+ t = parse_now("1 hour before now")
764
+ assert_equal Time.local(2006,8,16,13), t
765
+ end
766
+
767
+ def test_this_last
768
+ t = parse_now("this last day")
769
+ assert_equal Time.local(2006, 8, 15, 12), t
770
+
771
+ t = parse_now("this last hour")
772
+ assert_equal Time.local(2006, 8, 16, 13, 30), t
773
+ end
774
+
775
+ def test_hr_and_hrs
776
+ t = parse_now("in 3 hr")
777
+ assert_equal Time.local(2006, 8,16,17), t
778
+
779
+ t = parse_now("in 3 hrs")
780
+ assert_equal Time.local(2006, 8,16,17), t
781
+ end
782
+
783
+ def test_fractional_times
784
+ t = parse_now("in three and a half hours")
785
+ assert_equal Time.local(2006, 8,16,17, 30), t
786
+
787
+ t = parse_now("in 3.5 hours")
788
+ assert_equal Time.local(2006, 8,16,17, 30), t
789
+ end
790
+
791
+ def test_fry_parsed_as_fri
792
+ # the word "fry" was parsed as "fri"
793
+ assert_nil(Chronic.parse("Fry"))
794
+ assert_not_nil(Chronic.parse("Friday"))
795
+ end
796
+
797
+ def test_date_with_commas
798
+ t = parse_now("Today, 9pm")
799
+ assert_equal(Time.local(2006, 8, 16, 21), t)
800
+
801
+ t = parse_now("Meeting, Today, 9pm")
802
+ assert_equal(Time.local(2006, 8, 16, 21), t)
803
+ end
804
+
805
+ private
806
+ def parse_now(string, options={})
807
+ Chronic.parse(string, {:now => TIME_2006_08_16_14_00_00 }.merge(options))
808
+ end
809
+ end