Skip to content
This repository
Browse code

Merge pull request #11686 from gzohari/pr-refactor-time-datetime

Refactor Date, Time, DateTime timezone methods
  • Loading branch information...
commit a6f568acc60a44c3b01f133517ff61b3b520142b 2 parents 5e9f49a + 3102a9a
Andrew White pixeltrix authored
20 activesupport/lib/active_support/core_ext/date/zones.rb
... ... @@ -1,22 +1,6 @@
1 1 require 'date'
2   -require 'active_support/core_ext/time/zones'
  2 +require 'active_support/core_ext/date_and_time/zones'
3 3
4 4 class Date
5   - # Converts Date to a TimeWithZone in the current zone if Time.zone or Time.zone_default
6   - # is set, otherwise converts Date to a Time via Date#to_time
7   - #
8   - # Time.zone = 'Hawaii' # => 'Hawaii'
9   - # Date.new(2000).in_time_zone # => Sat, 01 Jan 2000 00:00:00 HST -10:00
10   - #
11   - # You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
12   - # and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
13   - #
14   - # Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00
15   - def in_time_zone(zone = ::Time.zone)
16   - if zone
17   - ::Time.find_zone!(zone).local(year, month, day)
18   - else
19   - to_time
20   - end
21   - end
  5 + include DateAndTime::Zones
22 6 end
41 activesupport/lib/active_support/core_ext/date_and_time/zones.rb
... ... @@ -0,0 +1,41 @@
  1 +module DateAndTime
  2 + module Zones
  3 + # Returns the simultaneous time in <tt>Time.zone</tt> if a zone is given or
  4 + # if Time.zone_default is set. Otherwise, it returns the current time.
  5 + #
  6 + # Time.zone = 'Hawaii' # => 'Hawaii'
  7 + # DateTime.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
  8 + # Date.new(2000).in_time_zone # => Sat, 01 Jan 2000 00:00:00 HST -10:00
  9 + #
  10 + # This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone
  11 + # instead of the operating system's time zone.
  12 + #
  13 + # You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
  14 + # and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
  15 + #
  16 + # Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
  17 + # DateTime.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
  18 + # Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00
  19 + def in_time_zone(zone = ::Time.zone)
  20 + time_zone = ::Time.find_zone! zone
  21 + time = acts_like?(:time) ? self : nil
  22 +
  23 + if time_zone
  24 + time_with_zone(time, time_zone)
  25 + else
  26 + time || self.to_time
  27 + end
  28 + end
  29 +
  30 + private
  31 +
  32 + def time_with_zone(time, zone)
  33 + if time
  34 + ActiveSupport::TimeWithZone.new(time.utc? ? time : time.getutc, zone)
  35 + else
  36 + ActiveSupport::TimeWithZone.new(nil, zone, to_time(:utc))
  37 + end
  38 + end
  39 + end
  40 +end
  41 +
23 activesupport/lib/active_support/core_ext/date_time/zones.rb
... ... @@ -1,25 +1,6 @@
1 1 require 'date'
2   -require 'active_support/core_ext/time/zones'
  2 +require 'active_support/core_ext/date_and_time/zones'
3 3
4 4 class DateTime
5   - # Returns the simultaneous time in <tt>Time.zone</tt>.
6   - #
7   - # Time.zone = 'Hawaii' # => 'Hawaii'
8   - # DateTime.new(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
9   - #
10   - # This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt>
11   - # as the local zone instead of the operating system's time zone.
12   - #
13   - # You can also pass in a TimeZone instance or string that identifies a TimeZone
14   - # as an argument, and the conversion will be based on that zone instead of
15   - # <tt>Time.zone</tt>.
16   - #
17   - # DateTime.new(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
18   - def in_time_zone(zone = ::Time.zone)
19   - if zone
20   - ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone))
21   - else
22   - self
23   - end
24   - end
  5 + include DateAndTime::Zones
25 6 end
22 activesupport/lib/active_support/core_ext/time/zones.rb
... ... @@ -1,6 +1,8 @@
1 1 require 'active_support/time_with_zone'
  2 +require 'active_support/core_ext/date_and_time/zones'
2 3
3 4 class Time
  5 + include DateAndTime::Zones
4 6 class << self
5 7 attr_accessor :zone_default
6 8
@@ -73,24 +75,4 @@ def find_zone(time_zone)
73 75 find_zone!(time_zone) rescue nil
74 76 end
75 77 end
76   -
77   - # Returns the simultaneous time in <tt>Time.zone</tt>.
78   - #
79   - # Time.zone = 'Hawaii' # => 'Hawaii'
80   - # Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
81   - #
82   - # This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone
83   - # instead of the operating system's time zone.
84   - #
85   - # You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
86   - # and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
87   - #
88   - # Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
89   - def in_time_zone(zone = ::Time.zone)
90   - if zone
91   - ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone))
92   - else
93   - self
94   - end
95   - end
96 78 end
8 activesupport/test/core_ext/time_with_zone_test.rb
@@ -997,6 +997,14 @@ def test_current_returns_time_zone_now_when_zone_set
997 997 Time.zone = nil
998 998 end
999 999
  1000 + def test_time_in_time_zone_doesnt_affect_receiver
  1001 + with_env_tz 'Europe/London' do
  1002 + time = Time.local(2000, 7, 1)
  1003 + time_with_zone = time.in_time_zone('Eastern Time (US & Canada)')
  1004 + assert_not time.utc?, 'time expected to be local, but is UTC'
  1005 + end
  1006 + end
  1007 +
1000 1008 protected
1001 1009 def with_env_tz(new_tz = 'US/Eastern')
1002 1010 old_tz, ENV['TZ'] = ENV['TZ'], new_tz

0 comments on commit a6f568a

Please sign in to comment.
Something went wrong with that request. Please try again.