class Tins::Duration

def format(template = '%d+%h:%m:%s.%f', precision: nil)

def format(template = '%d+%h:%m:%s.%f', precision: nil)
  result = template.gsub(/%[Ddhms]/) { |directive|
    case directive
    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