Sha256: 63d86a6ecff189ce9cceb18e743d65e64e8b513885fd996cd4904e5a2eb081e4

Contents?: true

Size: 603 Bytes

Versions: 5

Compression:

Stored size: 603 Bytes

Contents

require 'time'
require 'date'
DateTime.class_eval do
  #
  # Parses the time but never fails.
  # Return value is always in the UTC time zone.
  #
  # A flattened datetime -- a 12-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.blank?
    begin
      if dt.to_s =~ /\A\d{12}Z?\z/
        parse(dt+'Z', true)
      else
        parse(dt, true).utc
      end
    rescue StandardError => e
      Log.info e
    end
  end

  def self.parse_and_flatten str
    parse_safely(str).to_flat
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
wukong-1.5.3 lib/wukong/extensions/date_time.rb
wukong-1.5.2 lib/wukong/extensions/date_time.rb
wukong-1.5.1 lib/wukong/extensions/date_time.rb
wukong-1.5.0 lib/wukong/extensions/date_time.rb
wukong-1.4.12 lib/wukong/extensions/date_time.rb