Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/feedjira/core_ext/time.rb

Class Method Summary collapse

Class Method Details

.parse_safely(datetime) ⇒ 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.



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(datetime)
  if datetime.is_a?(Time)
    datetime.utc
  elsif datetime.respond_to?(:to_datetime)
    datetime.to_time.utc
  else
    parse_string_safely datetime.to_s
  end
rescue StandardError => e
  Feedjira.logger.debug("Failed to parse time #{datetime}")
  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 /\A\d{14}\z/.match?(string)
    parse("#{string}Z", true)
  else
    parse(string).utc
  end
end