diff --git a/outputer.go b/outputer.go index f8bcbf2b..5a6aa43e 100644 --- a/outputer.go +++ b/outputer.go @@ -92,6 +92,10 @@ func (c Carbon) ToWeekString(timezone ...string) string { // 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[0]) } @@ -101,10 +105,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)) + } +}