From e9425abe33924623b1dce62bd817eace757c2b4e Mon Sep 17 00:00:00 2001 From: Phil Ross Date: Fri, 29 Dec 2017 12:47:10 +0000 Subject: [PATCH] Update to TZInfo v2.0.0 Co-authored-by: Jared Beck Co-authored-by: Jonathan Hefner --- time_with_zone.rb | 20 +++++++++++++++----- values/time_zone.rb | 14 ++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 3be5f6f7b595..9a4c33ab0f19 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -57,12 +57,12 @@ def initialize(utc_time, time_zone, local_time = nil, period = nil) # Returns a Time instance that represents the time in +time_zone+. def time - @time ||= period.to_local(@utc) + @time ||= incorporate_utc_offset(@utc, utc_offset) end # Returns a Time instance of the simultaneous time in the UTC timezone. def utc - @utc ||= period.to_utc(@time) + @utc ||= incorporate_utc_offset(@time, -utc_offset) end alias_method :comparable_time, :utc alias_method :getgm, :utc @@ -104,13 +104,13 @@ def dst? # Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)' # Time.zone.now.utc? # => false def utc? - period.offset.abbreviation == :UTC || period.offset.abbreviation == :UCT + zone == "UTC" || zone == "UCT" end alias_method :gmt?, :utc? # Returns the offset from current time to UTC time in seconds. def utc_offset - period.utc_total_offset + period.observed_utc_offset end alias_method :gmt_offset, :utc_offset alias_method :gmtoff, :utc_offset @@ -132,7 +132,7 @@ def formatted_offset(colon = true, alternate_utc_string = nil) # Time.zone = 'Eastern Time (US & Canada)' # => "Eastern Time (US & Canada)" # Time.zone.now.zone # => "EST" def zone - period.zone_identifier.to_s + period.abbreviation end # Returns a string of the object's date, time, zone, and offset from UTC. @@ -514,6 +514,16 @@ def method_missing(sym, *args, &block) end private + SECONDS_PER_DAY = 86400 + + def incorporate_utc_offset(time, offset) + if time.kind_of?(Date) + time + Rational(offset, SECONDS_PER_DAY) + else + time + offset + end + end + def get_period_and_ensure_valid_local_time(period) # we don't want a Time.local instance enforcing its own DST rules as well, # so transfer time values to a utc constructor if necessary diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 90830b89bda3..2e5d9d3e9d4c 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -203,7 +203,7 @@ def seconds_to_utc_offset(seconds, colon = true) end def find_tzinfo(name) - TZInfo::Timezone.new(MAPPING[name] || name) + TZInfo::Timezone.get(MAPPING[name] || name) end alias_method :create, :new @@ -273,7 +273,7 @@ def load_country_zones(code) memo end else - create(tz_id, nil, TZInfo::Timezone.new(tz_id)) + create(tz_id, nil, TZInfo::Timezone.get(tz_id)) end end.flatten(1).sort! end @@ -302,11 +302,7 @@ def initialize(name, utc_offset = nil, tzinfo = nil) # Returns the offset of this time zone from UTC in seconds. def utc_offset - if @utc_offset - @utc_offset - else - tzinfo.current_period.utc_offset if tzinfo && tzinfo.current_period - end + @utc_offset || tzinfo&.current_period&.base_utc_offset end # Returns a formatted string of the offset from UTC, or an alternative @@ -498,7 +494,9 @@ def yesterday # represented by +self+. Returns a Time.utc() instance -- if you want an # ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead. def utc_to_local(time) - tzinfo.utc_to_local(time) + tzinfo.utc_to_local(time).yield_self do |t| + Time.utc(t.year, t.month, t.day, t.hour, t.min, t.sec, t.sec_fraction) + end end # Adjust the given time to the simultaneous time in UTC. Returns a