Rubygems Chronic cannot parse "2010-09-12 3:12pm" but can parse "2010-09-12 3pm" or "last monday 3:12pm"?

169 views Asked by At

It seems strange that "2010--9-12 3:12pm" is a weak spot of Chronic Rubygem? Is there a way to make it work? (or another gem that can do it?) I also hope to find a parser that can handle 2010-09-12 3:12pm UTC or other timezones such as PDT, PST, or UTC+8 or -0700

irb(main):001:0> RUBY_VERSION
=> "1.8.7"

irb(main):002:0> require 'rubygems'
=> true
irb(main):003:0> require 'chronic'
=> true

irb(main):004:0> Chronic.parse('9/12/2010')
=> Sun Sep 12 12:00:00 -0700 2010

irb(main):005:0> Chronic.parse('9/12/2010 3pm')
=> Sun Sep 12 15:00:00 -0700 2010

irb(main):006:0> Chronic.parse('9/12/2010 3:12pm')    # <-- cannot work
=> nil

irb(main):007:0> Chronic.parse('last monday 3:12pm')
=> Mon Sep 06 15:12:00 -0700 2010

irb(main):008:0> Chronic.parse('2010-09-12')
=> Sun Sep 12 12:00:00 -0700 2010

irb(main):009:0> Chronic.parse('2010-09-12 3:12pm')   # <-- cannot work
=> nil
1

There are 1 answers

2
draegtun On

It works for me using latest Chronic on Github on Ruby 1.8.7 (2009-06-12 patchlevel 174):

>> RUBY_VERSION
=> "1.8.7"

>> require './chronic/lib/chronic'
=> true

>> Chronic.parse('9/12/2010 3:12pm')
=> Sun Sep 12 15:12:00 +0100 2010

>> Chronic.parse('2010-09-12 3:12pm')
=> Sun Sep 12 15:12:00 +0100 2010

In the test suite there are tests for:

time = parse_now("2006-08-20 7pm")
assert_equal Time.local(2006, 8, 20, 19), time

time = parse_now("2006-08-20 03:00")
assert_equal Time.local(2006, 8, 20, 3), time

But not exact match to your problem date(s). So if you have discovered a bug then it might be worth telling Chronic's author so that he can update the tests.

BTW... what do you get when you try:

require 'time'

puts Time.parse('2010-09-12 3:12pm')

Because I get the correct: Sun Sep 12 15:12:00 +0100 2010 (NB. I'm in day light saving time here). I mention this because I saw a ambiguous tweet this morning about problem with time API in a minor point release.

/I3az/