Class: TimeInterval::Duration

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0) ⇒ Duration

Returns a new instance of Duration.



34
35
36
37
38
39
40
41
42
43
# File 'lib/time_interval/duration.rb', line 34

def initialize(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0,
  seconds: 0)
  @years = years
  @months = months
  @weeks = weeks
  @days = days
  @hours = hours
  @minutes = minutes
  @seconds = seconds
end

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



32
33
34
# File 'lib/time_interval/duration.rb', line 32

def days
  @days
end

#hoursObject (readonly)

Returns the value of attribute hours.



32
33
34
# File 'lib/time_interval/duration.rb', line 32

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



32
33
34
# File 'lib/time_interval/duration.rb', line 32

def minutes
  @minutes
end

#monthsObject (readonly)

Returns the value of attribute months.



32
33
34
# File 'lib/time_interval/duration.rb', line 32

def months
  @months
end

#secondsObject (readonly)

Returns the value of attribute seconds.



32
33
34
# File 'lib/time_interval/duration.rb', line 32

def seconds
  @seconds
end

#weeksObject (readonly)

Returns the value of attribute weeks.



32
33
34
# File 'lib/time_interval/duration.rb', line 32

def weeks
  @weeks
end

#yearsObject (readonly)

Returns the value of attribute years.



32
33
34
# File 'lib/time_interval/duration.rb', line 32

def years
  @years
end

Class Method Details

.parse(duration_str) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/time_interval/duration.rb', line 3

def self.parse(duration_str)
  if duration_str['PT']
    date_str = nil
    time_str = duration_str
  else
    date_str, time_str = duration_str.split('T')
  end

  if date_str
    y = date_str['Y'] ? date_str.match(/(\d+)Y/)[1].to_i : 0
    m = date_str['M'] ? date_str.match(/(\d+)M/)[1].to_i : 0
    w = date_str['W'] ? date_str.match(/(\d+)W/)[1].to_i : 0
    d = date_str['D'] ? date_str.match(/(\d+)D/)[1].to_i : 0
  else
    y = m = w = d = 0
  end

  if time_str
    h = time_str['H'] ? time_str.match(/(\d+)H/)[1].to_i : 0
    mi = time_str['M'] ? time_str.match(/(\d+)M/)[1].to_i : 0
    s = time_str['S'] ? time_str.match(/(\d+)S/)[1].to_i : 0
  else
    h = mi = s = 0
  end

  new(years: y, months: m, weeks: w, days: d, hours: h, minutes: mi,
      seconds: s)
end

Instance Method Details

#add_to(time) ⇒ Object



45
46
47
# File 'lib/time_interval/duration.rb', line 45

def add_to(time)
  time + total_seconds
end

#iso8601Object



49
50
51
52
53
54
# File 'lib/time_interval/duration.rb', line 49

def iso8601
  string = 'P' + iso8601_date
  string += iso8601_time unless iso8601_time == 'T'

  string
end

#present?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/time_interval/duration.rb', line 60

def present?
  total_seconds > 0
end

#subtract_from(time) ⇒ Object



56
57
58
# File 'lib/time_interval/duration.rb', line 56

def subtract_from(time)
  time - total_seconds
end

#total_secondsObject



64
65
66
67
# File 'lib/time_interval/duration.rb', line 64

def total_seconds
  years.years + months.months + weeks.weeks + days.days + hours.hours +
    minutes.minutes + seconds.seconds
end