Skip to content

Commit

Permalink
TimeWithZone #+ and #- behave consistently with numeric arguments reg…
Browse files Browse the repository at this point in the history
…ardless of whether wrapped time is a Time or DateTime; consistenty answers false to #acts_like?(:date)

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8884 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
gbuesing committed Feb 16, 2008
1 parent 3025ff2 commit e7ebd65
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* TimeWithZone #+ and #- behave consistently with numeric arguments regardless of whether wrapped time is a Time or DateTime; consistenty answers false to #acts_like?(:date) [Geoff Buesing]

* Add String#squish and String#squish! to remove consecutive chunks of whitespace. #11123 [jordi, Henrik N] * Add String#squish and String#squish! to remove consecutive chunks of whitespace. #11123 [jordi, Henrik N]


* Serialize BigDecimals as Floats when using to_yaml. #8746 [ernesto.jimenez] * Serialize BigDecimals as Floats when using to_yaml. #8746 [ernesto.jimenez]
Expand Down
17 changes: 15 additions & 2 deletions activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -129,10 +129,21 @@ def eql?(other)
utc == other utc == other
end end


# Need to override #- to intercept situation where a Time or Time With Zone object is passed in # If wrapped #time is a DateTime, use DateTime#since instead of #+
# Otherwise, just pass on to #method_missing
def +(other)
time.acts_like?(:date) ? method_missing(:since, other) : method_missing(:+, other)
end

# If a time-like object is passed in, compare it with #utc
# Else if wrapped #time is a DateTime, use DateTime#ago instead of #-
# Otherwise, just pass on to method missing # Otherwise, just pass on to method missing
def -(other) def -(other)
other.acts_like?(:time) ? utc - other : method_missing(:-, other) if other.acts_like?(:time)
utc - other
else
time.acts_like?(:date) ? method_missing(:ago, other) : method_missing(:-, other)
end
end end


def to_a def to_a
Expand Down Expand Up @@ -176,6 +187,8 @@ def freeze


# Ensure proxy class responds to all methods that underlying time instance responds to # Ensure proxy class responds to all methods that underlying time instance responds to
def respond_to?(sym) def respond_to?(sym)
# consistently respond false to acts_like?(:date), regardless of whether #time is a Time or DateTime
return false if sym.to_s == 'acts_like_date?'
super || time.respond_to?(sym) super || time.respond_to?(sym)
end end


Expand Down
22 changes: 20 additions & 2 deletions activesupport/test/core_ext/time_with_zone_test.rb
Expand Up @@ -121,17 +121,29 @@ def test_eql?
assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone["Hawaii"]) ) assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone["Hawaii"]) )
end end


def test_plus def test_plus_with_integer
assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time
end end


def test_plus_with_integer_when_self_wraps_datetime
datetime = DateTime.civil(2000, 1, 1, 0)
twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time
end

def test_plus_with_duration def test_plus_with_duration
assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time
end end


def test_minus def test_minus_with_integer
assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time
end end

def test_minus_with_integer_when_self_wraps_datetime
datetime = DateTime.civil(2000, 1, 1, 0)
twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time
end


def test_minus_with_duration def test_minus_with_duration
assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time
Expand Down Expand Up @@ -174,6 +186,12 @@ def test_to_datetime


def test_acts_like_time def test_acts_like_time
assert @twz.acts_like?(:time) assert @twz.acts_like?(:time)
assert ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:time)
end

def test_acts_like_date
assert_equal false, @twz.acts_like?(:date)
assert_equal false, ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:date)
end end


def test_is_a def test_is_a
Expand Down

0 comments on commit e7ebd65

Please sign in to comment.