Module: DateParser
- Defined in:
- lib/motion_model/date_parser.rb
Class Method Summary collapse
- .detect(date_string) ⇒ Object
-
.match(date_string) ⇒ Object
Parse a date into a raw match array for further processing.
-
.parse_date(date_string) ⇒ Object
Parse a date string: E.g.:.
-
.parse_duration(date_string) ⇒ Object
Parse a date string: E.g.:.
-
.parse_time_zone(date_string) ⇒ Object
Parse time zone from date.
Class Method Details
.detect(date_string) ⇒ Object
38 39 40 41 42 |
# File 'lib/motion_model/date_parser.rb', line 38 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 |
.match(date_string) ⇒ Object
Parse a date into a raw match array for further processing
33 34 35 |
# File 'lib/motion_model/date_parser.rb', line 33 def self.match(date_string) detect(date_string) end |
.parse_date(date_string) ⇒ Object
Parse a date string: E.g.:
DateParser.parse_date "There is a date in here tomorrow at 9:00 AM"
=> 2013-02-20 09:00:00 -0800
7 8 9 |
# File 'lib/motion_model/date_parser.rb', line 7 def self.parse_date(date_string) detect(date_string).first.date end |
.parse_duration(date_string) ⇒ Object
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.
28 29 30 |
# File 'lib/motion_model/date_parser.rb', line 28 def self.parse_duration(date_string) detect(date_string).first.send(:duration) end |
.parse_time_zone(date_string) ⇒ Object
Parse time zone from date
ateParser.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.
17 18 19 |
# File 'lib/motion_model/date_parser.rb', line 17 def self.parse_time_zone(date_string) detect(date_string).first.timeZone end |