Skip to content

Commit

Permalink
Honor Ruby's default calendar reform setting when creating DateTime o…
Browse files Browse the repository at this point in the history
…bjects via ActiveRecord's Time -> DateTime overflow, Time#time_with_datetime_fallback, Time#to_datetime, Date#to_datetime and String#to_datetime. Closes rails#10201

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8199 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
gbuesing committed Nov 24, 2007
1 parent d84846a commit becdb49
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* DateTimes use Ruby's default calendar reform setting. #10201 [Geoff Buesing]

* Dynamic finders on association collections respect association :order and :limit. #10211, #10227 [Patrick Joyce, Rick Olson, Jack Danger Canty]

* Add 'foxy' support for fixtures of polymorphic associations. #10183 [jbarnette, David Lowenfels]
Expand Down
Expand Up @@ -174,8 +174,7 @@ def new_time(year, mon, mday, hour, min, sec, microsec)
# Over/underflow to DateTime
rescue ArgumentError, TypeError
zone_offset = Base.default_timezone == :local ? DateTime.local_offset : 0
# Append zero calendar reform start to account for dates skipped by calendar reform
DateTime.new(year, mon, mday, hour, min, sec, zone_offset, 0) rescue nil
DateTime.civil(year, mon, mday, hour, min, sec, zone_offset) rescue nil
end

def fast_string_to_date(string)
Expand Down
3 changes: 2 additions & 1 deletion activerecord/test/migration_test.rb
Expand Up @@ -286,7 +286,7 @@ def test_native_types
:bio => "I was born ....", :age => 18, :height => 1.78,
:wealth => BigDecimal.new("12345678901234567890.0123456789"),
:birthday => 18.years.ago, :favorite_day => 10.days.ago,
:moment_of_truth => "1582-10-10 21:40:18", :male => true
:moment_of_truth => "1782-10-10 21:40:18", :male => true
end

bob = Person.find(:first)
Expand Down Expand Up @@ -323,6 +323,7 @@ def test_native_types
assert_equal DateTime.now.offset, bob.moment_of_truth.offset
assert_not_equal 0, bob.moment_of_truth.offset
assert_not_equal "Z", bob.moment_of_truth.zone
assert_equal DateTime::ITALY, bob.moment_of_truth.start
end

assert_equal TrueClass, bob.male?.class
Expand Down
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Time#time_with_datetime_fallback, Time#to_datetime, Date#to_datetime and String#to_datetime honor Ruby's default calendar reform setting. #10201 [Geoff Buesing]

* Change Time and DateTime #end_of_month to return last second of month instead of beginning of last day of month. Closes #10200 [Geoff Buesing]

* Speedup String#blank? [Jeremy Kemper, Koz]
Expand Down
Expand Up @@ -53,7 +53,7 @@ def to_time(form = :local)

# Converts self to a Ruby DateTime object; time is set to beginning of day
def to_datetime
::DateTime.civil(year, month, day, 0, 0, 0, 0, 0)
::DateTime.civil(year, month, day, 0, 0, 0, 0)
end if RUBY_VERSION < '1.9'

def xmlschema
Expand Down
Expand Up @@ -15,7 +15,7 @@ def to_date
end

def to_datetime
::DateTime.civil(*ParseDate.parsedate(self)[0..5].map {|arg| arg || 0} << 0 << 0)
::DateTime.civil(*ParseDate.parsedate(self)[0..5].map {|arg| arg || 0} << 0)
end
end
end
Expand Down
Expand Up @@ -36,7 +36,7 @@ def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=
::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
rescue
offset = utc_or_local.to_sym == :local ? ::DateTime.local_offset : 0
::DateTime.civil(year, month, day, hour, min, sec, offset, 0)
::DateTime.civil(year, month, day, hour, min, sec, offset)
end

# wraps class method time_with_datetime_fallback with utc_or_local == :utc
Expand Down
Expand Up @@ -43,7 +43,7 @@ def to_time

# converts to a Ruby DateTime instance; preserves utc offset
def to_datetime
::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400), 0)
::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400))
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions activesupport/test/core_ext/date_ext_test.rb
Expand Up @@ -23,6 +23,8 @@ def test_to_time

def test_to_datetime
assert_equal DateTime.civil(2005, 2, 21), Date.new(2005, 2, 21).to_datetime
assert_equal 0, Date.new(2005, 2, 21).to_datetime.offset # use UTC offset
assert_equal ::Date::ITALY, Date.new(2005, 2, 21).to_datetime.start # use Ruby's default start value
end

def test_to_date
Expand Down
10 changes: 9 additions & 1 deletion activesupport/test/core_ext/string_ext_test.rb
Expand Up @@ -83,8 +83,16 @@ def test_string_to_time
assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".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 Date.new(2005, 2, 27), "2005-02-27".to_date
end

def test_string_to_datetime
assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value
end

def test_string_to_date
assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
end

def test_access
Expand Down
2 changes: 2 additions & 0 deletions activesupport/test/core_ext/time_ext_test.rb
Expand Up @@ -334,6 +334,7 @@ def test_to_datetime
with_timezone 'NZ' do
assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400), 0)
end
assert_equal ::Date::ITALY, Time.utc(2005, 2, 21, 17, 44, 30).to_datetime.start # use Ruby's default start value
end

def test_to_time
Expand Down Expand Up @@ -378,6 +379,7 @@ def test_time_with_datetime_fallback
assert_equal Time.time_with_datetime_fallback(:utc, 2039), DateTime.civil(2039, 1, 1, 0, 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
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

def test_utc_time
Expand Down

0 comments on commit becdb49

Please sign in to comment.