Skip to content

Commit e2edfe1

Browse files
committed
Merge pull request rails#4645 from brainopia/deprecate_datetime_local_offset
Deprecate DateTime.local_offset
2 parents 203771d + b258bec commit e2edfe1

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
require 'active_support/deprecation'
2+
13
class DateTime
24
class << self
3-
# DateTimes aren't aware of DST rules, so use a consistent non-DST offset when creating a DateTime with an offset in the local zone
5+
# *DEPRECATED*: Use +DateTime.civil_from_format+ directly.
46
def local_offset
7+
ActiveSupport::Deprecation.warn 'DateTime.local_offset is deprecated. ' \
8+
'Use DateTime.civil_from_format directly.', caller
59
::Time.local(2012).utc_offset.to_r / 86400
610
end
711

activesupport/lib/active_support/core_ext/date_time/conversions.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,18 @@ def to_time
6464
self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec, sec_fraction * 1000000) : self
6565
end
6666

67+
# Returns DateTime with local offset for given year if format is local else offset is zero
68+
#
69+
# DateTime.civil_from_format :local, 2012
70+
# # => Sun, 01 Jan 2012 00:00:00 +0300
71+
# DateTime.civil_from_format :local, 2012, 12, 17
72+
# # => Mon, 17 Dec 2012 00:00:00 +0000
6773
def self.civil_from_format(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0)
68-
offset = utc_or_local.to_sym == :local ? local_offset : 0
74+
if utc_or_local.to_sym == :local
75+
offset = ::Time.local(year, month, day).utc_offset.to_r / 86400
76+
else
77+
offset = 0
78+
end
6979
civil(year, month, day, hour, min, sec, offset)
7080
end
7181

activesupport/test/core_ext/date_time_ext_test.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def test_to_time
4343
end
4444

4545
def test_civil_from_format
46-
assert_equal DateTime.civil(2010, 5, 4, 0, 0, 0, DateTime.local_offset), DateTime.civil_from_format(:local, 2010, 5, 4)
47-
assert_equal DateTime.civil(2010, 5, 4, 0, 0, 0, 0), DateTime.civil_from_format(:utc, 2010, 5, 4)
46+
assert_equal Time.local(2010, 5, 4, 0, 0, 0), DateTime.civil_from_format(:local, 2010, 5, 4)
47+
assert_equal Time.utc(2010, 5, 4, 0, 0, 0), DateTime.civil_from_format(:utc, 2010, 5, 4)
4848
end
4949

5050
def test_seconds_since_midnight
@@ -331,15 +331,6 @@ def test_acts_like_time
331331
assert DateTime.new.acts_like_time?
332332
end
333333

334-
def test_local_offset
335-
with_env_tz 'US/Eastern' do
336-
assert_equal Rational(-5, 24), DateTime.local_offset
337-
end
338-
with_env_tz 'US/Central' do
339-
assert_equal Rational(-6, 24), DateTime.local_offset
340-
end
341-
end
342-
343334
def test_utc?
344335
assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12).utc?
345336
assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc?

activesupport/test/core_ext/time_ext_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,14 +618,14 @@ def test_time_with_datetime_fallback
618618
assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30)
619619
assert_equal Time.time_with_datetime_fallback(:local, 2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30)
620620
assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0)
621-
assert_equal Time.time_with_datetime_fallback(:local, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, DateTime.local_offset)
621+
assert_equal Time.time_with_datetime_fallback(:local, 2039, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 2039, 2, 21, 17, 44, 30)
622622
assert_equal Time.time_with_datetime_fallback(:utc, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, 0)
623623
assert_equal Time.time_with_datetime_fallback(:utc, 2005), Time.utc(2005)
624624
assert_equal Time.time_with_datetime_fallback(:utc, 2039), DateTime.civil(2039, 1, 1, 0, 0, 0, 0)
625625
assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30, 1), Time.utc(2005, 2, 21, 17, 44, 30, 1) #with usec
626626
# This won't overflow on 64bit linux
627627
unless time_is_64bits?
628-
assert_equal Time.time_with_datetime_fallback(:local, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, DateTime.local_offset, 0)
628+
assert_equal Time.time_with_datetime_fallback(:local, 1900, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 1900, 2, 21, 17, 44, 30)
629629
assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1),
630630
DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0)
631631
assert_equal ::Date::ITALY, Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1).start # use Ruby's default start value
@@ -647,10 +647,10 @@ def test_utc_time
647647

648648
def test_local_time
649649
assert_equal Time.local_time(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30)
650-
assert_equal Time.local_time(2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, DateTime.local_offset)
650+
assert_equal Time.local_time(2039, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 2039, 2, 21, 17, 44, 30)
651651

652652
unless time_is_64bits?
653-
assert_equal Time.local_time(1901, 2, 21, 17, 44, 30), DateTime.civil(1901, 2, 21, 17, 44, 30, DateTime.local_offset)
653+
assert_equal Time.local_time(1901, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 1901, 2, 21, 17, 44, 30)
654654
end
655655
end
656656

0 commit comments

Comments
 (0)