Skip to content
This repository
Browse code

Merge pull request #153 from kareemk/master

Add support for RepeaterDayName, RepeaterMonthName, Ordinal/ScalarDay and Time
  • Loading branch information...
commit 80421739a0da721dca831a43af3314b7d7f46294 2 parents ed76d6e + cd78f11
Lee Jarvis authored
50  lib/chronic/handlers.rb
@@ -274,14 +274,46 @@ def handle_sm_sy(tokens, options)
274 274
     def handle_rdn_rmn_od(tokens, options)
275 275
       month = tokens[1].get_tag(RepeaterMonthName)
276 276
       day = tokens[2].get_tag(OrdinalDay).type
  277
+      time_tokens = tokens.last(tokens.size - 3)
277 278
       year = self.now.year
278 279
 
279 280
       return if month_overflow?(year, month.index, day)
280 281
 
281 282
       begin
282  
-        start_time = Chronic.time_class.local(year, month.index, day)
283  
-        end_time = time_with_rollover(year, month.index, day + 1)
284  
-        Span.new(start_time, end_time)
  283
+        if time_tokens.empty?
  284
+          start_time = Chronic.time_class.local(year, month.index, day)
  285
+          end_time = time_with_rollover(year, month.index, day + 1)
  286
+          Span.new(start_time, end_time)
  287
+        else
  288
+          day_start = Chronic.time_class.local(year, month.index, day)
  289
+          day_or_time(day_start, time_tokens, options)
  290
+        end
  291
+      rescue ArgumentError
  292
+        nil
  293
+      end
  294
+    end
  295
+
  296
+    # Handle RepeaterDayName OrdinalDay
  297
+    def handle_rdn_od(tokens, options)
  298
+      day = tokens[1].get_tag(OrdinalDay).type
  299
+      time_tokens = tokens.last(tokens.size - 2)
  300
+      year = self.now.year
  301
+      month = self.now.month
  302
+      if options[:context] == :future
  303
+        self.now.day > day ? month += 1 : month
  304
+      end
  305
+
  306
+      return if month_overflow?(year, month, day)
  307
+
  308
+      begin
  309
+        if time_tokens.empty?
  310
+          start_time = Chronic.time_class.local(year, month, day)
  311
+          end_time = time_with_rollover(year, month, day + 1)
  312
+          Span.new(start_time, end_time)
  313
+        else
  314
+          day_start = Chronic.time_class.local(year, month, day)
  315
+          day_or_time(day_start, time_tokens, options)
  316
+        end
285 317
       rescue ArgumentError
286 318
         nil
287 319
       end
@@ -291,14 +323,20 @@ def handle_rdn_rmn_od(tokens, options)
291 323
     def handle_rdn_rmn_sd(tokens, options)
292 324
       month = tokens[1].get_tag(RepeaterMonthName)
293 325
       day = tokens[2].get_tag(ScalarDay).type
  326
+      time_tokens = tokens.last(tokens.size - 3)
294 327
       year = self.now.year
295 328
 
296 329
       return if month_overflow?(year, month.index, day)
297 330
 
298 331
       begin
299  
-        start_time = Chronic.time_class.local(year, month.index, day)
300  
-        end_time = time_with_rollover(year, month.index, day + 1)
301  
-        Span.new(start_time, end_time)
  332
+        if time_tokens.empty?
  333
+          start_time = Chronic.time_class.local(year, month.index, day)
  334
+          end_time = time_with_rollover(year, month.index, day + 1)
  335
+          Span.new(start_time, end_time)
  336
+        else
  337
+          day_start = Chronic.time_class.local(year, month.index, day)
  338
+          day_or_time(day_start, time_tokens, options)
  339
+        end
302 340
       rescue ArgumentError
303 341
         nil
304 342
       end
3  lib/chronic/parser.rb
@@ -151,6 +151,9 @@ def definitions(options = {})
151 151
           Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day], :handle_rdn_rmn_sd),
152 152
           Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day, :scalar_year], :handle_rdn_rmn_sd_sy),
153 153
           Handler.new([:repeater_day_name, :repeater_month_name, :ordinal_day], :handle_rdn_rmn_od),
  154
+          Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day, :separator_at?, 'time?'], :handle_rdn_rmn_sd),
  155
+          Handler.new([:repeater_day_name, :repeater_month_name, :ordinal_day, :separator_at?, 'time?'], :handle_rdn_rmn_od),
  156
+          Handler.new([:repeater_day_name, :ordinal_day, :separator_at?, 'time?'], :handle_rdn_od),
154 157
           Handler.new([:scalar_year, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_day, :repeater_time, :time_zone], :handle_generic),
155 158
           Handler.new([:repeater_month_name, :scalar_day, :scalar_year], :handle_rmn_sd_sy),
156 159
           Handler.new([:repeater_month_name, :ordinal_day, :scalar_year], :handle_rmn_od_sy),
32  test/test_parsing.rb
@@ -1055,6 +1055,38 @@ def test_handle_rdn_rmn_sd
1055 1055
     assert_equal Time.local(2006, 12, 31, 12), time
1056 1056
   end
1057 1057
 
  1058
+  def test_handle_rdn_rmn_sd_rt
  1059
+    time = parse_now("Thu Aug 10 4pm")
  1060
+    assert_equal Time.local(2006, 8, 10, 16), time
  1061
+
  1062
+    time = parse_now("Thu Aug 10 at 4pm")
  1063
+    assert_equal Time.local(2006, 8, 10, 16), time
  1064
+  end
  1065
+
  1066
+  def test_handle_rdn_rmn_od_rt
  1067
+    time = parse_now("Thu Aug 10th at 4pm")
  1068
+    assert_equal Time.local(2006, 8, 10, 16), time
  1069
+  end
  1070
+
  1071
+  def test_handle_rdn_od_rt
  1072
+    time = parse_now("Thu 17th at 4pm")
  1073
+    assert_equal Time.local(2006, 8, 17, 16), time
  1074
+
  1075
+    time = parse_now("Thu 16th at 4pm")
  1076
+    assert_equal Time.local(2006, 8, 16, 16), time
  1077
+
  1078
+    time = parse_now("Thu 1st at 4pm")
  1079
+    assert_equal Time.local(2006, 9, 1, 16), time
  1080
+
  1081
+    time = parse_now("Thu 1st at 4pm", :context => :past)
  1082
+    assert_equal Time.local(2006, 8, 1, 16), time
  1083
+  end
  1084
+
  1085
+  def test_handle_rdn_od
  1086
+    time = parse_now("Thu 17th")
  1087
+    assert_equal Time.local(2006, 8, 17, 12), time
  1088
+  end
  1089
+
1058 1090
   def test_handle_rdn_rmn_sd_sy
1059 1091
     time = parse_now("Thu Aug 10 2006")
1060 1092
     assert_equal Time.local(2006, 8, 10, 12), time

0 notes on commit 8042173

Please sign in to comment.
Something went wrong with that request. Please try again.