class Tins::Duration

Public Class Methods

new(seconds) click to toggle source
# File lib/tins/duration.rb, line 5
def initialize(seconds)
  @negative = seconds < 0
  seconds = seconds.abs
  @original_seconds = seconds
  @days, @hours, @minutes, @seconds, @fractional_seconds =
    [ 86_400, 3600, 60, 1, 0 ].inject([ [], seconds ]) {|(r, s), d|
      if d > 0
        dd, rest = s.divmod(d)
        r << dd
        [ r, rest ]
      else
        r << s
      end
    }
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/tins/duration.rb, line 25
def <=>(other)
  to_f <=> other.to_f
end
days?() click to toggle source
# File lib/tins/duration.rb, line 33
def days?
  @days > 0
end
format(template = '%S%d+%h:%m:%s.%f', precision: nil) click to toggle source
# File lib/tins/duration.rb, line 53
def format(template = '%S%d+%h:%m:%s.%f', precision: nil)
  result = template.gsub(/%[DdhmSs]/) { |directive|
    case directive
    when '%S' then ?- if negative?
    when '%d' then @days
    when '%h' then '%02u' % @hours
    when '%m' then '%02u' % @minutes
    when '%s' then '%02u' % @seconds
    when '%D' then format_smart
    end
  }
  if result.include?('%f')
    if precision
      fractional_seconds = "%.#{precision}f" % @fractional_seconds
    else
      fractional_seconds = '%f' % @fractional_seconds
    end
    result.gsub!('%f', fractional_seconds[2..-1])
  end
  result
end
fractional_seconds?() click to toggle source
# File lib/tins/duration.rb, line 49
def fractional_seconds?
  @fractional_seconds > 0
end
hours?() click to toggle source
# File lib/tins/duration.rb, line 37
def hours?
  @hours > 0
end
minutes?() click to toggle source
# File lib/tins/duration.rb, line 41
def minutes?
  @minutes > 0
end
negative?() click to toggle source
# File lib/tins/duration.rb, line 29
def negative?
  @negative
end
seconds?() click to toggle source
# File lib/tins/duration.rb, line 45
def seconds?
  @seconds > 0
end
to_f() click to toggle source
# File lib/tins/duration.rb, line 21
def to_f
  @original_seconds.to_f
end
to_s() click to toggle source
# File lib/tins/duration.rb, line 75
def to_s
  format_smart
end

Private Instance Methods

format_smart() click to toggle source
# File lib/tins/duration.rb, line 81
def format_smart
  template  = '%h:%m:%s'
  precision = nil
  if days?
    template.prepend '%d+'
  end
  if fractional_seconds?
    template << '.%f'
    precision = 3
  end
  template.prepend '%S'
  format template, precision: precision
end