Skip to content
This repository

month/day format with time returns nil #114

Merged
merged 3 commits into from over 1 year ago

2 participants

Alex Grant Lee Jarvis
Alex Grant

This is related to #59, which I commented on but I guess it wasn't noticed. The issue is that month/day parsing (without a year) seems to always return nil when a time is given. For example:

irb> Chronic.parse('5/6')
=> 2012-05-06 12:00:00 -0400
irb> Chronic.parse('5/6/2012 at 12pm')
=> 2012-05-06 12:00:00 -0400
irb> Chronic.parse('5/6 at 12pm')
=> nil
Alex Grant

The attached pull request fixes this issue and includes test cases for it.

Alex Grant grantovich commented on the diff September 07, 2012
test/test_chronic.rb
@@ -67,6 +67,8 @@ def test_endian_definitions
67 67
     # middle, little
68 68
     endians = [
69 69
       Chronic::Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
  70
+      Chronic::Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_at?, 'time?'], :handle_sm_sd),
  71
+      Chronic::Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_at?, 'time?'], :handle_sd_sm),
70 72
       Chronic::Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
71 73
     ]
72 74
 
1

I left this test as-is in the interest of separating changes, but I'm not sure why these lines and the following assert_equal are present. It's just testing that the endians array is equal to a copy-pasted copy of itself. Am I missing something here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Lee Jarvis
Collaborator

Yeah I must have missed your comment on that other issue. This looks great, thanks for the pull will merge this straight away.

Lee Jarvis leejarvis merged commit 201e2c3 into from September 07, 2012
Lee Jarvis leejarvis closed this September 07, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
3  lib/chronic/chronic.rb
@@ -188,7 +188,6 @@ def definitions(options={})
188 188
           Handler.new([:scalar_day, :repeater_month_name, :scalar_year, :separator_at?, 'time?'], :handle_sd_rmn_sy),
189 189
           Handler.new([:scalar_day, :repeater_month_name, :separator_at?, 'time?'], :handle_sd_rmn),
190 190
           Handler.new([:scalar_year, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_day, :separator_at?, 'time?'], :handle_sy_sm_sd),
191  
-          Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day], :handle_sm_sd),
192 191
           Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_year], :handle_sm_sy),
193 192
           Handler.new([:scalar_day, :separator_slash_or_dash, :repeater_month_name, :separator_slash_or_dash, :scalar_year], :handle_sm_rmn_sy)
194 193
         ],
@@ -216,6 +215,8 @@ def definitions(options={})
216 215
 
217 216
       endians = [
218 217
         Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
  218
+        Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_at?, 'time?'], :handle_sm_sd),
  219
+        Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_at?, 'time?'], :handle_sd_sm),
219 220
         Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
220 221
       ]
221 222
 
19  lib/chronic/handlers.rb
@@ -224,27 +224,30 @@ def handle_sy_sm_sd(tokens, options)
224 224
       handle_sm_sd_sy(new_tokens + time_tokens, options)
225 225
     end
226 226
 
227  
-    # Handle scalar-day/scalar-month AND scalar-month/scalar-day
  227
+    # Handle scalar-month/scalar-day
228 228
     def handle_sm_sd(tokens, options)
229 229
       month = tokens[0].get_tag(ScalarMonth).type
230 230
       day = tokens[1].get_tag(ScalarDay).type
231 231
       year = Chronic.now.year
232  
-
233  
-      if Array(options[:endian_precedence]).first == :little
234  
-        day, month = month, day
235  
-      end
  232
+      time_tokens = tokens.last(tokens.size - 2)
236 233
 
237 234
       return if month_overflow?(year, month, day)
238 235
 
239 236
       begin
240  
-        start_time = Chronic.time_class.local(year, month, day)
241  
-        end_time = Chronic.time_class.local(year, month, day + 1)
242  
-        Span.new(start_time, end_time)
  237
+        day_start = Chronic.time_class.local(year, month, day)
  238
+        day_or_time(day_start, time_tokens, options)
243 239
       rescue ArgumentError
244 240
         nil
245 241
       end
246 242
     end
247 243
 
  244
+    # Handle scalar-day/scalar-month
  245
+    def handle_sd_sm(tokens, options)
  246
+      new_tokens = [tokens[1], tokens[0]]
  247
+      time_tokens = tokens.last(tokens.size - 2)
  248
+      handle_sm_sd(new_tokens + time_tokens, options)
  249
+    end
  250
+
248 251
     # Handle scalar-month/scalar-year
249 252
     def handle_sm_sy(tokens, options)
250 253
       month = tokens[0].get_tag(ScalarMonth).type
2  test/test_chronic.rb
@@ -67,6 +67,8 @@ def test_endian_definitions
67 67
     # middle, little
68 68
     endians = [
69 69
       Chronic::Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
  70
+      Chronic::Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_at?, 'time?'], :handle_sm_sd),
  71
+      Chronic::Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_at?, 'time?'], :handle_sd_sm),
70 72
       Chronic::Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
71 73
     ]
72 74
 
8  test/test_parsing.rb
@@ -294,8 +294,14 @@ def test_handle_sm_sd
294 294
     time = parse_now("05/06", :endian_precedence => [:little, :medium])
295 295
     assert_equal Time.local(2006, 6, 5, 12), time
296 296
 
  297
+    time = parse_now("05/06 6:05:57 PM")
  298
+    assert_equal Time.local(2006, 5, 6, 18, 05, 57), time
  299
+
  300
+    time = parse_now("05/06 6:05:57 PM", :endian_precedence => [:little, :medium])
  301
+    assert_equal Time.local(2006, 6, 5, 18, 05, 57), time
  302
+
297 303
     time = parse_now("13/01")
298  
-    assert_nil time
  304
+    assert_equal Time.local(2006, 1, 13, 12), time
299 305
   end
300 306
 
301 307
   # def test_handle_sm_sy
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.