Changeset 3645

Show
Ignore:
Timestamp:
12/28/07 01:39:24 (11 months ago)
Author:
walf443
Message:

lang/ruby/date_time-duration: supported nagative duration.

Location:
lang/ruby/date_time-duration
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/date_time-duration/lib/date_time/duration.rb

    r3644 r3645  
    77class DateTime::Duration 
    88  def initialize from, to 
    9     @from = from 
    10     @to   = to 
     9    @flag = ( from <= to ) ? 1 : -1 
     10    @from, @to = ( from <= to ) ? [from, to] : [ to, from ] 
    1111    @duration = to - from 
    1212  end 
     
    3535  # This method return not Rational but Fixnum. 
    3636  def months 
    37     i = 0 
    38     while ( ( @from >> i ) <= @to ) do 
    39       i += 1 
     37    if @months.nil? 
     38      i = 0 
     39      while ( ( @from >> i ) <= @to ) do 
     40        i += 1 
     41      end 
     42 
     43      @months = ( @flag * ( i - 1 ) ) 
    4044    end 
    41  
    42     return i - 1 
     45     
     46    return @months 
    4347  end 
    4448 
  • lang/ruby/date_time-duration/spec/date_time-duration_spec.rb

    r3644 r3645  
    1414    @dd3 = DateTime::Duration.new(@birth, @birth_after_a_year_before_one_day) 
    1515    @dd4 = DateTime::Duration.new(@birth, @birth_after_a_year ) 
     16    @dd5 = DateTime::Duration.new(@birth_after_a_year, @birth) 
    1617  end 
    1718 
     
    2122    @dd3.days.to_i.should == 364 
    2223    @dd4.days.to_i.should == 365 
     24    @dd5.days.to_i.should == -365 
    2325  end 
    2426 
     
    2830    @dd3.months.to_i.should == 11 
    2931    @dd4.months.to_i.should == 12 
     32    @dd5.months.to_i.should == -12 
    3033  end 
    3134 
     
    3538    @dd3.years.to_i.should == 0 
    3639    @dd4.years.to_i.should == 1 
     40    @dd5.years.to_i.should == -1 
    3741  end 
    3842