Skip to content

time: magic number datetime formatting #38871

@shaneHowearth

Description

@shaneHowearth

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

 $ go version
go version go1.14.2 linux/amd64

Does this issue reproduce with the latest release?

As far as I know

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

go env Output
 $ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/shane/.cache/go-build"
GOENV="/home/shane/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/shane/GoLang"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.14"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.14/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
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 -pt

What did you do?

https://play.golang.org/p/BSBuQKa9ihR
I had a bug in my code that was due to my misunderstanding the date format style; I had the month and the day the wrong way round. The bug was discovered when a day was added, and unexpected results were returned.

It's not a difficult bug to fix, but it's a bug that is caused by something I was taught as a programmer to avoid: Magic numbers.

The reference date used for formatting is a magic number, in order to know how you want the time to be parsed you need to know which number matches up to what value.

Given that Go is 10+ years old, changing that format style will hurt backward compatibility, and there will even be developers that have memorised the style and can spot the bug at a glance. I am not one of those.

I ask that a sister format be created, one that avoids magic numbers and uses human readable words or symbols to convey meaning.

There are the well known YYYY MM DD HH mm ss and %Y %M %D %H %m %S styles, although they have a confusing overlap with Months and minutes. The former of the two also implies the number of digits that will be used to represent the value (thus YYYY is 2006 and YY is 06). The information encoded is (IMO) clearer than the magic number format.

I know it can be done (in fact when I added my comment to a previously raised issue I was pointed to a 3rd party library that does what I think should be a part of the standard library), the only question is, I guess, do enough people agree with me

What did you expect to see?

Days treated as days and months treated as months

What did you see instead?

Days treated as months and months treated as days

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions