Class: TimePup::TimeParser
- Inherits:
-
Object
- Object
- TimePup::TimeParser
- Defined in:
- lib/time_pup/time_parser.rb
Constant Summary collapse
- MATCHERS =
{ twelve_hour_with_period: /\d{1,4}[a|p]m/i, twenty_four_hour_with_minutes: /[0-2]{,1}\d[0-5]\d/, twenty_four_hour_without_minutes: /\d\d{,1}/, }
- AM_PM =
/[a|p]m/i
Class Method Summary collapse
- .match_key_words(time) ⇒ Object
- .parse(parsable, zone = 'UTC') ⇒ Object
- .set_time_in_future(time_part, zone) ⇒ Object
Class Method Details
.match_key_words(time) ⇒ Object
32 33 34 |
# File 'lib/time_pup/time_parser.rb', line 32 def match_key_words(time) MATCHERS.keys.inject([]) { |result, m| result << time.scan(MATCHERS[m]) }.flatten end |
.parse(parsable, zone = 'UTC') ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/time_pup/time_parser.rb', line 11 def parse(parsable, zone = 'UTC') matches = match_key_words(parsable) if matches.empty? return nil else time_part = matches.first case time_part.length when 1..2 time_part = time_part + ':00' when 3..4 unless time_part.match(AM_PM) time_part = time_part.insert(-3,':') end when 5..6 time_part = time_part.insert(-5,':') end return set_time_in_future(time_part, zone) end end |
.set_time_in_future(time_part, zone) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/time_pup/time_parser.rb', line 36 def set_time_in_future(time_part, zone) time = DateTime.parse(time_part) time_for_today = DateTime.now.to_time.in_time_zone(ActiveSupport::TimeZone[zone]) time_for_today = time_for_today.change(hour: time.hour, min: time.min).utc if time_for_today < DateTime.now.to_time.utc time_for_today + 1.day else time_for_today end end |