Class: Timeframe::Iso8601::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/timeframe/iso_8601.rb

Overview

Internal use.

Parses a duration like ‘P1Y2M4DT3H4M2S’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date_part, time_part) ⇒ Duration

Returns a new instance of Duration.



8
9
10
11
12
13
14
15
16
# File 'lib/timeframe/iso_8601.rb', line 8

def initialize(date_part, time_part)
  y       = parse date_part, :Y
  m       = parse date_part, :M
  d       = parse date_part, :D
  h       = parse time_part, :H
  minutes = parse time_part, :M
  s       = parse time_part, :S
  @seconds = (y*31_556_926 + m*2_629_743.83 + d*86_400 + h*3_600 + minutes*60 + s).ceil
end

Instance Attribute Details

#secondsObject (readonly)

Returns the value of attribute seconds.



7
8
9
# File 'lib/timeframe/iso_8601.rb', line 7

def seconds
  @seconds
end

Instance Method Details

#parse(part, indicator) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/timeframe/iso_8601.rb', line 17

def parse(part, indicator)
  if part =~ /(\d+)#{indicator.to_s}/
    $1.to_f
  else
    0
  end
end