Class: Time

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

Class Method Summary collapse

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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/feedzirra/core_ext/time.rb', line 13

def self.parse_safely(dt)
  if dt
    case
    when dt.is_a?(Time)
      dt.utc
    when dt.respond_to?(:empty?) && dt.empty?
      nil
    when dt.to_s =~ /\A\d{14}\z/
      parse("#{dt.to_s}Z", true)
    else
      parse(dt.to_s, true).utc
    end
  end
rescue StandardError
  nil
end