Skip to content

Commit d132095

Browse files
committed
Time#change should pass the zone property through if it's set
1 parent 3248812 commit d132095

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* `Time#change` and methods that call it (eg. `Time#advance`) will now
2+
return a `Time` with the timezone argument provided, if the caller was
3+
initialized with a timezone argument.
4+
5+
Fixes [#42467](https://github.com/rails/rails/issues/42467).
6+
7+
*Alex Ghiculescu*
8+
19
* Allow serializing any module or class to JSON by name
210

311
*Tyler Rick*, *Zachary Scott*

activesupport/lib/active_support/core_ext/time/calculations.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ def change(options)
159159
::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec, new_offset)
160160
elsif utc?
161161
::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec)
162+
elsif zone&.respond_to?(:utc_to_local)
163+
::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec, zone)
162164
elsif zone
163165
::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec)
164166
else

activesupport/test/core_ext/time_ext_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,14 @@ def test_advance
483483
assert_equal Time.local(2005, 2, 28, 20, 22, 19), Time.local(2005, 2, 28, 15, 15, 10).advance(hours: 5, minutes: 7, seconds: 9)
484484
assert_equal Time.local(2005, 2, 28, 10, 8, 1), Time.local(2005, 2, 28, 15, 15, 10).advance(hours: -5, minutes: -7, seconds: -9)
485485
assert_equal Time.local(2013, 10, 17, 20, 22, 19), Time.local(2005, 2, 28, 15, 15, 10).advance(years: 7, months: 19, weeks: 2, days: 5, hours: 5, minutes: 7, seconds: 9)
486+
487+
assert_equal Time.new(2021, 5, 29, 0, 0, 0, "+03:00"), Time.new(2021, 5, 29, 0, 0, 0, ActiveSupport::TimeZone["Moscow"])
488+
assert_equal Time.new(2021, 5, 29, 0, 0, 0, "+03:00").advance(seconds: 60), Time.new(2021, 5, 29, 0, 0, 0, ActiveSupport::TimeZone["Moscow"]).advance(seconds: 60)
489+
assert_equal Time.new(2021, 5, 29, 0, 0, 0, "+03:00").advance(days: 3), Time.new(2021, 5, 29, 0, 0, 0, ActiveSupport::TimeZone["Moscow"]).advance(days: 3)
490+
491+
assert_equal Time.new(2021, 5, 29, 0, 0, 0, "+03:00"), ActiveSupport::TimeZone["Moscow"].local(2021, 5, 29, 0, 0, 0)
492+
assert_equal Time.new(2021, 5, 29, 0, 0, 0, "+03:00").advance(seconds: 60), ActiveSupport::TimeZone["Moscow"].local(2021, 5, 29, 0, 0, 0).advance(seconds: 60)
493+
assert_equal Time.new(2021, 5, 29, 0, 0, 0, "+03:00").advance(days: 3), ActiveSupport::TimeZone["Moscow"].local(2021, 5, 29, 0, 0, 0).advance(days: 3)
486494
end
487495

488496
def test_utc_advance

0 commit comments

Comments
 (0)