Sha256: 63e9e2b87daae1729beafdb1667037ca57861c9e677e930f5b6826150269107e

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require 'active_support/time'

module ApiResource

  module Typecast

    ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/

    module TimeTypecaster

      def self.from_api(value)
        return value if value.is_a?(Time)
        value = value.to_s
        return nil if value.empty?

        if value =~ ApiResource::Typecast::ISO_DATETIME
          micro = ($7.to_f * 1_000_000).to_i
          return self.new_time(false, $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, micro)
        end

        time_info = Date._parse(value)
        time_info[:micro] = ((time_info[:sec_fraction].to_f % 1) * 1_000_000).to_i

        if time_info[:zone].present?
          new_time(true, *time_info.values_at(:year, :mon, :mday, :hour, :min, :sec, :zone))
        else
          new_time(false, *time_info.values_at(:year, :mon, :mday, :hour, :min, :sec, :micro))
        end

      end

      def self.to_api(value)
        return value.to_s
      end

      protected

      def self.new_time(use_zone, *args)
        year = args.first
        return nil if year.nil? || year == 0
        if use_zone
          Time.new(*args).utc rescue nil
        else
          Time.utc(*args) rescue nil
        end
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api_resource-0.6.22 lib/api_resource/typecasters/time_typecaster.rb