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
proposal: time: Duration String() should omit zero units #39064
Comments
Unfortunately, at this point this seems likely to break some existing programs. Is the benefit worth the cost? |
Well, it will not break programs which try to parse to the duration since duration, _ := time.ParseDuration("5h")
if duration == 5*time.Hour {
fmt.Println("equal")
} But, it will break programs that try to stringify the This proposal is a sort of enhancing the If the Go team thinks that the cost is not acceptable, feel free to close it. |
At work we have a time.Duration printing function that trims the useless If we can't change the behavior of String, perhaps we could add a new method. ( |
FWIW, we used to print just "0" for zero and changed it to "0s" a while back. #14058. (Not the same as this issue but related.) It looks like right now the implementation has the invariant that it always prints a string ending in "s" - that is, there is always some number of seconds reported. Maybe a smaller unit, but always some kind of seconds. If larger units can be factored out, they are printed ahead of the seconds. The docs are also pretty clear:
It seems almost certain that changing this would break tests, and probably also libraries, and silently. For better or worse, this is the documented format. It's too late to change. |
Based on the discussion above, this seems like a likely decline. |
@cespare I agree that a function like |
No change in consensus, so declined. @ainar-g, exactly what you want to display changes from team to team and maybe project to project. The best place for this logic is in your own code, or in a third-party library if you want to share it. (Personally I always use fmt.Sprintf("%.3fs", d.Seconds()), but I'm not adding that to the standard library either.) |
What version of Go are you using (
go version
)?1.14.2
Proposal
I propose that
time.Duration.Strings()
should omit zero units.For example
With this proposal, this example result will change from
5h0m0s
to5h
.0m0s
is meaningless.ns
,µs
andms
units if its value is zero, so we should also emit thes
andh
unit to keep theString()
behavior more consistent and predictable.The text was updated successfully, but these errors were encountered: