Sha256: a9c59676ef909b753df95629d99aaef14c38ba7aedc63f0a7d7e10cc1e2d6231

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

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?
      converted_timestamp = ActiveSupport::TimeZone.new(time_zone).local_to_utc(__getobj__)
      converted_timestamp.in_time_zone(time_zone) if converted_timestamp.respond_to? :in_time_zone
    end
    
    private
    
    def from_local(date_time)
      return if date_time.nil?
      converted_timestamp = ActiveSupport::TimeZone.new(active_record_timezone).local_to_utc(date_time)
      converted_timestamp.in_time_zone(active_record_timezone) if converted_timestamp.respond_to? :in_time_zone
    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.2 lib/local_date_time_attributes/local_date_time.rb