1. Computing

Parse Time Phrases With the Chronic RubyGem

Using the Chronic RubyGem


The Chronic RubyGem is a library for dealing with language representations of dates and time. It parses the English phrase and turning it into a Ruby variable. Take, for example, the phrase "next Tuesday." It is common in the English language but difficult to turn into a Ruby Time variable. The ChronicRubyGem will parse such phrases and generate Ruby Time variables for use in your programs.

Installing Chronic

Chronic is installed as any other gem. There are no native language dependencies, so Chronic can be installed easily on all platforms.

$ sudo gem install chronic

Parse Dates and Times

The primary method of parsing English language dates is with the Chronic.parse method. Simply pass the parse method a string containing an English phrase and Chronic will attempt to generate a Time variable from it. If the phrase is not understood, nil will be returned. Below is anIRB session showing some examples of how Chronic can be used.

$ irb -rubygems
irb(main):001:0> require 'chronic'
=> true
irb(main):002:0> Chronic.parse "two weeks from now"
=> Mon Aug 04 15:18:18 -0400 2008
irb(main):003:0> Chronic.parse "10 minutes ago"
=> Mon Jul 21 15:08:27 -0400 2008
irb(main):004:0> Chronic.parse "next Tuesday"
=> Tue Jul 22 12:00:00 -0400 2008
irb(main):005:0> Chronic.parse "3 fridays from now"
=> nil
irb(main):006:0> Chronic.parse "first friday of next month"
=> Fri Aug 08 12:00:00 -0400 2008
irb(main):007:0> Chronic.parse "last friday in 2008"
=> nil

These examples were shown to illustrate two points: Chronic knows how to parse many common phrases having to do with time but, since there are many ways to say things in the idiomatic English language, not all phrases can be supported.

Parse Time and Date Ranges

By default, Chronic's parse method returns a single time. With some phrases, such as "10 minutes ago," this makes perfect sense. However, returning a single time for "next week" doesn't make a lot of sense. The phrase is referring to the entire week, not to a single time within that week. To deal with the idiosyncrasy of time ranges, you have to pass the :guess => false parameter to the parse method.

A Chronic::Span object is derived from the Ruby Range object. The object can be manipulated and compared in the same ways. Of particular interest is the include? method, which will return true if a given time is within the range.

$ irb -rubygems
irb(main):001:0> require 'chronic'
=> true
irb(main):002:0> a = Chronic.parse "next week", :guess => false
=> Sun Jul 27 00:00:00 -0400 2008..Sun Aug 03 00:00:00 -0400 2008
irb(main):003:0> b = Chronic.parse "next monday"
=> Mon Jul 28 12:00:00 -0400 2008
irb(main):004:0> a.include? b
=> true
irb(main):005:0> c = Chronic.parse "yesterday"
=> Sun Jul 20 12:00:00 -0400 2008
irb(main):006:0> a.include? c
=> false

Limitations and Ambiguities

Chronic should be used with care. Though it may seem extremely helpful at first, it does have some serious limitations.
  1. Chronic doesn't account for language ambiguities. Phrases such as "next Tuesday" can mean different things to different people. To some, it means "the next Tuesday" and to others it means "the Tuesday of next week." Using such phrases in place of a more accurate method for time and date input can cause mistakes.
  2. Chronic cannot parse all phrases. Its method of parsing has no understanding of the English language and has a very limited vocabulary. Though more phrases will undoubtedly be added in future versions, Chronic will never be a natural language parser.
  3. Chronic only supports the English language. If the users of your application speak another language, Chronic will not be useful to them at all.
  1. About.com
  2. Computing
  3. Ruby
  4. Gems
  5. Spotlight on RubyGems: Chronic

©2014 About.com. All rights reserved.