chronic_duration icon indicating copy to clipboard operation
chronic_duration copied to clipboard

Add a `pad_to` option for the chrono format

Open glacials opened this issue 9 years ago • 0 comments

Description

Allows a pad_to option when outputting in the chrono format.

pad_to can be any of :years, months, :days, :hours, :minutes, or :seconds. When present, zeros will not be stripped for the given unit and all units below it.

e.g.

> ChronicDuration.output(649, :format => :chrono)
=> "10:49"
> ChronicDuration.output(649, :format => :chrono, :pad_to => :hours)
=> "0:10:49"
> ChronicDuration.output(649, :format => :chrono, :pad_to => :days)
=> "0:00:10:49"

My use case for this is that I'm outputting a bunch of durations in an HTML table column, and wanted the vertical edges of the times to line up with each other (+/- a character). Some are minutes long, others hours long.

Code

I didn't want to refactor too much or move much around as I'm not sure how you'd prefer for that to happen, so I tried to do this in place, directly replacing the old gsub(/^(00:)+/, '') with this injected tap. As a result it can probably be considered a bit messy, so if you'd like me to clean it up let me know if there's anything I should or shouldn't move around while refactoring.

glacials avatar Feb 27 '15 05:02 glacials