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: error handling for time.Month.String() and time.Weekday.String() is inconsistent #24692

Closed
jaseemabid opened this issue Apr 5, 2018 · 3 comments

Comments

Projects
None yet
5 participants
@jaseemabid
Copy link

commented Apr 5, 2018

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

go version go1.10 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/j/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/j/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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-build714199702=/tmp/go-build -gno-record-gcc-switches"

What did you do?

When converting a month out of range to String, the function returns a debug string, but panics for Weekday.

Code sample at https://play.golang.org/p/3U4gRY5kRzc

What did you expect to see?

Both should either panic, or return error string.

What did you see instead?

time.Month.String() returned debug string, time.Weekday.String() paniced.

@agnivade

This comment has been minimized.

Copy link
Member

commented Apr 5, 2018

Indeed.

Month.String() checks for range, and if outside it returns a stringified representation of it.

// String returns the English name of the month ("January", "February", ...).
func (m Month) String() string {
	if January <= m && m <= December {
		return months[m-1]
	}
	buf := make([]byte, 20)
	n := fmtInt(buf, uint64(m))
	return "%!Month(" + string(buf[n:]) + ")"
}

Whereas Weekday.String() does not.

// String returns the English name of the day ("Sunday", "Monday", ...).
func (d Weekday) String() string { return days[d] }

We should probably fix to not panic on (Weekday).String(), or there might be a reason for this. Not sure. In any case, I think this might be on the verge of breaking Go1 compat. NeedsDecision.

/cc @rsc, @ianlancetaylor

@ALTree ALTree changed the title Error handling for time.Month.String() and time.Weekday.String() is inconsistent time: error handling for time.Month.String() and time.Weekday.String() is inconsistent Apr 5, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Apr 11, 2018

We should fix Weekday.String as we fixed Month.String for #17720.

@gopherbot

This comment has been minimized.

Copy link

commented Apr 12, 2018

Change https://golang.org/cl/106535 mentions this issue: time: don't panic when stringifying Weekday

@gopherbot gopherbot closed this in 94ef1da Apr 12, 2018

@golang golang locked and limited conversation to collaborators Apr 12, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.