Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source
Show
Ignore:
Timestamp:
04/02/08 06:56:44 (1 month ago)
Author:
gbuesing
Message:

TimeWithZone#method_missing: send to utc to advance with dst correctness, otherwise send to time. Adding tests for time calculations methods

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/time_with_zone.rb

    r9149 r9208  
    145145    end 
    146146     
    147     %w(asctime day hour min mon sec wday yday year to_date).each do |name| 
    148       define_method(name) do 
    149         time.__send__(name) 
    150       end 
    151     end 
    152     alias_method :ctime, :asctime 
    153     alias_method :mday, :day 
    154     alias_method :month, :mon 
    155      
    156147    def usec 
    157148      time.respond_to?(:usec) ? time.usec : 0 
    158149    end 
    159      
    160     %w(sunday? monday? tuesday? wednesday? thursday? friday? saturday?).each do |name| 
    161       define_method(name) do 
    162         time.__send__(name) 
    163       end 
    164     end unless RUBY_VERSION < '1.9' 
    165150     
    166151    def to_a 
     
    220205    # Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone 
    221206    def method_missing(sym, *args, &block) 
    222       result = utc.__send__(sym, *args, &block) 
    223       result = result.in_time_zone(time_zone) if result.acts_like?(:time) 
    224       result 
     207      if %w(+ - since ago advance).include?(sym.to_s) 
     208        result = utc.__send__(sym, *args, &block) 
     209        result.acts_like?(:time) ? result.in_time_zone(time_zone) : result 
     210      else 
     211        result = time.__send__(sym, *args, &block) 
     212        result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result 
     213      end 
    225214    end 
    226215