Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/feedjira/core_ext/time.rb
Overview
rubocop:disable Style/DocumentationMethod
Class Method Summary collapse
-
.parse_safely(dt) ⇒ Object
Parse a time string and convert it to UTC without raising errors.
- .parse_string_safely(string) ⇒ Object
Class Method Details
.parse_safely(dt) ⇒ Object
Parse a time string and convert it to UTC without raising errors. Parses a flattened 14-digit time (YYYYmmddHHMMMSS) as UTC.
Parameters
- dt<String or Time>
-
Time definition to be parsed.
Returns
A Time instance in UTC or nil if there were errors while parsing. rubocop:disable Metrics/MethodLength
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/feedjira/core_ext/time.rb', line 15 def self.parse_safely(dt) if dt.is_a?(Time) dt.utc elsif dt.respond_to?(:to_datetime) dt.to_datetime.utc elsif dt.respond_to? :to_s parse_string_safely dt.to_s end rescue StandardError => e Feedjira.logger.debug { "Failed to parse time #{dt}" } Feedjira.logger.debug(e) nil end |
.parse_string_safely(string) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/feedjira/core_ext/time.rb', line 29 def self.parse_string_safely(string) return nil if string.empty? if string =~ /\A\d{14}\z/ parse("#{string}Z", true) else parse(string).utc end end |