Class: Vpim::Duration

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

Constant Summary collapse

SECS_HOUR =
60 * 60
SECS_DAY =
24 * SECS_HOUR
MINS_HOUR =
60

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secs) ⇒ Duration

Initialize from a number of seconds.



17
18
19
# File 'lib/vpim/duration.rb', line 17

def initialize(secs)
  @secs = secs
end

Class Method Details

.as_str(arr) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vpim/duration.rb', line 73

def Duration.as_str(arr)
  s = ""
  case arr.length
    when 4
      if arr[0] > 0
        s << "#{arr[0]} days"
      end
      if arr[1] > 0
        if s.length > 0
          s << ', '
        end
        s << "#{arr[1]} hours"
      end
      if arr[2] > 0
        if s.length > 0
          s << ', '
        end
        s << "#{arr[2]} mins"
      end
      if arr[3] > 0
        if s.length > 0
          s << ', '
        end
        s << "#{arr[3]} secs"
      end
    when 3
      if arr[0] > 0
        s << "#{arr[0]} hours"
      end
      if arr[1] > 0
        if s.length > 0
          s << ', '
        end
        s << "#{arr[1]} mins"
      end
      if arr[2] > 0
        if s.length > 0
          s << ', '
        end
        s << "#{arr[2]} secs"
      end
  end

  s
end

.days(days) ⇒ Object



33
34
35
# File 'lib/vpim/duration.rb', line 33

def Duration.days(days)
  Duration.new(days * SECS_DAY)
end

.hours(hours) ⇒ Object



29
30
31
# File 'lib/vpim/duration.rb', line 29

def Duration.hours(hours)
  Duration.new(hours * SECS_HOUR)
end

.mins(mins) ⇒ Object



25
26
27
# File 'lib/vpim/duration.rb', line 25

def Duration.mins(mins)
  Duration.new(mins * 60)
end

.secs(secs) ⇒ Object



21
22
23
# File 'lib/vpim/duration.rb', line 21

def Duration.secs(secs)
  Duration.new(secs)
end

Instance Method Details

#by_daysObject



61
62
63
# File 'lib/vpim/duration.rb', line 61

def by_days
  [ days, hours % 24, mins % MINS_HOUR, secs % 60]
end

#by_hoursObject



57
58
59
# File 'lib/vpim/duration.rb', line 57

def by_hours
  [ hours, mins % MINS_HOUR, secs % 60]
end

#daysObject



49
50
51
# File 'lib/vpim/duration.rb', line 49

def days
  (@secs/SECS_DAY).to_i
end

#hoursObject



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

def hours
  (@secs/SECS_HOUR).to_i
end

#minsObject



41
42
43
# File 'lib/vpim/duration.rb', line 41

def mins
  (@secs/60).to_i
end

#secsObject



37
38
39
# File 'lib/vpim/duration.rb', line 37

def secs
  @secs
end

#to_aObject



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

def to_a
  by_days
end

#to_sObject



69
70
71
# File 'lib/vpim/duration.rb', line 69

def to_s
  Duration.as_str(self.to_a)
end

#weeksObject



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

def weeks
  (days/7).to_i
end