mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
TimeWithZone#method_missing: send to utc to advance with dst correctness, otherwise send to time. Adding tests for time calculations methods
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9208 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
c4ccca1f84
commit
f4dc834976
3 changed files with 72 additions and 18 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* TimeWithZone#method_missing: send to utc to advance with dst correctness, otherwise send to time. Adding tests for time calculations methods [Geoff Buesing]
|
||||
|
||||
* Add config.active_support.use_standard_json_time_format setting so that Times and Dates export to ISO 8601 dates. [rick]
|
||||
|
||||
* TZInfo: Removing unneeded TimezoneProxy class [Geoff Buesing]
|
||||
|
|
|
@ -144,25 +144,10 @@ module ActiveSupport
|
|||
end
|
||||
end
|
||||
|
||||
%w(asctime day hour min mon sec wday yday year to_date).each do |name|
|
||||
define_method(name) do
|
||||
time.__send__(name)
|
||||
end
|
||||
end
|
||||
alias_method :ctime, :asctime
|
||||
alias_method :mday, :day
|
||||
alias_method :month, :mon
|
||||
|
||||
def usec
|
||||
time.respond_to?(:usec) ? time.usec : 0
|
||||
end
|
||||
|
||||
%w(sunday? monday? tuesday? wednesday? thursday? friday? saturday?).each do |name|
|
||||
define_method(name) do
|
||||
time.__send__(name)
|
||||
end
|
||||
end unless RUBY_VERSION < '1.9'
|
||||
|
||||
def to_a
|
||||
[time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone]
|
||||
end
|
||||
|
@ -219,9 +204,13 @@ module ActiveSupport
|
|||
|
||||
# Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone
|
||||
def method_missing(sym, *args, &block)
|
||||
result = utc.__send__(sym, *args, &block)
|
||||
result = result.in_time_zone(time_zone) if result.acts_like?(:time)
|
||||
result
|
||||
if %w(+ - since ago advance).include?(sym.to_s)
|
||||
result = utc.__send__(sym, *args, &block)
|
||||
result.acts_like?(:time) ? result.in_time_zone(time_zone) : result
|
||||
else
|
||||
result = time.__send__(sym, *args, &block)
|
||||
result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -393,6 +393,69 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
|||
assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f
|
||||
end
|
||||
end
|
||||
|
||||
def test_change
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
|
||||
assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.change(:year => 2001).inspect
|
||||
assert_equal "Wed, 31 Mar 1999 19:00:00 EST -05:00", @twz.change(:month => 3).inspect
|
||||
assert_equal "Wed, 03 Mar 1999 19:00:00 EST -05:00", @twz.change(:month => 2).inspect
|
||||
assert_equal "Wed, 15 Dec 1999 19:00:00 EST -05:00", @twz.change(:day => 15).inspect
|
||||
assert_equal "Fri, 31 Dec 1999 06:00:00 EST -05:00", @twz.change(:hour => 6).inspect
|
||||
assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.change(:min => 15).inspect
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.change(:sec => 30).inspect
|
||||
end
|
||||
|
||||
def test_advance
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
|
||||
assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.advance(:years => 2).inspect
|
||||
assert_equal "Fri, 31 Mar 2000 19:00:00 EST -05:00", @twz.advance(:months => 3).inspect
|
||||
assert_equal "Tue, 04 Jan 2000 19:00:00 EST -05:00", @twz.advance(:days => 4).inspect
|
||||
assert_equal "Sat, 01 Jan 2000 01:00:00 EST -05:00", @twz.advance(:hours => 6).inspect
|
||||
assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.advance(:minutes => 15).inspect
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.advance(:seconds => 30).inspect
|
||||
end
|
||||
|
||||
def beginning_of_year
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
|
||||
assert_equal "Fri, 01 Jan 1999 00:00:00 EST -05:00", @twz.beginning_of_year.inspect
|
||||
end
|
||||
|
||||
def end_of_year
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
|
||||
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_year.inspect
|
||||
end
|
||||
|
||||
def beginning_of_month
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
|
||||
assert_equal "Fri, 01 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_month.inspect
|
||||
end
|
||||
|
||||
def end_of_month
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
|
||||
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_month.inspect
|
||||
end
|
||||
|
||||
def beginning_of_day
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
|
||||
assert_equal "Fri, 31 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_day.inspect
|
||||
end
|
||||
|
||||
def end_of_day
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
|
||||
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect
|
||||
end
|
||||
|
||||
def test_since
|
||||
assert_equal "Fri, 31 Dec 1999 19:00:01 EST -05:00", @twz.since(1).inspect
|
||||
end
|
||||
|
||||
def test_ago
|
||||
assert_equal "Fri, 31 Dec 1999 18:59:59 EST -05:00", @twz.ago(1).inspect
|
||||
end
|
||||
|
||||
def test_seconds_since_midnight
|
||||
assert_equal 19 * 60 * 60, @twz.seconds_since_midnight
|
||||
end
|
||||
end
|
||||
|
||||
class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase
|
||||
|
|
Loading…
Reference in a new issue