Sha256: a55d9ec9bd2a06886fe8fe98e2d988c8ef8a18e9233a40a7f7f0ed66fe53be69

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 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

    def nil?
      __getobj__.nil?
    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.3 lib/local_date_time_attributes/local_date_time.rb