mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Deprecate obsolete Time to DateTime fallback methods
The Time.time_with_datetime_fallback, Time.utc_time and Time.local_time methods were added to handle the limitations of Ruby's native Time implementation. Those limitations no longer apply so we are deprecating them in 4.0 and they will be removed in 4.1.
This commit is contained in:
parent
45a6f546b6
commit
48583f8bf7
13 changed files with 67 additions and 38 deletions
|
@ -132,7 +132,7 @@ module ActiveRecord
|
|||
if object.class.send(:create_time_zone_conversion_attribute?, name, column)
|
||||
Time.zone.local(*set_values)
|
||||
else
|
||||
Time.time_with_datetime_fallback(object.class.default_timezone, *set_values)
|
||||
Time.send(object.class.default_timezone, *set_values)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -240,7 +240,7 @@ module ActiveRecord
|
|||
# Treat 0000-00-00 00:00:00 as nil.
|
||||
return nil if year.nil? || (year == 0 && mon == 0 && mday == 0)
|
||||
|
||||
Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
|
||||
Time.send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
|
||||
end
|
||||
|
||||
def fast_string_to_date(string)
|
||||
|
|
|
@ -8,15 +8,15 @@ class DateTimeTest < ActiveRecord::TestCase
|
|||
with_active_record_default_timezone :utc do
|
||||
time_values = [1807, 2, 10, 15, 30, 45]
|
||||
# create DateTime value with local time zone offset
|
||||
local_offset = Rational(Time.local_time(*time_values).utc_offset, 86400)
|
||||
local_offset = Rational(Time.local(*time_values).utc_offset, 86400)
|
||||
now = DateTime.civil(*(time_values + [local_offset]))
|
||||
|
||||
task = Task.new
|
||||
task.starting = now
|
||||
task.save!
|
||||
|
||||
# check against Time.local_time, since some platforms will return a Time instead of a DateTime
|
||||
assert_equal Time.local_time(*time_values), Task.find(task.id).starting
|
||||
# check against Time.local, since some platforms will return a Time instead of a DateTime
|
||||
assert_equal Time.local(*time_values), Task.find(task.id).starting
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
## Rails 4.0.0 (unreleased) ##
|
||||
|
||||
* Deprecate `Time.time_with_date_fallback`, `Time.utc_time` and `Time.local_time`.
|
||||
These methods were added to handle the limited range of Ruby's native Time
|
||||
implementation. Those limitations no longer apply so we are deprecating them in 4.0
|
||||
and they will be removed in 4.1.
|
||||
|
||||
*Andrew White*
|
||||
|
||||
* Deprecate `Date#to_time_in_current_zone` and add `Date#in_time_zone`. *Andrew White*
|
||||
|
||||
* Add `String#in_time_zone` method to convert a string to an ActiveSupport::TimeWithZone. *Andrew White*
|
||||
|
|
|
@ -75,7 +75,7 @@ class Date
|
|||
#
|
||||
# date.to_time(:utc) # => Sat Nov 10 00:00:00 UTC 2007
|
||||
def to_time(form = :local)
|
||||
::Time.send("#{form}_time", year, month, day)
|
||||
::Time.send(form, year, month, day)
|
||||
end
|
||||
|
||||
def xmlschema
|
||||
|
|
|
@ -21,7 +21,7 @@ class String
|
|||
date_values[6] *= 1000000
|
||||
offset = date_values.pop
|
||||
|
||||
::Time.send("#{form}_time", *date_values) - offset
|
||||
::Time.send(form, *date_values) - offset
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ require 'active_support/core_ext/time/conversions'
|
|||
require 'active_support/time_with_zone'
|
||||
require 'active_support/core_ext/time/zones'
|
||||
require 'active_support/core_ext/date_and_time/calculations'
|
||||
require 'active_support/deprecation'
|
||||
|
||||
class Time
|
||||
include DateAndTime::Calculations
|
||||
|
@ -25,10 +26,13 @@ class Time
|
|||
end
|
||||
end
|
||||
|
||||
# *DEPRECATED*: Use +Time#utc+ or +Time#local+ instead.
|
||||
#
|
||||
# Returns a new Time if requested year can be accommodated by Ruby's Time class
|
||||
# (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture);
|
||||
# otherwise returns a DateTime.
|
||||
def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
|
||||
ActiveSupport::Deprecation.warn 'time_with_datetime_fallback is deprecated. Use Time#utc or Time#local instead', caller
|
||||
time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
|
||||
|
||||
# This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138.
|
||||
|
@ -41,13 +45,19 @@ class Time
|
|||
::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
|
||||
end
|
||||
|
||||
# *DEPRECATED*: Use +Time#utc+ instead.
|
||||
#
|
||||
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>.
|
||||
def utc_time(*args)
|
||||
ActiveSupport::Deprecation.warn 'utc_time is deprecated. Use Time#utc instead', caller
|
||||
time_with_datetime_fallback(:utc, *args)
|
||||
end
|
||||
|
||||
# *DEPRECATED*: Use +Time#local+ instead.
|
||||
#
|
||||
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:local</tt>.
|
||||
def local_time(*args)
|
||||
ActiveSupport::Deprecation.warn 'local_time is deprecated. Use Time#local instead', caller
|
||||
time_with_datetime_fallback(:local, *args)
|
||||
end
|
||||
|
||||
|
|
|
@ -344,7 +344,7 @@ module ActiveSupport
|
|||
end
|
||||
|
||||
def transfer_time_values_to_utc_constructor(time)
|
||||
::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:nsec) ? Rational(time.nsec, 1000) : 0)
|
||||
::Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec, Rational(time.nsec, 1000))
|
||||
end
|
||||
|
||||
def duration_of_variable_length?(obj)
|
||||
|
|
|
@ -252,7 +252,7 @@ module ActiveSupport
|
|||
# Time.zone = 'Hawaii' # => "Hawaii"
|
||||
# Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
|
||||
def local(*args)
|
||||
time = Time.utc_time(*args)
|
||||
time = Time.utc(*args)
|
||||
ActiveSupport::TimeWithZone.new(nil, self, time)
|
||||
end
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
|
|||
|
||||
def test_to_time
|
||||
assert_equal Time.local(2005, 2, 21), Date.new(2005, 2, 21).to_time
|
||||
assert_equal Time.local_time(2039, 2, 21), Date.new(2039, 2, 21).to_time
|
||||
assert_equal Time.local(2039, 2, 21), Date.new(2039, 2, 21).to_time
|
||||
silence_warnings do
|
||||
0.upto(138) do |year|
|
||||
[:utc, :local].each do |format|
|
||||
|
|
|
@ -42,7 +42,7 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
|
||||
def test_to_time
|
||||
assert_equal Time.utc(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time
|
||||
assert_equal Time.utc_time(2039, 2, 21, 10, 11, 12), DateTime.new(2039, 2, 21, 10, 11, 12, 0).to_time
|
||||
assert_equal Time.utc(2039, 2, 21, 10, 11, 12), DateTime.new(2039, 2, 21, 10, 11, 12, 0).to_time
|
||||
# DateTimes with offsets other than 0 are returned unaltered
|
||||
assert_equal DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)), DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).to_time
|
||||
# Fractional seconds are preserved
|
||||
|
|
|
@ -291,7 +291,7 @@ class StringConversionsTest < ActiveSupport::TestCase
|
|||
assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time
|
||||
assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local)
|
||||
assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
|
||||
assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
|
||||
assert_equal Time.local(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
|
||||
assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time
|
||||
assert_nil "".to_time
|
||||
end
|
||||
|
|
|
@ -567,45 +567,57 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
def test_time_with_datetime_fallback
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30)
|
||||
assert_equal Time.time_with_datetime_fallback(:local, 2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0)
|
||||
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)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, 0)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2005), Time.utc(2005)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2039), DateTime.civil(2039, 1, 1, 0, 0, 0, 0)
|
||||
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
|
||||
# This won't overflow on 64bit linux
|
||||
unless time_is_64bits?
|
||||
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)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1),
|
||||
DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0)
|
||||
assert_equal ::Date::ITALY, Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1).start # use Ruby's default start value
|
||||
end
|
||||
silence_warnings do
|
||||
0.upto(138) do |year|
|
||||
[:utc, :local].each do |format|
|
||||
assert_equal year, Time.time_with_datetime_fallback(format, year).year
|
||||
ActiveSupport::Deprecation.silence do
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30)
|
||||
assert_equal Time.time_with_datetime_fallback(:local, 2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0)
|
||||
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)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, 0)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2005), Time.utc(2005)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2039), DateTime.civil(2039, 1, 1, 0, 0, 0, 0)
|
||||
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
|
||||
# This won't overflow on 64bit linux
|
||||
unless time_is_64bits?
|
||||
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)
|
||||
assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1),
|
||||
DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0)
|
||||
assert_equal ::Date::ITALY, Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1).start # use Ruby's default start value
|
||||
end
|
||||
silence_warnings do
|
||||
0.upto(138) do |year|
|
||||
[:utc, :local].each do |format|
|
||||
assert_equal year, Time.time_with_datetime_fallback(format, year).year
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_utc_time
|
||||
assert_equal Time.utc_time(2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30)
|
||||
assert_equal Time.utc_time(2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0)
|
||||
assert_equal Time.utc_time(1901, 2, 21, 17, 44, 30), DateTime.civil(1901, 2, 21, 17, 44, 30, 0)
|
||||
ActiveSupport::Deprecation.silence do
|
||||
assert_equal Time.utc_time(2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30)
|
||||
assert_equal Time.utc_time(2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0)
|
||||
assert_equal Time.utc_time(1901, 2, 21, 17, 44, 30), DateTime.civil(1901, 2, 21, 17, 44, 30, 0)
|
||||
end
|
||||
end
|
||||
|
||||
def test_local_time
|
||||
assert_equal Time.local_time(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30)
|
||||
assert_equal Time.local_time(2039, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 2039, 2, 21, 17, 44, 30)
|
||||
ActiveSupport::Deprecation.silence do
|
||||
assert_equal Time.local_time(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30)
|
||||
assert_equal Time.local_time(2039, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 2039, 2, 21, 17, 44, 30)
|
||||
|
||||
unless time_is_64bits?
|
||||
assert_equal Time.local_time(1901, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 1901, 2, 21, 17, 44, 30)
|
||||
unless time_is_64bits?
|
||||
assert_equal Time.local_time(1901, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 1901, 2, 21, 17, 44, 30)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_time_with_datetime_fallback_deprecations
|
||||
assert_deprecated(/time_with_datetime_fallback/) { Time.time_with_datetime_fallback(:utc, 2012, 6, 7) }
|
||||
assert_deprecated(/utc_time/) { Time.utc_time(2012, 6, 7) }
|
||||
assert_deprecated(/local_time/) { Time.local_time(2012, 6, 7) }
|
||||
end
|
||||
|
||||
def test_last_month_on_31st
|
||||
assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue