require 'time' require 'date' class Time # Parse a time string and convert it to UTC without raising errors. # Parses a flattened 14-digit time (YYYYmmddHHMMMSS) as UTC. # # === Parameters # [dt] Time definition to be parsed. # # === Returns # A Time instance in UTC or nil if there were errors while parsing. 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 nil end 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 end