Sha256: 4683b846bf4c9f0631affd0770ad3a0a581623eed7ba0ee56fe90d88ab1a738d

Contents?: true

Size: 705 Bytes

Versions: 2

Compression:

Stored size: 705 Bytes

Contents

Time.class_eval do
  #
  # Parses the time but never fails.
  # Return value is always in the UTC time zone.
  #
  # A flattened datetime -- a 14-digit YYYYmmddHHMMMSS -- is fixed to the UTC
  # time zone by parsing it as YYYYmmddHHMMMSSZ <- 'Z' at end
  #
  def self.parse_safely dt
    return nil if dt.nil? || (dt.respond_to?(:empty) && dt.empty?)
    begin
      case
      when dt.is_a?(Time)            then dt.utc
      when (dt.to_s =~ /\A\d{14}\z/) then parse(dt.to_s+'Z', true)
      else                                parse(dt.to_s,     true).utc
      end
    rescue StandardError => err
      warn "Can't parse a #{self} from #{dt.inspect}"
      warn err
      return nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gorillib-model-0.0.3 lib/gorillib/core_ext/datetime.rb
gorillib-model-0.0.1 lib/gorillib/core_ext/datetime.rb