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 support for durations. #55

Open
myusuf3 opened this issue Jun 12, 2017 · 2 comments
Open

Add support for durations. #55

myusuf3 opened this issue Jun 12, 2017 · 2 comments

Comments

@myusuf3
Copy link

myusuf3 commented Jun 12, 2017

To change something like this:

354h22m3.24s // :S

Into something like this:

2 weeks 18 hours 22 minutes 3 seconds

or various breakdowns like minutes or just hours.

@dmitshur
Copy link
Collaborator

@dustin
Copy link
Owner

dustin commented Jun 10, 2020

Perhaps it would be good to understand why go only rolls up to the nearest hour. I suppose if we're approximating, we can throw away precision and be fairly loose with what a day is. This seems consistent with the idea of humanization.

Formatting is the hard part here. I suspect we'd want a data-driven language for formatting strings, e.g., one would like to express the example above as:

humanize.Duration("%y %m %w %d %H %M %S", d)

where the variables, e.g. %d expands into one of three string segments based on cardinality:

0: ""
1: "1 day"
n: n + " days"

The formatting table is fairly straightforward, but space consumption might be slightly tricky. In our case here, we'd want to consume any whitespace around the zero case, as it is effectively a lexical black hole absorbing the space made for time units that aren't relevant for its case.

An optional operator might cause the 0 case to be treated as the n case (or just supply a modified expansion table). This depends on how interesting such a case is.

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

No branches or pull requests

3 participants