Commit f2045aad authored by David Symonds's avatar David Symonds

time: accept numbers larger than 2^32 in ParseDuration.

Fixes #3374.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6683047
parent 24083437
...@@ -323,7 +323,8 @@ func atoi(s string) (x int, err error) { ...@@ -323,7 +323,8 @@ func atoi(s string) (x int, err error) {
neg = true neg = true
s = s[1:] s = s[1:]
} }
x, rem, err := leadingInt(s) q, rem, err := leadingInt(s)
x = int(q)
if err != nil || rem != "" { if err != nil || rem != "" {
return 0, atoiError return 0, atoiError
} }
...@@ -954,18 +955,18 @@ func parseNanoseconds(value string, nbytes int) (ns int, rangeErrString string, ...@@ -954,18 +955,18 @@ func parseNanoseconds(value string, nbytes int) (ns int, rangeErrString string,
var errLeadingInt = errors.New("time: bad [0-9]*") // never printed var errLeadingInt = errors.New("time: bad [0-9]*") // never printed
// leadingInt consumes the leading [0-9]* from s. // leadingInt consumes the leading [0-9]* from s.
func leadingInt(s string) (x int, rem string, err error) { func leadingInt(s string) (x int64, rem string, err error) {
i := 0 i := 0
for ; i < len(s); i++ { for ; i < len(s); i++ {
c := s[i] c := s[i]
if c < '0' || c > '9' { if c < '0' || c > '9' {
break break
} }
if x >= (1<<31-10)/10 { if x >= (1<<63-10)/10 {
// overflow // overflow
return 0, "", errLeadingInt return 0, "", errLeadingInt
} }
x = x*10 + int(c) - '0' x = x*10 + int64(c) - '0'
} }
return x, s[i:], nil return x, s[i:], nil
} }
...@@ -1010,7 +1011,7 @@ func ParseDuration(s string) (Duration, error) { ...@@ -1010,7 +1011,7 @@ func ParseDuration(s string) (Duration, error) {
for s != "" { for s != "" {
g := float64(0) // this element of the sequence g := float64(0) // this element of the sequence
var x int var x int64
var err error var err error
// The next character must be [0-9.] // The next character must be [0-9.]
......
...@@ -999,6 +999,8 @@ var parseDurationTests = []struct { ...@@ -999,6 +999,8 @@ var parseDurationTests = []struct {
{"-2m3.4s", true, -(2*Minute + 3*Second + 400*Millisecond)}, {"-2m3.4s", true, -(2*Minute + 3*Second + 400*Millisecond)},
{"1h2m3s4ms5us6ns", true, 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond}, {"1h2m3s4ms5us6ns", true, 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond},
{"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond}, {"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond},
// large value
{"52763797000ns", true, 52763797000 * Nanosecond},
// errors // errors
{"", false, 0}, {"", false, 0},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment