diff options
author | Obeyda Djeffal <djefobey@gmail.com> | 2020-04-11 23:21:22 +0100 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2020-04-14 00:01:14 +0000 |
commit | 201cb046b745f8bb00e3d382290190c74ba7b7e1 (patch) | |
tree | eb4e97572b16f5c526e5aaea09eadccfe0b1925d /src/time/format.go | |
parent | 5a706d163d332ec3a83e38849efbaf550a9da7ab (diff) | |
download | go-201cb046b745f8bb00e3d382290190c74ba7b7e1.tar.gz go-201cb046b745f8bb00e3d382290190c74ba7b7e1.zip |
time: quote original value in errors returned by ParseDuration
Quote original values passed as substring of ParseError.Message.
Improves the user experience of ParseDuration by making it
quote its original argument, for example:
_, err := time.ParseDuration("for breakfast")
will now produce an error, which when printed out is:
time: invalid duration "for breakfast"
instead of:
time: invalid duration for breakfast
Adapt test cases for format.Parse and format.ParseDuration.
Fixes #38295
Change-Id: Ife322c8f3c859e1e4e8dd546d4cf0d519b4bfa81
Reviewed-on: https://go-review.googlesource.com/c/go/+/227878
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/time/format.go')
-rw-r--r-- | src/time/format.go | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/src/time/format.go b/src/time/format.go index 899b6a40b0..b74108f0e7 100644 --- a/src/time/format.go +++ b/src/time/format.go @@ -856,7 +856,7 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error) } if std == 0 { if len(value) != 0 { - return Time{}, &ParseError{alayout, avalue, "", value, ": extra text: " + value} + return Time{}, &ParseError{alayout, avalue, "", value, ": extra text: " + quote(value)} } break } @@ -1390,7 +1390,7 @@ func ParseDuration(s string) (Duration, error) { return 0, nil } if s == "" { - return 0, errors.New("time: invalid duration " + orig) + return 0, errors.New("time: invalid duration " + quote(orig)) } for s != "" { var ( @@ -1402,13 +1402,13 @@ func ParseDuration(s string) (Duration, error) { // The next character must be [0-9.] if !(s[0] == '.' || '0' <= s[0] && s[0] <= '9') { - return 0, errors.New("time: invalid duration " + orig) + return 0, errors.New("time: invalid duration " + quote(orig)) } // Consume [0-9]* pl := len(s) v, s, err = leadingInt(s) if err != nil { - return 0, errors.New("time: invalid duration " + orig) + return 0, errors.New("time: invalid duration " + quote(orig)) } pre := pl != len(s) // whether we consumed anything before a period @@ -1422,7 +1422,7 @@ func ParseDuration(s string) (Duration, error) { } if !pre && !post { // no digits (e.g. ".s" or "-.s") - return 0, errors.New("time: invalid duration " + orig) + return 0, errors.New("time: invalid duration " + quote(orig)) } // Consume unit. @@ -1434,17 +1434,17 @@ func ParseDuration(s string) (Duration, error) { } } if i == 0 { - return 0, errors.New("time: missing unit in duration " + orig) + return 0, errors.New("time: missing unit in duration " + quote(orig)) } u := s[:i] s = s[i:] unit, ok := unitMap[u] if !ok { - return 0, errors.New("time: unknown unit " + u + " in duration " + orig) + return 0, errors.New("time: unknown unit " + quote(u) + " in duration " + quote(orig)) } if v > (1<<63-1)/unit { // overflow - return 0, errors.New("time: invalid duration " + orig) + return 0, errors.New("time: invalid duration " + quote(orig)) } v *= unit if f > 0 { @@ -1453,13 +1453,13 @@ func ParseDuration(s string) (Duration, error) { v += int64(float64(f) * (float64(unit) / scale)) if v < 0 { // overflow - return 0, errors.New("time: invalid duration " + orig) + return 0, errors.New("time: invalid duration " + quote(orig)) } } d += v if d < 0 { // overflow - return 0, errors.New("time: invalid duration " + orig) + return 0, errors.New("time: invalid duration " + quote(orig)) } } |