aboutsummaryrefslogtreecommitdiff
path: root/src/time/format.go
diff options
context:
space:
mode:
authorLE Manh Cuong <cuong.manhle.vn@gmail.com>2019-01-30 12:38:15 +0700
committerIan Lance Taylor <iant@golang.org>2019-04-26 14:01:55 +0000
commit8feeada50cf3f783fe3c8113d5da51ad8c0b3014 (patch)
tree622a400e6360f167d3eaaea59279e3ec1d672244 /src/time/format.go
parentbc48cc770f966e401873436413d31436e84df2d8 (diff)
downloadgo-8feeada50cf3f783fe3c8113d5da51ad8c0b3014.tar.gz
go-8feeada50cf3f783fe3c8113d5da51ad8c0b3014.zip
time: fix misleading error with the leading zero format
When the leading zero format is used, we currently don't handle the month and year properly. For the month, we were reporting an out of range error when getnum returns zero of its own, as it also returns the month 0. That's confusing, so only check the range when getnum returns a nil error. For the year, we don't restore the value when parsing error occurs. For example, with the incorrect input "111-01", "01" will be used to report an error. So restore the value when an error occurs fix the problem. Fixes #29918 Fixes #29916 Change-Id: I3145f8c46813a0457766b7c302482e6b56f94ed6 Reviewed-on: https://go-review.googlesource.com/c/go/+/160338 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.go7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/time/format.go b/src/time/format.go
index d8e295f696..b531cb4760 100644
--- a/src/time/format.go
+++ b/src/time/format.go
@@ -865,9 +865,12 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
err = errBad
break
}
+ hold := value
p, value = value[0:2], value[2:]
year, err = atoi(p)
- if year >= 69 { // Unix time starts Dec 31 1969 in some time zones
+ if err != nil {
+ value = hold
+ } else if year >= 69 { // Unix time starts Dec 31 1969 in some time zones
year += 1900
} else {
year += 2000
@@ -887,7 +890,7 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
month++
case stdNumMonth, stdZeroMonth:
month, value, err = getnum(value, std == stdZeroMonth)
- if month <= 0 || 12 < month {
+ if err == nil && (month <= 0 || 12 < month) {
rangeErrString = "month"
}
case stdWeekDay: