Class: TheFox::Timr::Duration
- Inherits:
-
Object
- Object
- TheFox::Timr::Duration
- Includes:
- Error
- Defined in:
- lib/timr/duration.rb
Overview
Uses seconds as basis.
Instance Attribute Summary collapse
-
#seconds ⇒ Object
readonly
Basis Data (Integer).
Class Method Summary collapse
-
.parse(str) ⇒ Object
Parse a String using [ChronicDuration](rubygems.org/gems/chronic_duration) and create a new Duration instance.
Instance Method Summary collapse
-
#+(duration) ⇒ Object
Adds two Durations.
-
#-(duration) ⇒ Object
Subtract two Durations.
-
#<(obj) ⇒ Object
Lesser.
-
#>(obj) ⇒ Object
Greater.
-
#initialize(seconds = 0) ⇒ Duration
constructor
A new instance of Duration.
-
#to_human ⇒ Object
Converts seconds to ‘nh nm ns` format.
-
#to_human_s ⇒ Object
Convert to_human to a String when nil.
-
#to_i ⇒ Object
Use this instead of ‘Duration.seconds`.
-
#to_man_days ⇒ Object
Man-days, Man-hours.
-
#to_s ⇒ Object
String.
-
#to_smh ⇒ Object
Seconds, Minutes, Hours as Array.
Constructor Details
#initialize(seconds = 0) ⇒ Duration
Returns a new instance of Duration.
17 18 19 20 21 22 23 |
# File 'lib/timr/duration.rb', line 17 def initialize(seconds = 0) @seconds = seconds.to_i @smh_seconds = nil @smh_minutes = nil @smh_hours = nil end |
Instance Attribute Details
#seconds ⇒ Object (readonly)
Basis Data (Integer)
A Duration is stored as seconds.
15 16 17 |
# File 'lib/timr/duration.rb', line 15 def seconds @seconds end |
Class Method Details
.parse(str) ⇒ Object
Parse a String using [ChronicDuration](rubygems.org/gems/chronic_duration) and create a new Duration instance.
154 155 156 |
# File 'lib/timr/duration.rb', line 154 def parse(str) Duration.new(ChronicDuration.parse(str)) end |
Instance Method Details
#+(duration) ⇒ Object
Adds two Durations.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/timr/duration.rb', line 90 def +(duration) case duration when Integer Duration.new(@seconds + duration) when Duration Duration.new(@seconds + duration.seconds) when nil Duration.new(@seconds) else raise DurationError, "Wrong type #{duration.class} for '+' function." end end |
#-(duration) ⇒ Object
Subtract two Durations.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/timr/duration.rb', line 104 def -(duration) case duration when Integer diff = @seconds - duration when Duration diff = @seconds - duration.seconds else raise DurationError, "Wrong type #{duration.class} for '+' function." end if diff < 0 diff = 0 end Duration.new(diff) end |
#<(obj) ⇒ Object
Lesser
121 122 123 124 125 126 127 128 |
# File 'lib/timr/duration.rb', line 121 def <(obj) case obj when Integer @seconds < obj when Duration @seconds < obj.to_i end end |
#>(obj) ⇒ Object
Greater
131 132 133 134 135 136 137 138 |
# File 'lib/timr/duration.rb', line 131 def >(obj) case obj when Integer @seconds > obj when Duration @seconds > obj.to_i end end |
#to_human ⇒ Object
Converts seconds to ‘nh nm ns` format. Where `n` is a number.
40 41 42 43 44 45 46 47 48 |
# File 'lib/timr/duration.rb', line 40 def to_human dur_opt = { :format => :short, :limit_to_hours => true, :keep_zero => false, :units => 2, } h = ChronicDuration.output(@seconds, dur_opt) end |
#to_human_s ⇒ Object
Convert to_human to a String when nil.
51 52 53 54 |
# File 'lib/timr/duration.rb', line 51 def to_human_s h = to_human h ? h : '---' end |
#to_i ⇒ Object
Use this instead of ‘Duration.seconds`.
146 147 148 |
# File 'lib/timr/duration.rb', line 146 def to_i @seconds end |
#to_man_days ⇒ Object
Man-days, Man-hours
Man Unit:
-
8 hours are 1 man-day.
-
5 man-days are 1 man-week, and so on.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/timr/duration.rb', line 62 def to_man_days if @seconds < 28800 # 8 * 3600 = 1 man-day to_human elsif @seconds < 144000 # 5 * 28800 = 1 man-week seconds = @seconds man_days = seconds / 28800 seconds -= man_days * 28800 man_hours = seconds / 3600 '%dd %dh' % [man_days, man_hours] else seconds = @seconds man_weeks = seconds / 144000 seconds -= man_weeks * 144000 man_days = seconds / 28800 seconds -= man_days * 28800 man_hours = seconds / 3600 '%dw %dd %dh' % [man_weeks, man_days, man_hours] end end |
#to_s ⇒ Object
String
141 142 143 |
# File 'lib/timr/duration.rb', line 141 def to_s @seconds.to_s end |
#to_smh ⇒ Object
Seconds, Minutes, Hours as Array
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/timr/duration.rb', line 26 def to_smh @smh_seconds = @seconds @smh_hours = @smh_seconds / 3600 @smh_seconds -= @smh_hours * 3600 @smh_minutes = @smh_seconds / 60 @smh_seconds -= @smh_minutes * 60 [@smh_seconds, @smh_minutes, @smh_hours] end |