Sha256: 3d7483a6f0a626618fa054ddab20177f5cd01840b2bae9cbc87205ebf411a41f

Contents?: true

Size: 983 Bytes

Versions: 8

Compression:

Stored size: 983 Bytes

Contents

# frozen_string_literal: true

module FreightKit
  # Represent dates and times in varous formats.
  #
  # @attribute date_time_with_zone
  #   Date and time with time zone.
  #   @return [ActiveSupport::TimeWithZone]
  #
  # @attribute local_date
  #   Local date.
  #   @return [Date]
  #
  # @attribute local_date_time
  #   Local date and time in the format `"YYYY-MM-DD HH:MM:SS"`
  #   (zero-padded 24 hour clock) aka `DateTime#to_fs(:db)` format.
  #   @return [String]
  #
  class DateTime < Model
    attr_accessor :local_date, :local_date_time, :location, :date_time_with_zone

    def initialize(*)
      super

      attempt_upgrade_using_location(location) if location.present?
    end

    private

    def attempt_upgrade_using_location(location)
      return if @date_time_with_zone.present? || @local_date_time.blank? || location.time_zone.blank?

      @date_time_with_zone = location.time_zone.parse(@local_date_time)
      @local_date_time = nil
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
freight_kit-0.1.11 lib/freight_kit/models/date_time.rb
freight_kit-0.1.10 lib/freight_kit/models/date_time.rb
freight_kit-0.1.7 lib/freight_kit/models/date_time.rb
freight_kit-0.1.4 lib/freight_kit/models/date_time.rb
freight_kit-0.1.3 lib/freight_kit/models/date_time.rb
freight_kit-0.1.2 lib/freight_kit/models/date_time.rb
freight_kit-0.1.1 lib/freight_kit/models/date_time.rb
freight_kit-0.1.0 lib/freight_kit/models/date_time.rb