golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.11k stars 17.68k forks source link

time: pre-defined layout cannot be parsed #45081

Closed changkun closed 3 years ago

changkun commented 3 years ago

What version of Go are you using (go version)?

$ go version
go version devel +72b501cb03 Wed Mar 17 07:01:00 2021 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/changkun/.cache/go-build"
GOENV="/home/changkun/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/changkun/go/pkg/mod"
GONOPROXY="poly.red/x"
GONOSUMDB="poly.red/x"
GOOS="linux"
GOPATH="/home/changkun/go"
GOPRIVATE="poly.red/x"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/changkun/dev/godev/go-gerrit"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/changkun/dev/godev/go-gerrit/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel +fa58313d83 Wed Mar 17 07:56:10 2021 +0100"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/changkun/dev/changkun.de/redir/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2137753388=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
    "testing"
    "time"
)

func TestParseLayout(t *testing.T) {
    tests := []string{
        time.ANSIC,
        time.UnixDate,
        time.RubyDate,
        time.RFC822,
        time.RFC822Z,
        time.RFC850,
        time.RFC1123,
        time.RFC1123Z,
        time.RFC3339,
        time.RFC3339Nano,
        time.Kitchen,
        time.Stamp,
        time.StampMilli,
        time.StampMicro,
        time.StampNano}

    for _, tt := range tests {
        _, err := time.Parse(tt, tt)
        if err != nil {
            t.Error(err)
        } else {
            t.Log("OK", tt)
        }
    }
}

What did you expect to see?

All OK.

What did you see instead?

Only part of them gets OK.

=== RUN   TestParseLayout
    time_test.go:29: parsing time "Mon Jan _2 15:04:05 2006" as "Mon Jan _2 15:04:05 2006": cannot parse "_2 15:04:05 2006" as "_2"
    time_test.go:29: parsing time "Mon Jan _2 15:04:05 MST 2006" as "Mon Jan _2 15:04:05 MST 2006": cannot parse "_2 15:04:05 MST 2006" as "_2"
    time_test.go:31: OK Mon Jan 02 15:04:05 -0700 2006
    time_test.go:31: OK 02 Jan 06 15:04 MST
    time_test.go:31: OK 02 Jan 06 15:04 -0700
    time_test.go:31: OK Monday, 02-Jan-06 15:04:05 MST
    time_test.go:31: OK Mon, 02 Jan 2006 15:04:05 MST
    time_test.go:31: OK Mon, 02 Jan 2006 15:04:05 -0700
    time_test.go:29: parsing time "2006-01-02T15:04:05Z07:00": extra text: "07:00"
    time_test.go:29: parsing time "2006-01-02T15:04:05.999999999Z07:00": extra text: "07:00"
    time_test.go:31: OK 3:04PM
    time_test.go:29: parsing time "Jan _2 15:04:05" as "Jan _2 15:04:05": cannot parse "_2 15:04:05" as "_2"
    time_test.go:29: parsing time "Jan _2 15:04:05.000" as "Jan _2 15:04:05.000": cannot parse "_2 15:04:05.000" as "_2"
    time_test.go:29: parsing time "Jan _2 15:04:05.000000" as "Jan _2 15:04:05.000000": cannot parse "_2 15:04:05.000000" as "_2"
    time_test.go:29: parsing time "Jan _2 15:04:05.000000000" as "Jan _2 15:04:05.000000000": cannot parse "_2 15:04:05.000000000" as "_2"
--- FAIL: TestParseLayout (0.00s)
FAIL
exit status 1
seankhliao commented 3 years ago

https://golang.org/pkg/time/#pkg-constants

Some valid layouts are invalid time values for time.Parse, due to formats such as _ for space padding and Z for zone information.

Closing as working as intented