golang / go

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

time: ISO 8601 template const #36650

Closed rbUUbr closed 4 years ago

rbUUbr commented 4 years ago

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

$ go version
1.12.9

Does this issue reproduce with the latest release?

yes

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

Mac OS, catalina

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/userx/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/userx/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/userx/myproject/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/0z/xvrbhgwn6vx8kfhcgn60bzvw0000gn/T/go-build050042786=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Parsing date to time object in GoLang https://play.golang.org/p/dWcJIGo9K7g

What did you expect to see?

Any predefined const for date in ISO 8601 format

What did you see instead?

I see no predefined const for date objects, only for datetime

I think, it is important to add it, because it's pretty common case when you have just date without time in ISO 8601 format

ALTree commented 4 years ago

You don't need a predefined constant to parse that date, just write

d, err := time.Parse("2006-01-02", "2020-01-02")

For such a short format, I'm not sure a pre-defined constant would be very useful.

rbUUbr commented 4 years ago

@ALTree Yes, I do something like this, but it's very strange when we have const for ISO 8601 format for DateTime, but don't have such const for Date type. WDYT? I think, it's pretty common case to resolve and it can be good to add predefined template const

ianlancetaylor commented 4 years ago

I'm not sure what the exact suggestion is. ISO 8601 defines several different formats: date, time, combined date and time, duration, interval, repeating interval, etc. What precisely do you think we should do?

rbUUbr commented 4 years ago

I often need to parse date (for example: "2019-12-12") to ISO 8601 format. It's pretty unclear for me, why should I define template for it everywhere or create some util function to do this. I think, it will be good to add const that define template for dates in ISO 8601, just for date. WDYT about it?

ianlancetaylor commented 4 years ago

Why should we add a constant just for ISO 8601 dates, and not for any other aspect of ISO 8601?

rbUUbr commented 4 years ago

I think, it's pretty more common case to parse date, not interval. Anyway, it also can be good to add consts for all aspects

beoran commented 4 years ago

https://github.com/beoran/iso8601/blob/master/iso8601.go perhaps?

However, I noticed that there seems to be no support in the time package for ISO8601 week dates such as 2006-W23, 2006-W23D1 or day dates 2000-234. In particular the week numbers are useful for certain types of planning software.

rbUUbr commented 4 years ago

@beoran thanks, interesting, but here the main sense is to add it to stdlib like it's made in Ruby. it gives a possibility to not load some additional modules just for a 1 const.

beoran commented 4 years ago

As @ianlancetaylor said there's more to this topic than just adding 1 or 2 constants to the 'time' package. Currently, the time package lacks several features to be ISO8601 compatible, and if they add the constants, that would not be sufficient for real ISO8601 support. A separate package may be the best way to get started on designing what this functionality should be.

rbUUbr commented 4 years ago

ok then, can we can discuss here some structure of separate package for date? and need for this package at all? @ianlancetaylor @beoran

ALTree commented 4 years ago

@rbUUbr such package should be a third-party package, I don't think anyone was proposing it for inclusion in the standard library. For this reason, this is probably not the right place to discuss it.

Reading the discussion above, it seems that we are not going to add just a "2006-01-02" constant for ISO 8601 dates in the time package, and providing a bunch of constants for every aspect of ISO 8601 is something that should be implemented as an external package, so I think we can close this issue.