Skip to content
This repository
tree: a0ccc40da5
Fetching contributors…

Cannot retrieve contributors at this time

file 55 lines (41 sloc) 1.189 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
# A wrapper for the 'chronic' gem, which parses
# date information.
#
# Project website: http://chronic.rubyforge.org/
class Treat::Extractors::Time::Chronic

  # Require the 'chronic' gem.
  silence_warnings { require 'chronic' }

  # Require the Ruby DateTime module
  require 'date'

  # Return the date information contained within
  # the entity by parsing it with the 'chronic' gem.
  #
  # Options: none.
  def self.time(entity, options = {})

    s = entity.to_s
    return if s =~ /^[0-9]+$/
    time = nil
    
    silence_warnings do
      time = ::Chronic.parse(s, {:guess => true})
    end
    
    if entity.has_parent? && remove_time_from_ancestors(entity, time)
      nil
    else
      time
    end
    
  end
  
  # Keeps the lowest-level time annotations that do
  # not conflict with a higher time annotation.
  # Returns true if the entity conflicts with a
  # higher-level time annotation.
  def self.remove_time_from_ancestors(entity, time)
    
    entity.ancestors_with_type(:phrase).each do |a|
      
      next if !a.has?(:time)
      unless a.get(:time) == time
        return true
      end
      a.unset(:time)
      
    end
    
    false
    
  end

end
Something went wrong with that request. Please try again.