The Chronic: A Ruby Time Library

So, I was trying to use Ruby to get the date 6 months ago.  This is usually accomplished using the core extensions in ActiveSupport, which is OK, but it was too much work.  Then, I found an awesome article talking about Chronic, a minimal date/time parsing library for Ruby:

$ irb -rrubygems -rchronic
irb(main):001:0> Chronic.parse('6 months ago')
=> 2010-03-25 21:10:10 -0400

Wow that’s like so awesome.  I wonder what else it can do…

irb(main):002:0> Chronic.parse('three days after memorial day')
=> 2010-09-28 23:00:00 -0400

Well, that didn’t work.  Maybe I’ll submit a patch…  How about this?:

irb(main):014:0> Chronic.parse('three days after today')
=> 2010-09-28 23:00:00 -0400
irb(main):015:0> Chronic.parse('three days after tomorrow')
=> 2010-09-29 12:00:00 -0400

Well, that worked.  Patch time!


Well, I added US non-religious holidays to chronic.  It took me a while to figure it out, but it works!

irb(main):001:0> Chronic.parse("MLK Day")
=> 2011-01-17 12:00:00 -0500
irb(main):002:0> Chronic.parse("christmas eve")
=> 2010-12-24 12:00:00 -0500
irb(main):005:0> Chronic.parse("christmas")
=> 2010-12-25 12:00:00 -0500

Of course, my original example still fails, because Memorial Day is very special:

# (last Monday of May, traditionally 30 May)
text.gsub!(/\bMemorial Day\b/i, 'last monday in may')

The last selector still doesn’t work since it’s not an identifiable ordinal.  If someone wants to fix this, please do it.  Once things like “the last monday in may” work in all of Chronic, Memorial day will start working automatically.

You can get the ruby gem using the instructions here:

The git repository is here if you want to fork:

2 thoughts on “The Chronic: A Ruby Time Library

  1. Matt

    Does ‘three days after memorial day’ mean the closest Memorial day or the Memorial day in the current calendar year?

    Maybe you weren’t specific enough.

Comments are closed.