Sha256: ba823c36444d73ef9829f8a064b115a3d183abf4bec926a956a9f041edd38e91

Contents?: true

Size: 1.72 KB

Versions: 19

Compression:

Stored size: 1.72 KB

Contents

module SugarCube
  module DateParser
    # Parse a date string: E.g.:
    #
    # SugarCube::DateParser.parse_date "There is a date in here tomorrow at 9:00 AM"
    #
    # => 2013-02-20 09:00:00 -0800
    def self.parse_date(date_string)
      detect(date_string).first.date
    end

    # Parse time zone from date
    #
    # SugarCube::DateParser.parse_date "There is a date in here tomorrow at 9:00 AM EDT"
    #
    # Caveat: This is implemented per Apple documentation. I've never really
    #         seen it work.
    def self.parse_time_zone(date_string)
      detect(date_string).first.timeZone
    end

    # Parse a date string: E.g.:
    #
    # SugarCube::DateParser.parse_date "You have a meeting from 9:00 AM to 3:00 PM"
    #
    # => 21600.0
    #
    # Divide by 3600.0 to get number of hours duration.
    def self.parse_duration(date_string)
      detect(date_string).first.send(:duration)
    end

    # Parse a date into a raw match array for further processing
    def self.match(date_string)
      detect(date_string)
    end

    private
    def self.detect(date_string)
      error = Pointer.new(:object)
      detector = NSDataDetector.dataDetectorWithTypes(NSTextCheckingTypeDate, error:error)
      matches = detector.matchesInString(date_string, options:0, range:NSMakeRange(0, date_string.length))
    end
  end
end

class String
  # Use NSDataDetector to parse a string containing a date
  # or duration. These can be of the form:
  #
  # "tomorrow at 7:30 PM"
  # "11.23.2013"
  # "from 7:30 to 10:00 AM"
  #
  # etc.
  def to_date
    SugarCube::DateParser.parse_date(self)
  end

  def to_timezone
    SugarCube::DateParser.parse_timezone(self)
  end

  def to_duration
    SugarCube::DateParser.parse_duration(self)
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
sugarcube-0.20.13 lib/sugarcube/date_parser.rb
sugarcube-0.20.12 lib/sugarcube/date_parser.rb
sugarcube-0.20.11 lib/sugarcube/date_parser.rb
sugarcube-0.20.10 lib/sugarcube/date_parser.rb
sugarcube-0.20.9 lib/sugarcube/date_parser.rb
sugarcube-0.20.8 lib/sugarcube/date_parser.rb
sugarcube-0.20.7 lib/sugarcube/date_parser.rb
sugarcube-0.20.6 lib/sugarcube/date_parser.rb
sugarcube-0.20.5 lib/sugarcube/date_parser.rb
sugarcube-0.20.4 lib/sugarcube/date_parser.rb
sugarcube-0.20.3 lib/sugarcube/date_parser.rb
sugarcube-0.20.1 lib/sugarcube/date_parser.rb
sugarcube-0.20.0 lib/sugarcube/date_parser.rb
sugarcube-0.19.5 lib/sugarcube/date_parser.rb
sugarcube-0.19.4 lib/sugarcube/date_parser.rb
sugarcube-0.19.2 lib/sugarcube/date_parser.rb
sugarcube-0.19.0 lib/sugarcube/date_parser.rb
sugarcube-0.18.21 lib/sugarcube/date_parser.rb
sugarcube-0.18.20 lib/sugarcube/date_parser.rb