Parse natural language dates and times into Time or Chronic::Span objects.
Examples:
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 Chronic.parse('this tuesday 5:00') #=> Tue Aug 29 17:00:00 PDT 2006 Chronic.parse('this tuesday 5:00', :ambiguous_time_range => :none) #=> Tue Aug 29 05:00:00 PDT 2006 Chronic.parse('may 27th', :now => Time.local(2000, 1, 1)) #=> Sat May 27 12:00:00 PDT 2000 Chronic.parse('may 27th', :guess => false) #=> Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007
VERSION | = | "0.7.0" | ||
DEFAULT_OPTIONS | = | { :context => :future, :now => nil, :guess => true, :ambiguous_time_range => 6, :endian_precedence => [:middle, :little], :ambiguous_year_future_bias => 50 | Returns a Hash of default configuration options. |
debug | [RW] | Returns true when debug mode is enabled. |
now | [RW] |
The current Time Chronic
is using to base from.
Examples: Time.now #=> 2011-06-06 14:13:43 +0100 Chronic.parse('yesterday') #=> 2011-06-05 12:00:00 +0100 now = Time.local(2025, 12, 24) Chronic.parse('tomorrow', :now => now) #=> 2025-12-25 12:00:00 +0000 Returns a Time object. |
time_class | [RW] |
Examples:
require 'chronic' require 'active_support/time' Time.zone = 'UTC' Chronic.time_class = Time.zone Chronic.parse('June 15 2006 at 5:54 AM') # => Thu, 15 Jun 2006 05:45:00 UTC +00:00 |
Construct a new time object determining possible month overflows and leap years.
year - Integer year. month - Integer month. day - Integer day. hour - Integer hour. minute - Integer minute. second - Integer second.
Returns a new Time object constructed from these params.
List of Handler definitions. See parse for a list of options this method accepts.
options - An optional Hash of configuration options:
:endian_precedence -
Returns A Hash of Handler definitions.
Guess a specific time within the given span.
span - The Chronic::Span object to calcuate a guess from.
Returns a new Time object.
Convert number words to numbers (three => 3, fourth => 4th).
text - The String to convert.
Returns a new String with words converted to numbers.
Parses a string containing a natural language date or time.
If the parser can find a date or time, either a Time or Chronic::Span will be returned (depending on the value of `:guess`). If no date or time can be found, `nil` will be returned.
text - The String text to parse. opts - An optional Hash of configuration options:
:context - If your string represents a birthday, you can set this value to :past and if an ambiguous string is given, it will assume it is in the past. :now - Time, all computations will be based off of time instead of Time.now. :guess - By default the parser will guess a single point in time for the given date or time. If you'd rather have the entire time span returned, set this to false and a Chronic::Span will be returned. :ambiguous_time_range - If an Integer is given, ambiguous times (like 5:00) will be assumed to be within the range of that time in the AM to that time in the PM. For example, if you set it to `7`, then the parser will look for the time between 7am and 7pm. In the case of 5:00, it would assume that means 5:00pm. If `:none` is given, no assumption will be made, and the first matching instance of that time will be used. :endian_precedence - By default, Chronic will parse "03/04/2011" as the fourth day of the third month. Alternatively you can tell Chronic to parse this as the third day of the fourth month by setting this to [:little, :middle]. :ambiguous_year_future_bias - When parsing two digit years (ie 79) unlike Rubys Time class, Chronic will attempt to assume the full year using this figure. Chronic will look x amount of years into the future and past. If the two digit year is `now + x years` it's assumed to be the future, `now - x years` is assumed to be the past.
Returns a new Time object, or Chronic::Span if :guess option is false.
Clean up the specified text ready for parsing.
Clean up the string by stripping unwanted characters, converting idioms to their canonical form, converting number words to numbers (three => 3), and converting ordinal words to numeric ordinals (third => 3rd)
text - The String text to normalize.
Examples:
Chronic.pre_normalize('first day in May') #=> "1st day in may" Chronic.pre_normalize('tomorrow after noon') #=> "next day future 12:00" Chronic.pre_normalize('one hundred and thirty six days from now') #=> "136 days future this second"