修复 'ToWeekString' 方法忽略 SetWeekStartsAt设置的bug

This commit is contained in:
Peleus 2023-11-30 15:53:05 +08:00
parent b09d48d6de
commit 8a0bbf1487
2 changed files with 23 additions and 4 deletions

View File

@ -82,8 +82,8 @@ func (c Carbon) ToWeekString(timezone ...string) string {
}
if months, ok := c.lang.resources["weeks"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 7 {
return slice[c.Week()]
if len(slice) == DaysPerWeek {
return slice[c.DayOfWeek()%DaysPerWeek]
}
}
return ""
@ -103,8 +103,8 @@ func (c Carbon) ToShortWeekString(timezone ...string) string {
}
if months, ok := c.lang.resources["short_weeks"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 7 {
return slice[c.Week()]
if len(slice) == DaysPerWeek {
return slice[c.DayOfWeek()%DaysPerWeek]
}
}
return ""

View File

@ -1789,3 +1789,22 @@ func TestCarbon_ToStdTime(t *testing.T) {
actual := Now().ToStdTime().Format(DateTimeLayout)
assert.Equal(t, expected, actual)
}
// https://github.com/golang-module/carbon/issues/200
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))
}
}