Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a pad_to option for the chrono format #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

glacials
Copy link

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.

`pad_to` can be any of `:years`, `months`, `:days`, `:hours`, `:minutes`, or `:seconds`
When present, leading `00:`s 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant