public
Fork of mojombo/chronic
Description: Chronic is a pure Ruby natural language date parser.
Homepage: http://chronic.rubyforge.org
Clone URL: git://github.com/technoweenie/chronic.git
Add handler for '5pm on May 27' and the like
indirect (author)
Tue Jan 29 11:50:25 -0800 2008
commit  2dd91432a5c39809c7d3bb3f8e928d86080b947e
tree    b4be45f9646d8d14119d40673bf6a2217422b5ad
parent  bc84958b62840100ab06f67a6656213e81b047e9
...
10
11
12
 
13
 
14
15
16
...
109
110
111
 
 
 
 
 
 
 
 
112
113
114
 
 
 
 
 
 
 
 
115
116
117
...
10
11
12
13
14
15
16
17
18
...
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
0
@@ -10,7 +10,9 @@ module Chronic
0
                  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_time, :repeater_day_portion?, :separator_on?, :repeater_month_name, :scalar_day], :handle_rmn_sd_on),
0
                  Handler.new([:repeater_month_name, :ordinal_day, :separator_at?, 'time?'], :handle_rmn_od),
0
+ Handler.new([:repeater_time, :repeater_day_portion?, :separator_on?, :repeater_month_name, :ordinal_day], :handle_rmn_od_on),
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
@@ -109,9 +111,25 @@ module Chronic
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_sd_on(tokens, options) #:nodoc:
0
+ if tokens.size > 3
0
+ handle_m_d(tokens[2].get_tag(RepeaterMonthName), tokens[3].get_tag(ScalarDay).type, tokens[0..1], options)
0
+ else
0
+ handle_m_d(tokens[1].get_tag(RepeaterMonthName), tokens[2].get_tag(ScalarDay).type, tokens[0..0], options)
0
+ end
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_od_on(tokens, options) #:nodoc:
0
+ if tokens.size > 3
0
+ handle_m_d(tokens[2].get_tag(RepeaterMonthName), tokens[3].get_tag(OrdinalDay).type, tokens[0..1], options)
0
+ else
0
+ handle_m_d(tokens[1].get_tag(RepeaterMonthName), tokens[2].get_tag(OrdinalDay).type, tokens[0..0], options)
0
+ end
0
+ end
0
     
0
     def handle_rmn_sy(tokens, options) #:nodoc:
0
       month = tokens[0].get_tag(RepeaterMonthName).index
...
7
8
9
 
10
11
12
...
44
45
46
 
 
 
 
 
 
 
 
47
48
49
...
72
73
74
 
 
 
 
 
 
75
76
77
...
7
8
9
10
11
12
13
...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
...
81
82
83
84
85
86
87
88
89
90
91
92
0
@@ -7,6 +7,7 @@ module Chronic
0
         if t = self.scan_for_slash_or_dash(tokens[i]) then tokens[i].tag(t); next end
0
         if t = self.scan_for_at(tokens[i]) then tokens[i].tag(t); next end
0
         if t = self.scan_for_in(tokens[i]) then tokens[i].tag(t); next end
0
+ if t = self.scan_for_on(tokens[i]) then tokens[i].tag(t); next end
0
       end
0
       tokens
0
     end
0
@@ -44,6 +45,14 @@ module Chronic
0
       return nil
0
     end
0
     
0
+ def self.scan_for_on(token)
0
+ scanner = {/^on$/ => :on}
0
+ scanner.keys.each do |scanner_item|
0
+ return SeparatorOn.new(scanner[scanner_item]) if scanner_item =~ token.word
0
+ end
0
+ return nil
0
+ end
0
+
0
     def to_s
0
       'separator'
0
     end
0
@@ -72,5 +81,11 @@ module Chronic
0
       super << '-in'
0
     end
0
   end
0
+
0
+ class SeparatorOn < Separator #:nodoc:
0
+ def to_s
0
+ super << '-on'
0
+ end
0
+ end
0
 
0
 end
0
\ No newline at end of file
...
27
28
29
 
 
 
 
 
 
 
 
 
 
 
30
31
32
...
44
45
46
 
 
 
 
 
 
 
 
 
 
 
47
48
49
...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
0
@@ -27,6 +27,17 @@ class TestParsing < Test::Unit::TestCase
0
     time = parse_now("may 28 at 5:32.19pm", :context => :past)
0
     assert_equal Time.local(2006, 5, 28, 17, 32, 19), time
0
     
0
+ # rm_sd_on
0
+
0
+ time = parse_now("5pm on may 28")
0
+ assert_equal Time.local(2007, 5, 28, 17), time
0
+
0
+ time = parse_now("5pm may 28")
0
+ assert_equal Time.local(2007, 5, 28, 17), time
0
+
0
+ time = parse_now("5 on may 28", :ambiguous_time_range => :none)
0
+ assert_equal Time.local(2007, 5, 28, 05), time
0
+
0
     # rm_od
0
     
0
     time = parse_now("may 27th")
0
@@ -44,6 +55,17 @@ class TestParsing < Test::Unit::TestCase
0
     time = parse_now("may 27th at 5", :ambiguous_time_range => :none)
0
     assert_equal Time.local(2007, 5, 27, 5), time
0
     
0
+ # rm_od_on
0
+
0
+ time = parse_now("5:00 pm may 27th", :context => :past)
0
+ assert_equal Time.local(2006, 5, 27, 17), time
0
+
0
+ time = parse_now("5pm on may 27th", :context => :past)
0
+ assert_equal Time.local(2006, 5, 27, 17), time
0
+
0
+ time = parse_now("5 on may 27th", :ambiguous_time_range => :none)
0
+ assert_equal Time.local(2007, 5, 27, 5), time
0
+
0
     # rm_sy
0
     
0
     time = parse_now("June 1979")

Comments

    No one has commented yet.