Sha256: c5ee19e0736f92282c7129752a9e65c2c16153801e78ce05e21b8a0237e46736

Contents?: true

Size: 771 Bytes

Versions: 12

Compression:

Stored size: 771 Bytes

Contents

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<String or Time>] 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
      case
      when dt.is_a?(Time)
        dt.utc
      when dt.respond_to?(:empty?) && dt.empty?
        nil
      when dt.respond_to?(:to_datetime)
        dt.to_datetime.utc
      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 unless method_defined?(:parse_safely)
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
feedjira-2.0.0 lib/feedjira/core_ext/time.rb
feedjira-1.6.0 lib/feedjira/core_ext/time.rb
feedjira-1.5.0 lib/feedjira/core_ext/time.rb
feedjira-1.4.0 lib/feedjira/core_ext/time.rb
feedjira-1.3.1 lib/feedjira/core_ext/time.rb
feedjira-1.3.0 lib/feedjira/core_ext/time.rb
feedjira-1.2.0 lib/feedjira/core_ext/time.rb
feedjira-1.1.0 lib/feedjira/core_ext/time.rb
feedjira-1.0.0 lib/feedjira/core_ext/time.rb
feedjira-0.9.0 lib/feedjira/core_ext/time.rb
feedzirra-0.7.1 lib/feedzirra/core_ext/time.rb
feedzirra-0.7.0 lib/feedzirra/core_ext/time.rb