# File lib/chronic/handlers.rb, line 85 def day_or_time(day_start, time_tokens, options) outer_span = Span.new(day_start, day_start + (24 * 60 * 60)) if !time_tokens.empty? @now = outer_span.begin time = get_anchor(dealias_and_disambiguate_times(time_tokens, options), options) return time else return outer_span end end
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.
Options are:
:past or :future (defaults to :future)
If your string represents a birthday, you can set :context to :past and if an ambiguous string is given, it will assume it is in the past. Specify :future or omit to set a future context.
Time (defaults to Time.now)
By setting :now to a Time, all computations will be based off of that time instead of Time.now
true or false (defaults to true)
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 :guess to false and a Chronic::Span will be returned.
Integer or :none (defaults to 6 (6am-6pm))
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.
# File lib/chronic/chronic.rb, line 41 def parse(text, specified_options = {}) # get options and set defaults if necessary default_options = {:context => :future, :now => Time.now, :guess => true, :ambiguous_time_range => 6} options = default_options.merge specified_options # ensure the specified options are valid specified_options.keys.each do |key| default_options.keys.include?(key) || raise(InvalidArgumentException, "#{key} is not a valid option key.") end [:past, :future, :none].include?(options[:context]) || raise(InvalidArgumentException, "Invalid value ':#{options[:context]}' for :context specified. Valid values are :past and :future.") # store now for later =) @now = options[:now] # put the text into a normal format to ease scanning text = self.pre_normalize(text) # get base tokens for each word @tokens = self.base_tokenize(text) # scan the tokens with each token scanner [Repeater].each do |tokenizer| @tokens = tokenizer.scan(@tokens, options) end [Grabber, Pointer, Scalar, Ordinal, Separator, TimeZone].each do |tokenizer| @tokens = tokenizer.scan(@tokens) end # strip any non-tagged tokens @tokens = @tokens.select { |token| token.tagged? } if Chronic.debug puts "+---------------------------------------------------" puts "| " + @tokens.to_s puts "+---------------------------------------------------" end # do the heavy lifting begin span = self.tokens_to_span(@tokens, options) rescue raise return nil end # guess a time within a span if required if options[:guess] return self.guess(span) else return span end end
Generated with the Darkfish Rdoc Generator 2.