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
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@jaseemabid
Copy link

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
Copy link
Contributor

agnivade 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
@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 5, 2018
@ianlancetaylor
Copy link
Contributor

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

@ianlancetaylor ianlancetaylor added the NeedsFix The path to resolution is known, but the work has not been done. label Apr 11, 2018
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 11, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Apr 11, 2018
@gopherbot
Copy link

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

@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.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants