public
Fork of mojombo/chronic
Description: "Chronic is a pure Ruby natural language date parser." + improvements, corrections, speedups, and additions
Homepage: http://chronic.rubyforge.org
Clone URL: git://github.com/jf/chronic.git
Search Repo:
added pre-existing codebase
mojombo (author)
Mon Sep 04 11:43:02 -0700 2006
commit  da3a121b4b0d2764f16401a51db44f3573df331a
tree    34291fce6d5b2401011507abbab64ddc2fe98837
parent  1e3824339762bd48316fe87bfafc853732d43264
0
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
0
@@ -1 +1,119 @@
0
+# rdoc --exclude test --exclude benchmark --main README lib README
0
+
0
+=Chronic
0
+
0
+Chronic is a natural language date/time parser written in pure Ruby. See below for the wide variety of formats Chronic will parse.
0
+
0
+==Installation
0
+
0
+Chronic can be installed via RubyGems:
0
+
0
+ $ sudo gem install chronic
0
+
0
+==Usage
0
+
0
+You can parse strings containing a natural language date using the Chronic::parse method.
0
+
0
+ require 'chronic'
0
+
0
+ Time.now
0
+ #=> Sun Aug 27 23:18:25 PDT 2006
0
+
0
+ Chronic.parse('tomorrow')
0
+ #=> Mon Aug 28 12:00:00 PDT 2006
0
+
0
+ Chronic.parse('monday', :context => :past)
0
+ #=> Mon Aug 21 12:00:00 PDT 2006
0
+
0
+ Chronic.parse('this tuesday 5:00')
0
+ #=> Tue Aug 29 17:00:00 PDT 2006
0
+
0
+ Chronic.parse('this tuesday 5:00', :ambiguous_time_range => :none)
0
+ #=> Tue Aug 29 05:00:00 PDT 2006
0
+
0
+ Chronic.parse('may 27th', :now => Time.local(2000, 1, 1))
0
+ #=> Sat May 27 12:00:00 PDT 2000
0
+
0
+ Chronic.parse('may 27th', :guess => false)
0
+ #=> Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007
0
+
0
+Chronic can parse an extremely wide variety of date/time formats. Following is a small sample of strings that will be properly parsed.
0
+
0
+Simple
0
+
0
+ thursday
0
+ november
0
+ summer
0
+ friday 13:00
0
+ mon 2:35
0
+ 4pm
0
+ 6 in the morning
0
+ friday 1pm
0
+ sat 7 in the evening
0
+ yesterday
0
+ today
0
+ tomorrow
0
+ this tuesday
0
+ next month
0
+ last winter
0
+ this morning
0
+ last night
0
+ this second
0
+ yesterday at 4:00
0
+ last friday at 20:00
0
+ last week tuesday
0
+ tomorrow at 6:45pm
0
+ afternoon yesterday
0
+ thursday last week
0
+
0
+Complex
0
+
0
+ 3 years ago
0
+ 5 months before now
0
+ 7 hours ago
0
+ 7 days from now
0
+ 1 week hence
0
+ in 3 hours
0
+ 1 year ago tomorrow
0
+ 3 months ago saturday at 5:00 pm
0
+ 7 hours before tomorrow at noon
0
+ 3rd wednesday in november
0
+ 3rd month next year
0
+ 3rd thursday this september
0
+ 4th day last week
0
+
0
+Specific Dates
0
+
0
+ January 5
0
+ dec 25
0
+ may 27th
0
+ October 2006
0
+ oct 06
0
+ jan 3 2010
0
+ february 14, 2004
0
+ 3 jan 2000
0
+ 17 april 85
0
+ 5/27/1979
0
+ 27/5/1979
0
+ 05/06
0
+ 1979-05-27
0
+ Friday
0
+ 5
0
+ 4:00
0
+ 17:00
0
+ 0800
0
+
0
+Specific Times (many of the above with an added time)
0
+
0
+ January 5 at 7pm
0
+ 1979-05-27 05:00
0
+ etc
0
+
0
+==Options
0
+
0
+==Limitations
0
+
0
+Chronic uses Ruby's built in Time class for all time storage and computation. Because of this, only times that the Time class can handle will be properly parsed. Parsing for times outside of this range will simply return nil. Support for a wider range of times is planned for a future release.
0
+
0
+Time zones other than the local one are not currently supported. Support for other time zones is planned for a future release.
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -1 +1,16 @@
0
+require 'chronic'
0
+require 'benchmark'
0
+
0
+print "jan 3 2010: "
0
+puts Benchmark.measure { Chronic.parse("jan 3 2010") }
0
+
0
+print "7 hours before tomorrow at midnight: "
0
+puts Benchmark.measure { Chronic.parse("7 hours before tomorrow at midnight") }
0
+
0
+
0
+
0
+# n = 100
0
+# Benchmark.bm(14) do |x|
0
+# x.report("jan 3 2010:") { for i in 1..n; Chronic.parse("jan 3 2010"); end }
0
+# end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0
@@ -1 +1,32 @@
0
+require 'chronic/base'
0
+
0
+require 'chronic/repeater'
0
+require 'chronic/repeater_year'
0
+require 'chronic/repeater_season'
0
+require 'chronic/repeater_season_name'
0
+require 'chronic/repeater_month'
0
+require 'chronic/repeater_month_name'
0
+require 'chronic/repeater_fortnight'
0
+require 'chronic/repeater_week'
0
+require 'chronic/repeater_weekend'
0
+require 'chronic/repeater_day'
0
+require 'chronic/repeater_day_name'
0
+require 'chronic/repeater_day_portion'
0
+require 'chronic/repeater_hour'
0
+require 'chronic/repeater_minute'
0
+require 'chronic/repeater_second'
0
+require 'chronic/repeater_time'
0
+
0
+require 'chronic/grabber'
0
+require 'chronic/pointer'
0
+require 'chronic/scalar'
0
+require 'chronic/ordinal'
0
+require 'chronic/construct'
0
+require 'chronic/separator'
0
+
0
+require 'ruby-debug'
0
+
0
+module Chronic
0
+ @@debug = true
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
0
@@ -1 +1,245 @@
0
+require 'date'
0
+
0
+# Chronic is a natural language date/time parser in pure Ruby.
0
+module Chronic
0
+
0
+ class << self
0
+
0
+ # Parses a string containing a natural language date/time. If Chronic
0
+ # can find a date/time, either a Time or Span will be returned
0
+ # (depending on the value of :guess). If no date/time can be found,
0
+ # nil will be returned.
0
+ #
0
+ # Options are:
0
+ #
0
+ # [<tt>:context</tt>]
0
+ # <tt>:past</tt> or <tt>:future</tt> (defaults to <tt>:future</tt>)
0
+ #
0
+ # If your string represents a birthday, you can set <tt>:context</tt> to <tt>:past</tt>
0
+ # and if an ambiguous string is given, it will assume it is in the
0
+ # past.
0
+ #
0
+ # [<tt>:now</tt>]
0
+ # Time (defaults to Time.now)
0
+ #
0
+ # By setting :now to a Time, all computations will be based off
0
+ # of that time instead of Time.now
0
+ #
0
+ # [<tt>:guess</tt>]
0
+ # true or false (defaults to true)
0
+ #
0
+ # By default, <tt>Chronic::parse</tt> will guess a single point in time for the
0
+ # given date/time. If you'd rather have the entire Span returned,
0
+ # set <tt>:guess</tt> to false.
0
+ #
0
+ # [<tt>:ambiguous_time_range</tt>]
0
+ # Integer or <tt>:none</tt> (defaults to 6 (6am-6pm))
0
+ #
0
+ # If an Integer is given, ambiguous times (like 5:00) will be
0
+ # assumed to be within the range of that time in the AM to that time
0
+ # in the PM. For example, if you set it to 7, then the parser will
0
+ # look for the time between 7am and 7pm. In the case of 5:00, it would
0
+ # assume that means 5:00pm. If <tt>:none</tt> is given, no assumption
0
+ # will be made, and the first matching instance of that time will
0
+ # be used.
0
+ def parse(text, specified_options = {})
0
+ # get options and set defaults if necessary
0
+ default_options = {:context => :future,
0
+ :now => Time.now,
0
+ :guess => true,
0
+ :ambiguous_time_range => 6}
0
+ options = default_options.merge specified_options
0
+
0
+ # ensure the specified options are valid
0
+ specified_options.keys.each do |key|
0
+ default_options.keys.include?(key) || raise(InvalidArgumentException, "#{key} is not a valid option key.")
0
+ end
0
+ [:past, :future].include?(options[:context]) || raise(InvalidArgumentException, "Invalid value '#{options[:context]}' for :context specified. Valid values are :past and :future.")
0
+
0
+ # store now for later =)
0
+ @now = options[:now]
0
+
0
+ # put the text into a normal format to ease scanning
0
+ text = self.pre_normalize(text)
0
+
0
+ puts text
0
+
0
+ # get base tokens for each word
0
+ @tokens = self.base_tokenize(text)
0
+
0
+ # scan the tokens with each token scanner
0
+ [Repeater].each do |tokenizer|
0
+ @tokens = tokenizer.scan(@tokens, options)
0
+ end
0
+
0
+ [Grabber, Pointer, Scalar, Ordinal, Separator].each do |tokenizer|
0
+ @tokens = tokenizer.scan(@tokens)
0
+ end
0
+
0
+ # strip any non-tagged tokens
0
+ @tokens = @tokens.select { |token| token.tagged? }
0
+
0
+ if @@debug
0
+ puts "+---------------------------------------------------"
0
+ puts "| " + @tokens.to_s
0
+ puts "+---------------------------------------------------"
0
+ end
0
+
0
+ # do the heavy lifting
0
+ span = self.tokens_to_span(@tokens, options)
0
+
0
+ # guess a time within a span if required
0
+ if options[:guess]
0
+ return self.guess(span)
0
+ else
0
+ return span
0
+ end
0
+ end
0
+
0
+ # Clean up the specified input text by stripping unwanted characters,
0
+ # converting idioms to their canonical form, converting number words
0
+ # to numbers (three => 3), and converting ordinal words to numeric
0
+ # ordinals (third => 3rd)
0
+ def pre_normalize(text) #:nodoc:
0
+ normalized_text = text.downcase
0
+ normalized_text.gsub!(/['"\.]/, '')
0
+ normalized_text.gsub!(/([\/\-\,\@])/) { ' ' + $1 + ' ' }
0
+ normalized_text.gsub!(/\btoday\b/, 'this day')
0
+ normalized_text.gsub!(/\btomm?orr?ow\b/, 'next day')
0
+ normalized_text.gsub!(/\byesterday\b/, 'last day')
0
+ normalized_text.gsub!(/\bnoon\b/, '12:00')
0
+ normalized_text.gsub!(/\bmidnight\b/, '24:00')
0
+ normalized_text.gsub!(/\bfrom now\b/, 'future')
0
+ normalized_text.gsub!(/\bbefore now\b/, 'past')
0
+ normalized_text.gsub!(/\bnow\b/, 'this hour')
0
+ normalized_text.gsub!(/\b(ago|before)\b/, 'past')
0
+ normalized_text.gsub!(/\bthis past\b/, 'last')
0
+ normalized_text.gsub!(/\bthis last\b/, 'last')
0
+ normalized_text.gsub!(/\b(?:in|during) the (morning)\b/, '\1')
0
+ normalized_text.gsub!(/\b(?:in the|during the|at) (afternoon|evening|night)\b/, '\1')
0
+ normalized_text.gsub!(/\btonight\b/, 'this night')
0
+ normalized_text.gsub!(/(?=\w)([ap]m|oclock)\b/, ' \1')
0
+ normalized_text.gsub!(/\b(hence|after|from)\b/, 'future')
0
+ normalized_text.gsub!(/\ba\b/, '1')
0
+ normalized_text.gsub!(/\s+/, ' ')
0
+ normalized_text = numericize_numbers(normalized_text)
0
+ normalized_text = numericize_ordinals(normalized_text)
0
+ end
0
+
0
+ # Convert number words to numbers (three => 3)
0
+ def numericize_numbers(text) #:nodoc:
0
+ text
0
+ end
0
+
0
+ # Convert ordinal words to numeric ordinals (third => 3rd)
0
+ def numericize_ordinals(text) #:nodoc:
0
+ text
0
+ end
0
+
0
+ # Split the text on spaces and convert each word into
0
+ # a Token
0
+ def base_tokenize(text) #:nodoc:
0
+ text.split(' ').map { |word| Token.new(word) }
0
+ end
0
+
0
+ # Guess a specific time within the given span
0
+ def guess(span) #:nodoc:
0
+ return nil if span.nil?
0
+ if span.width > 1
0
+ span.begin + (span.width / 2)
0
+ else
0
+ span.begin
0
+ end
0
+ end
0
+ end
0
+
0
+ class Token #:nodoc:
0
+ attr_accessor :word, :tags
0
+
0
+ def initialize(word)
0
+ @word = word
0
+ @tags = []
0
+ end
0
+
0
+ # Tag this token with the specified tag
0
+ def tag(new_tag)
0
+ @tags << new_tag
0
+ end
0
+
0
+ # Remove all tags of the given class
0
+ def untag(tag_class)
0
+ @tags = @tags.select { |m| !m.kind_of? tag_class }
0
+ end
0
+
0
+ # Return true if this token has any tags
0
+ def tagged?
0
+ @tags.size > 0
0
+ end
0
+
0
+ # Return the Tag that matches the given class
0
+ def get_tag(tag_class)
0
+ matches = @tags.select { |m| m.kind_of? tag_class }
0
+ #matches.size < 2 || raise("Multiple identical tags found")
0
+ return matches.first
0
+ end
0
+
0
+ # Print this Token in a pretty way
0
+ def to_s
0
+ @word << '(' << @tags.join(', ') << ') '
0
+ end
0
+ end
0
+
0
+ # A Span represents a range of time. Since this class extends
0
+ # Range, you can use #begin and #end to get the beginning and
0
+ # ending times of the span (they will be of class Time)
0
+ class Span < Range
0
+ # Returns the width of this span in seconds
0
+ def width
0
+ (self.end - self.begin).to_i
0
+ end
0
+
0
+ # Add a number of seconds to this span, returning the
0
+ # resulting Span
0
+ def +(seconds)
0
+ Span.new(self.begin + seconds, self.end + seconds)
0
+ end
0
+
0
+ # Subtract a number of seconds to this span, returning the
0
+ # resulting Span
0
+ def -(seconds)
0
+ self + -seconds
0
+ end
0
+
0
+ # Prints this span in a nice fashion
0
+ def to_s
0
+ '(' << self.begin.to_s << '..' << self.end.to_s << ')'
0
+ end
0
+ end
0
+
0
+ # Tokens are tagged with subclassed instances of this class when
0
+ # they match specific criteria
0
+ class Tag #:nodoc:
0
+ attr_accessor :type
0
+
0
+ def initialize(type)
0
+ @type = type
0
+ end
0
+
0
+ def start=(s)
0
+ @now = s
0
+ end
0
+ end
0
+
0
+ # Internal exception
0
+ class ChronicPain < Exception #:nodoc:
0
+
0
+ end
0
+
0
+ # This exception is raised if an invalid argument is provided to
0
+ # any of Chronic's methods
0
+ class InvalidArgumentException < Exception
0
+
0
+ end
0
+
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
0
@@ -1 +1,414 @@
0
+module Chronic
0
+
0
+  class << self
0
+  
0
+   def definitions #:nodoc:
0
+   @definitions ||=
0
+ {:time => [Handler.new([:repeater_time, :repeater_day_portion?], nil)],
0
+
0
+ :date => [Handler.new([:repeater_month_name, :scalar_day, :scalar_year], :handle_rmn_sd_sy),
0
+ Handler.new([:repeater_month_name, :scalar_day, :scalar_year, :separator_at?, 'time?'], :handle_rmn_sd_sy),
0
+ Handler.new([:repeater_month_name, :scalar_day, :separator_at?, 'time?'], :handle_rmn_sd),
0
+ Handler.new([:repeater_month_name, :ordinal_day, :separator_at?, 'time?'], :handle_rmn_od),
0
+ Handler.new([:repeater_month_name, :scalar_year], :handle_rmn_sy),
0
+ Handler.new([:scalar_day, :repeater_month_name, :scalar_year, :separator_at?, 'time?'], :handle_sd_rmn_sy),
0
+ Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
0
+ Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy),
0
+ Handler.new([:scalar_year, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_day, :separator_at?, 'time?'], :handle_sy_sm_sd),
0
+ Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_year], :handle_sm_sy)],
0
+
0
+ :anchor => [Handler.new([:grabber?, :repeater, :separator_at?, :repeater?, :repeater?], :handle_r),
0
+ Handler.new([:repeater, :grabber, :repeater], :handle_r_g_r)],
0
+
0
+ :arrow => [Handler.new([:scalar, :repeater, :pointer], :handle_s_r_p),
0
+ Handler.new([:pointer, :scalar, :repeater], :handle_p_s_r),
0
+ Handler.new([:scalar, :repeater, :pointer, 'anchor'], :handle_s_r_p_a)],
0
+
0
+ :narrow => [Handler.new([:ordinal, :repeater, :separator_in, :repeater], :handle_o_r_s_r),
0
+ Handler.new([:ordinal, :repeater, :grabber, :repeater], :handle_o_r_g_r)]
0
+ }
0
+ end
0
+
0
+ def tokens_to_span(tokens, options) #:nodoc:
0
+ # maybe it's a specific date
0
+
0
+ self.definitions[:date].each do |handler|
0
+ if handler.match(tokens, self.definitions)
0
+ puts handler.handler_method
0
+ good_tokens = tokens.select { |o| !o.get_tag Separator }
0
+ return self.send(handler.handler_method, good_tokens, options)
0
+ end
0
+ end
0
+
0
+ # I guess it's not a specific date, maybe it's just an anchor
0
+
0
+ self.definitions[:anchor].each do |handler|
0
+ if handler.match(tokens, self.definitions)
0
+ good_tokens = tokens.select { |o| !o.get_tag Separator }
0
+ return self.send(handler.handler_method, good_tokens, options)
0
+ end
0
+ end
0
+
0
+ # not an anchor, perhaps it's an arrow
0
+
0
+ self.definitions[:arrow].each do |handler|
0
+ if handler.match(tokens, self.definitions)
0
+ good_tokens = tokens.reject { |o| o.get_tag(SeparatorAt) || o.get_tag(SeparatorSlashOrDash) || o.get_tag(SeparatorComma) }
0
+ return self.send(handler.handler_method, good_tokens, options)
0
+ end
0
+ end
0
+
0
+ # not an arrow, let's hope it's an narrow
0
+
0
+ self.definitions[:narrow].each do |handler|
0
+ if handler.match(tokens, self.definitions)
0
+ #good_tokens = tokens.select { |o| !o.get_tag Separator }
0
+ return self.send(handler.handler_method, tokens, options)
0
+ end
0
+ end
0
+
0
+ # I guess you're out of luck!
0
+ return nil
0
+ end
0
+
0
+ #--------------
0
+
0
+ def day_or_time(day_start, time_tokens, options)
0
+ outer_span = Span.new(day_start, day_start + (24 * 60 * 60))
0
+
0
+ if !time_tokens.empty?
0
+ @now = outer_span.begin
0
+ time = get_anchor(dealias_and_disambiguate_times(time_tokens, options), options)
0
+ return time
0
+ else
0
+ return outer_span
0
+ end
0
+ end
0
+
0
+ #--------------
0
+
0
+ def handle_m_d(month, day, time_tokens, options) #:nodoc:
0
+ month.start = @now
0
+ span = month.next(options[:context])
0
+
0
+ day_start = Time.local(span.begin.year, span.begin.month, day)
0
+
0
+ day_or_time(day_start, time_tokens, options)
0
+ end
0
+
0
+ def handle_rmn_sd(tokens, options) #:nodoc:
0
+ handle_m_d(tokens[0].get_tag(RepeaterMonthName), tokens[1].get_tag(ScalarDay).type, tokens[2..tokens.size], options)
0
+ end
0
+
0
+ def handle_rmn_od(tokens, options) #:nodoc:
0
+ handle_m_d(tokens[0].get_tag(RepeaterMonthName), tokens[1].get_tag(OrdinalDay).type, tokens[2..tokens.size], options)
0
+ end
0
+
0
+ def handle_rmn_sy(tokens, options) #:nodoc:
0
+ month = tokens[0].get_tag(RepeaterMonthName).index
0
+ year = tokens[1].get_tag(ScalarYear).type
0
+
0
+ if month == 12
0
+ next_month_year = year + 1
0
+ next_month_month = 1
0
+ else
0
+ next_month_year = year
0
+ next_month_month = month + 1
0
+ end
0
+
0
+ begin
0
+ Span.new(Time.local(year, month), Time.local(next_month_year, next_month_month))
0
+ rescue ArgumentError
0
+ nil
0
+ end
0
+ end
0
+
0
+ def handle_rmn_sd_sy(tokens, options) #:nodoc:
0
+ month = tokens[0].get_tag(RepeaterMonthName).index
0
+ day = tokens[1].get_tag(ScalarDay).type
0
+ year = tokens[2].get_tag(ScalarYear).type
0
+
0
+ time_tokens = tokens.last(tokens.size - 3)
0
+
0
+ begin
0
+ day_start = Time.local(year, month, day)
0
+ day_or_time(day_start, time_tokens, options)
0
+ rescue ArgumentError
0
+ nil
0
+ end
0
+ end
0
+
0
+ def handle_sd_rmn_sy(tokens, options) #:nodoc:
0
+ new_tokens = [tokens[1], tokens[0], tokens[2]]
0
+ time_tokens = tokens.last(tokens.size - 3)
0
+ self.handle_rmn_sd_sy(new_tokens + time_tokens, options)
0
+ end
0
+
0
+ def handle_sm_sd_sy(tokens, options) #:nodoc:
0
+ month = tokens[0].get_tag(ScalarMonth).type
0
+ day = tokens[1].get_tag(ScalarDay).type
0
+ year = tokens[2].get_tag(ScalarYear).type
0
+
0
+ time_tokens = tokens.last(tokens.size - 3)
0
+
0
+ begin
0
+ day_start = Time.local(year, month, day) #:nodoc:
0
+ day_or_time(day_start, time_tokens, options)
0
+ rescue ArgumentError
0
+ nil
0
+ end
0
+ end
0
+
0
+ def handle_sd_sm_sy(tokens, options) #:nodoc:
0
+ new_tokens = [tokens[1], tokens[0], tokens[2]]
0
+ time_tokens = tokens.last(tokens.size - 3)
0
+ self.handle_sm_sd_sy(new_tokens + time_tokens, options)
0
+ end
0
+
0
+ def handle_sy_sm_sd(tokens, options) #:nodoc:
0
+ new_tokens = [tokens[1], tokens[2], tokens[0]]
0
+ time_tokens = tokens.last(tokens.size - 3)
0
+ self.handle_sm_sd_sy(new_tokens + time_tokens, options)
0
+ end
0
+
0
+ def handle_sm_sy(tokens, options) #:nodoc:
0
+ month = tokens[0].get_tag(ScalarMonth).type
0
+ year = tokens[1].get_tag(ScalarYear).type
0
+
0
+ if month == 12
0
+ next_month_year = year + 1
0
+ next_month_month = 1
0
+ else
0
+ next_month_year = year
0
+ next_month_month = month + 1
0
+ end
0
+
0
+ begin
0
+ Span.new(Time.local(year, month), Time.local(next_month_year, next_month_month))
0
+ rescue ArgumentError
0
+ nil
0
+ end
0
+ end
0
+
0
+ # anchors
0
+
0
+ def handle_r(tokens, options) #:nodoc:
0
+ dd_tokens = dealias_and_disambiguate_times(tokens, options)
0
+ self.get_anchor(dd_tokens, options)
0
+ end
0
+
0
+ def handle_r_g_r(tokens, options) #:nodoc:
0
+ new_tokens = [tokens[1], tokens[0], tokens[2]]
0
+ self.handle_r(new_tokens, options)
0
+ end
0
+
0
+ # arrows
0
+
0
+ def handle_srp(tokens, span, options) #:nodoc:
0
+ distance = tokens[0].get_tag(Scalar).type
0
+ repeater = tokens[1].get_tag(Repeater)
0
+ pointer = tokens[2].get_tag(Pointer).type
0
+
0
+ repeater.offset(span, distance, pointer)
0
+ end
0
+
0
+ def handle_s_r_p(tokens, options) #:nodoc:
0
+ repeater = tokens[1].get_tag(Repeater)
0
+
0
+ span =
0
+ case true
0
+ when [RepeaterYear, RepeaterSeason, RepeaterSeasonName, RepeaterMonth, RepeaterMonthName, RepeaterFortnight, RepeaterWeek].include?(repeater.class)
0
+ self.parse("today", :guess => false, :now => @now)
0
+ when [RepeaterWeekend, RepeaterDay, RepeaterDayName, RepeaterDayPortion, RepeaterHour].include?(repeater.class)
0
+ self.parse("this minute", :guess => false, :now => @now)
0
+ when [RepeaterMinute, RepeaterSecond].include?(repeater.class)
0
+ self.parse("this second", :guess => false, :now => @now)
0
+ else
0
+ raise(ChronicPain, "Invalid repeater: #{repeater.class}")
0
+ end
0
+
0
+ self.handle_srp(tokens, span, options)
0
+ end
0
+
0
+ def handle