Changeset 3657

Show
Ignore:
Timestamp:
12/28/07 09:18:31 (11 months ago)
Author:
walf443
Message:

lang/ruby/date_time-duration: added DateTime::Duration.new2 method.

Files:
1 modified

Legend:

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

    r3656 r3657  
    5050    months / 12 
    5151  end 
     52 
     53  def self.new2 from, hash 
     54    duration = Hash.new(0) 
     55    duration[:months] = ( hash[:years] || 0 ) * 12 + ( hash[:months] || 0 ) 
     56    duration[:days] = Rational(( hash[:weeks] || 0 ) * 7 + ( hash[:days] || 0), 1) 
     57    duration[:seconds] = Rational( 
     58      ( hash[:hours] || 0 ) * 60 * 60 + ( hash[:minutes] || 0 ) * 60 + ( hash[:seconds] || 0 ), 24 * 60 * 60 
     59    ) 
     60 
     61    to = ( from + ( duration[:days] + duration[:seconds] ) ) >> duration[:months] 
     62 
     63    new(from, to) 
     64  end 
    5265end