class TheFox::Timr::Duration

Uses seconds as basis.

Attributes

seconds[R]

Basis Data (Integer)

A Duration is stored as seconds.

Public Class Methods

new(seconds = 0) click to toggle source
# 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
parse(str) click to toggle source

Parse a String using [ChronicDuration](rubygems.org/gems/chronic_duration) and create a new Duration instance.

# File lib/timr/duration.rb, line 154
def parse(str)
        Duration.new(ChronicDuration.parse(str))
end

Public Instance Methods

+(duration) click to toggle source

Adds two Durations.

# 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) click to toggle source

Subtract two Durations.

# 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) click to toggle source

Lesser

# File lib/timr/duration.rb, line 121
def <(obj)
        case obj
        when Integer
                @seconds < obj
        when Duration
                @seconds < obj.to_i
        end
end
>(obj) click to toggle source

Greater

# 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() click to toggle source

Converts seconds to `nh nm ns` format. Where `n` is a number.

# 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() click to toggle source

Convert to_human to a String when nil.

# File lib/timr/duration.rb, line 51
def to_human_s
        h = to_human
        h ? h : '---'
end
to_i() click to toggle source

Use this instead of `Duration.seconds`.

# File lib/timr/duration.rb, line 146
def to_i
        @seconds
end
to_man_days() click to toggle source

Man-days, Man-hours

Man Unit:

  • 8 hours are 1 man-day.

  • 5 man-days are 1 man-week, and so on.

# 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() click to toggle source

String

# File lib/timr/duration.rb, line 141
def to_s
        @seconds.to_s
end
to_smh() click to toggle source

Seconds, Minutes, Hours as Array

# 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