mirror of
https://github.com/golang/go
synced 2024-11-23 14:50:07 -07:00
time: don't panic when stringifying Weekday
Fixes #24692 Change-Id: I14058cd3968d08fbcfc275f1b13b6dba9e3c5068 Reviewed-on: https://go-review.googlesource.com/106535 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
2dfb423e6e
commit
94ef1dafbd
@ -323,7 +323,14 @@ var days = [...]string{
|
||||
}
|
||||
|
||||
// String returns the English name of the day ("Sunday", "Monday", ...).
|
||||
func (d Weekday) String() string { return days[d] }
|
||||
func (d Weekday) String() string {
|
||||
if Sunday <= d && d <= Saturday {
|
||||
return days[d]
|
||||
}
|
||||
buf := make([]byte, 20)
|
||||
n := fmtInt(buf, uint64(d))
|
||||
return "%!Weekday(" + string(buf[n:]) + ")"
|
||||
}
|
||||
|
||||
// Computations on time.
|
||||
//
|
||||
|
@ -1319,6 +1319,16 @@ func TestZeroMonthString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 24692: Out of range weekday panics
|
||||
func TestWeekdayString(t *testing.T) {
|
||||
if got, want := Weekday(Tuesday).String(), "Tuesday"; got != want {
|
||||
t.Errorf("Tuesday weekday = %q; want %q", got, want)
|
||||
}
|
||||
if got, want := Weekday(14).String(), "%!Weekday(14)"; got != want {
|
||||
t.Errorf("14th weekday = %q; want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFileLimit(t *testing.T) {
|
||||
const zero = "/dev/zero"
|
||||
if _, err := os.Stat(zero); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user