Usage
20.to_stopwatch # 00:00:20
class Numeric
def to_stopwatch
hours, remainder = (self / 3660).floor.to_i, self % 3660
minutes, seconds = (remainder / 60).floor.to_i, (remainder % 60).to_i
"#{sprintf("%02d", hours)}:#{sprintf("%02d", minutes)}:#{sprintf("%02d", seconds)}"
end
end
class NilClass
delegate :to_stopwatch, :to => 0
end
No comments:
Post a Comment