Sha256: e9c79ad9755a91401ba2f3581885f9b48e133459986e603dfdfd5ab8910f7911

Contents?: true

Size: 888 Bytes

Versions: 1

Compression:

Stored size: 888 Bytes

Contents

require 'delegate'
require 'active_support'
require 'active_support/time'
require 'active_support/time_with_zone'
module LocalDateTimeAttributes
  class LocalDateTime < SimpleDelegator
    def initialize(date_time)
      super(from_local(date_time))
    end

    # Returns a datetime in the timezone specified without changing the time
    def to_local(time_zone = Time.zone.try(:name))
      return if __getobj__.nil? || time_zone.nil?
      ActiveSupport::TimeZone.new(time_zone).local_to_utc(__getobj__).in_time_zone(time_zone)
    end
    
    private
    
    def from_local(date_time)
      return if date_time.nil?
      ActiveSupport::TimeZone.new(active_record_timezone).local_to_utc(date_time).in_time_zone(active_record_timezone)
    end

    def active_record_timezone
      ActiveRecord::Base.default_timezone == :UTC ? 'UTC' : Rails.configuration.time_zone
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
local_date_time_attributes-0.1.1 lib/local_date_time_attributes/local_date_time.rb