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.RFC3339 does not support space between date and time #41461

Closed
loganjhennessy opened this issue Sep 17, 2020 · 1 comment
Closed

time.RFC3339 does not support space between date and time #41461

loganjhennessy opened this issue Sep 17, 2020 · 1 comment

Comments

@loganjhennessy
Copy link

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

$ go version
go version go1.15.2 darwin/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="/Users/logan.hennessy/Library/Caches/go-build"
GOENV="/Users/logan.hennessy/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/logan.hennessy/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/logan.hennessy/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/fl/b9303rmd11zgcf2t8_qxsvzc0000gp/T/go-build771365924=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I tried to parse an RFC3339-compliant time with the RFC3339 formatter in the time library.

Specifically: time.Parse(time.RFC3339, "2020-09-08 15:52:43-07:00")

See https://play.golang.org/p/cFo1Pd0ciKa for the full code snippet.

Code snippet also pasted here
package main

import "fmt"
import "time"

func parseTime(traw string) {
p := fmt.Printf
t, e := time.Parse(time.RFC3339, traw)
if e != nil {
p("Failed to parse "%s", error: %v\n", traw, e)
} else {
p("Successfully parsed "%s" as "%v"\n", traw, t)
}
}

func main() {
t1raw := "2020-09-08 15:52:43-07:00"
t2raw := "2020-09-08T15:52:43-07:00"

parseTime(t1raw)
parseTime(t2raw)

}

What did you expect to see?

I expected this code to successfully parse both times, as they are equivalent according to RFC3339.

What did you see instead?

"2020-09-08T15:52:43-07:00" was successfully parsed.

"2020-09-08 15:52:43-07:00" failed with the following error:

Failed to parse "2020-09-08 15:52:43-07:00", error: parsing time "2020-09-08 15:52:43-07:00" as "2006-01-02T15:04:05Z07:00": cannot parse " 15:52:43-07:00" as "T"

It looks like the parser fails to recognize the optional nature of the T and instead attempts to parse the entire time section of the string as T, which fails.

From https://tools.ietf.org/html/rfc3339#section-5 section 5.6:

  NOTE: ISO 8601 defines date and time separated by "T".
  Applications using this syntax may choose, for the sake of
  readability, to specify a full-date and full-time separated by
  (say) a space character.
@ianlancetaylor
Copy link
Contributor

As the docs say: "RFC3339, RFC822, RFC822Z, RFC1123, and RFC1123Z are useful for formatting; when used with time.Parse they do not accept all the time formats permitted by the RFCs and they do accept time formats not formally defined."

@golang golang locked and limited conversation to collaborators Sep 17, 2021
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

3 participants