Sha256: 8c89bdd3c900e7fdf1343a8ecc142ea89bc3b7a44c29b8dba0b94dc48a7bb279

Contents?: true

Size: 758 Bytes

Versions: 3

Compression:

Stored size: 758 Bytes

Contents

module Cotcube
  module Helpers
    VALID_DATETIME_STRING = lambda {|str| str.is_a?(String) and [10,25,29].include?(str.length) and str.count("^0-9:TZ+-= ").zero? }

    def deep_decode_datetime(data, zone: DateTime)
      case data
      when nil;    nil
      when VALID_DATETIME_STRING
        res = nil
        begin
          res = zone.parse(data)
        rescue ArgumentError
          data
        end
        [ DateTime, ActiveSupport::TimeWithZone ].include?(res.class) ? res : data
      when Array; data.map!              { |d| deep_decode_datetime(d, zone: zone) }
      when Hash;  data.transform_values! { |v| deep_decode_datetime(v, zone: zone) }
      else;       data
      end
    end

    module_function :deep_decode_datetime
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cotcube-helpers-0.2.4 lib/cotcube-helpers/deep_decode_datetime.rb
cotcube-helpers-0.2.3 lib/cotcube-helpers/deep_decode_datetime.rb
cotcube-helpers-0.2.2.5 lib/cotcube-helpers/deep_decode_datetime.rb