by Tom Preston-Werner
Chronic is a natural language date/time parser written in pure Ruby. See below for the wide variety of formats Chronic will parse.
This is a fork of stable version supported by evaryont, see github.com/evaryont/chronic
This version supports english and russian queries. If you’re interested in adding more translations, take a look at chronic/lib/translation.rb and examples in chronic/lib/languages/ folder.
Chronic can be installed via RubyGems:
$ sudo gem install evaryont-chronic
You can parse strings containing a natural language date using the Chronic.parse method.
require 'rubygems' require 'chronic' Time.now #=> Sun Aug 27 23:18:25 PDT 2006 #--- Chronic.parse('tomorrow') #=> Mon Aug 28 12:00:00 PDT 2006 Chronic.parse('monday', :context => :past) #=> Mon Aug 21 12:00:00 PDT 2006
More examples are shown on evaryont’s readme: github.com/evaryont/chronic/blob/master/README.rdoc
You can specify full or partial date:
Chronic.parse('16 марта') #=> Wed Mar 16 12:00:00 +0300 2011 Chronic.parse('январь 2011', :guess => false) #=> Sat Jan 01 00:00:00 +0300 2011...Tue Feb 01 00:00:00 +0300 2011
Relative dates also work («сегодня», «завтра», «позавчера», «через неделю»).
Weekdays are supported too:
Time.now #=> Sun Oct 31 23:17:48 +0300 2010 Chronic.parse('прошлая среда') #=> Wed Oct 27 12:00:00 +0400 2010
What’s not here:
* numeric words. While 'среда через 2 недели' will work, 'среда через две недели' won't * phrases like 'бесчетверти пять' or 'пол-шестого'
Chronic allows you to set which Time class to use when constructing times. By default, the built in Ruby time class creates times in your system’s local time zone. You can set this to something like ActiveSupport’s TimeZone class to get full time zone support.
>> Time.zone = "UTC" >> Chronic.time_class = Time.zone >> Chronic.parse("June 15 2006 at 5:45 AM") => Thu, 15 Jun 2006 05:45:00 UTC +00:00
Chronic uses Ruby’s built in Time class for all time storage and computation. Because of this, only times that the Time class can handle will be properly parsed. Parsing for times outside of this range will simply return nil. Support for a wider range of times is planned for a future release.
(The MIT License)
Copyright © 2008 Tom Preston-Werner
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.