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

time: ParseDuration fails for large strings #15011

Closed
siritinga opened this issue Mar 29, 2016 · 4 comments
Closed

time: ParseDuration fails for large strings #15011

siritinga opened this issue Mar 29, 2016 · 4 comments

Comments

@siritinga
Copy link

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go 1.6
  2. What operating system and processor architecture are you using (go env)?
    Ubuntu x64 but it fails on play.golang.org too.
  3. What did you do?
    I ran http://play.golang.org/p/N0A7ph_xkj which contains a large string (not really that large) to be parsed.
  4. What did you expect to see?
    Duration parsed with no errors. It works if you remove the last number of duration.
  5. What did you see instead?
    2009/11/10 23:00:00 time: invalid duration 0.12345678912345678912h
@bradfitz
Copy link
Contributor

/cc @robpike

@martisch
Copy link
Contributor

From what i remember when changing ParseDuration it does not use infinite precision math:
Everything after the . is read in as an int64 and then scaled to get a duration in nanoseconds.
"12345678912345678912" overflows int64 but "1234567891234567891" as int64 does not.
Whether 0.12345678912345678912 or 0.1234567891234567891 is used does not seem to make a difference for the duration as both would at the end result in the same duration, 444444440844 nanoseconds. So in principle one could parse more digits only to discard them since they have no affect on the final duration in nanoseconds. Not sure if that would be preferred since it gives an even more false sense of how accurate the parsed duration really is.

@siritinga
Copy link
Author

Hi, thanks for checking this.

I understand that there is no benefit in using so many digits. I found the issue because I was parsing the duration as I got it from somewhere else and this string came.

However, it seems that this case also fails, but it is a valid time inside precision (59 seconds and one nanosecond, expressed in hours): http://play.golang.org/p/xCp_XyTxXC. If you start removing the last digits, you get 59s, not 59s1ns. You can get 59s1ns if you round it by hand (http://play.golang.org/p/kD8r2ShnNB). I don't know if these cases should be managed by ParseDuration (and Parse may have the same issue).

@bradfitz bradfitz added this to the Unplanned milestone Apr 7, 2016
@gopherbot
Copy link

CL https://golang.org/cl/29338 mentions this issue.

@golang golang locked and limited conversation to collaborators Sep 19, 2017
@rsc rsc unassigned robpike Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants