From 85ff137ff65119f9c16637057b7b1ebafd00e45d Mon Sep 17 00:00:00 2001 From: Wen Sun Date: Fri, 24 Nov 2023 17:25:14 +0900 Subject: [PATCH] Correct (short) week string result when set start of week --- outputer.go | 27 ++++++++------------------- outputer_test.go | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/outputer.go b/outputer.go index 74b121ee..36c3d1eb 100644 --- a/outputer.go +++ b/outputer.go @@ -71,27 +71,16 @@ func (c Carbon) ToShortMonthString(timezone ...string) string { // ToWeekString outputs a string in week layout like "Sunday", i18n is supported. // 输出完整星期字符串,支持i18n func (c Carbon) ToWeekString(timezone ...string) string { - if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) - } - if c.IsInvalid() { - return "" - } - if len(c.lang.resources) == 0 { - c.lang.SetLocale(defaultLocale) - } - if months, ok := c.lang.resources["weeks"]; ok { - slice := strings.Split(months, "|") - if len(slice) == 7 { - return slice[c.Week()] - } - } - return "" + return c.toWeekString("weeks", timezone) } // ToShortWeekString outputs a string in short week layout like "Sun", i18n is supported. // 输出缩写星期字符串,支持i18n func (c Carbon) ToShortWeekString(timezone ...string) string { + return c.toWeekString("short_weeks", timezone) +} + +func (c Carbon) toWeekString(resourceKey string, timezone []string) string { if len(timezone) > 0 { c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) } @@ -101,10 +90,10 @@ func (c Carbon) ToShortWeekString(timezone ...string) string { if len(c.lang.resources) == 0 { c.lang.SetLocale(defaultLocale) } - if months, ok := c.lang.resources["short_weeks"]; ok { + if months, ok := c.lang.resources[resourceKey]; ok { slice := strings.Split(months, "|") - if len(slice) == 7 { - return slice[c.Week()] + if len(slice) == DaysPerWeek { + return slice[c.DayOfWeek()%DaysPerWeek] } } return "" diff --git a/outputer_test.go b/outputer_test.go index 8a3510f9..4083b176 100644 --- a/outputer_test.go +++ b/outputer_test.go @@ -2,10 +2,11 @@ package carbon import ( "fmt" - "github.com/stretchr/testify/assert" "strconv" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestCarbon_String(t *testing.T) { @@ -1789,3 +1790,21 @@ func TestCarbon_ToStdTime(t *testing.T) { actual := Now().ToStdTime().Format(DateTimeLayout) assert.Equal(t, expected, actual) } + +func TestCarbon_Issue200(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input Carbon + expectedWeekString string + expectedShortWeekString string + }{ + {Now().StartOfWeek(), "Sunday", "Sun"}, + {Now().SetWeekStartsAt(Monday).StartOfWeek(), "Monday", "Mon"}, + {Now().SetWeekStartsAt(Wednesday).StartOfWeek(), "Wednesday", "Wed"}, + } + for index, test := range tests { + assert.Equal(test.expectedWeekString, test.input.ToWeekString(), "Current test index is "+strconv.Itoa(index)) + assert.Equal(test.expectedShortWeekString, test.input.ToShortWeekString(), "Current test index is "+strconv.Itoa(index)) + } +}