Sha256: cb7970e46e7549f401b6f6bf1b143fde9c084ff65623cdea6a3ba75defcdc8eb

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/date_and_time/compatibility"
require "active_support/core_ext/module/redefine_method"

class Time
  include DateAndTime::Compatibility

  silence_redefinition_of_method :to_time

  # Either return +self+ or the time in the local system timezone depending
  # on the setting of +ActiveSupport.to_time_preserves_timezone+.
  def to_time
    preserve_timezone ? self : getlocal
  end

  def preserve_timezone # :nodoc:
    system_local_time? || super
  end

  private
    def system_local_time?
      if ::Time.equal?(self.class)
        zone = self.zone
        String === zone &&
          (zone != "UTC" || active_support_local_zone == "UTC")
      end
    end

    @@active_support_local_tz = nil

    def active_support_local_zone
      @@active_support_local_zone = nil if @@active_support_local_tz != ENV["TZ"]
      @@active_support_local_zone ||=
        begin
          @@active_support_local_tz = ENV["TZ"]
          Time.new.zone
        end
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activesupport-8.0.1 lib/active_support/core_ext/time/compatibility.rb
activesupport-8.0.0.1 lib/active_support/core_ext/time/compatibility.rb
activesupport-8.0.0 lib/active_support/core_ext/time/compatibility.rb