Class: DateTime::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/date_time/duration.rb

Overview

dur.years.to_i # => 22

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Duration

Returns a new instance of Duration.



8
9
10
11
12
# File 'lib/date_time/duration.rb', line 8

def initialize from, to
  @flag = ( from <= to ) ? 1 : -1
  @from, @to = ( from <= to ) ? [from, to] : [ to, from ]
  @duration = to.ajd - from.ajd
end

Class Method Details

.new2(from, hash) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/date_time/duration.rb', line 57

def self.new2 from, hash
  duration = Hash.new(0)
  duration[:months] = ( hash[:years] || 0 ) * 12 + ( hash[:months] || 0 )
  duration[:days] = Rational(( hash[:weeks] || 0 ) * 7 + ( hash[:days] || 0), 1)
  duration[:seconds] = Rational(
    ( hash[:hours] || 0 ) * 60 * 60 + ( hash[:minutes] || 0 ) * 60 + ( hash[:seconds] || 0 ), 24 * 60 * 60
  )

  to = ( ( from + ( duration[:days] + duration[:seconds] ) ) >> duration[:months] )

  new(from, to)
end

Instance Method Details

#==(anothor) ⇒ Object



14
15
16
# File 'lib/date_time/duration.rb', line 14

def == anothor
  ( ( self.days == anothor.days ) && ( self.months == anothor.months ) )
end

#daysObject



31
32
33
# File 'lib/date_time/duration.rb', line 31

def days
  @duration
end

#hoursObject



27
28
29
# File 'lib/date_time/duration.rb', line 27

def hours
  @duration * 24
end

#minutesObject



23
24
25
# File 'lib/date_time/duration.rb', line 23

def minutes
  @duration * 24 * 60
end

#monthsObject

This method return not Rational but Fixnum.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/date_time/duration.rb', line 40

def months
  if @months.nil?
    i = 0
    while ( ( @from >> i ) <= @to ) do
      i += 1
    end

    @months = ( @flag * ( i - 1 ) )
  end
  
  return @months
end

#secondsObject

It doesn’t care about leap seconds.



19
20
21
# File 'lib/date_time/duration.rb', line 19

def seconds
  @duration * 24 * 60 * 60 
end

#weeksObject



35
36
37
# File 'lib/date_time/duration.rb', line 35

def weeks
  @duration / 7
end

#yearsObject



53
54
55
# File 'lib/date_time/duration.rb', line 53

def years
  months / 12
end