Skip to content

Instantly share code, notes, and snippets.

@sxross
Created February 19, 2013 22:21
Show Gist options
  • Save sxross/4990717 to your computer and use it in GitHub Desktop.
Save sxross/4990717 to your computer and use it in GitHub Desktop.
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"
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.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment