golang-module / carbon

A simple, semantic and developer-friendly golang package for time

Home Page:https://pkg.go.dev/github.com/golang-module/carbon/v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`ToWeekString()` ignores `SetWeekStartsAt()`

vaughany opened this issue · comments

Hi there.

I encountered an issue with the following code (which is quite verbose on purpose, showing 'now', the start of the week by default (Sunday), the start of the week set to Monday, and then again as Wednesday):

package main

import (
	"fmt"

	"github.com/golang-module/carbon/v2"
)

func main() {
	now := carbon.Now()
	fmt.Println("Now:", now)
	fmt.Println("Now:", now.ToWeekString())

	sunday := now.StartOfWeek()
	fmt.Println("Sunday:", sunday)
	fmt.Println("Sunday:", sunday.ToWeekString())

	monday := now.SetWeekStartsAt(carbon.Monday).StartOfWeek()
	fmt.Println("Monday:", monday)
	fmt.Println("Monday:", monday.ToWeekString())

	wednesday := now.SetWeekStartsAt(carbon.Wednesday).StartOfWeek()
	fmt.Println("Wednesday:", wednesday)
	fmt.Println("Wednesday:", wednesday.ToWeekString())
}

golang version: go1.21.4 linux/amd64

carbon version: 2.2.13

time zone: GMT

I expected to get:

Now: 2023-11-16 15:48:54
Now: Thursday

Sunday: 2023-11-12 00:00:00
Sunday: Sunday

Monday: 2023-11-13 00:00:00
Monday: Monday

Wednesday: 2023-11-15 00:00:00
Wednesday: Wednesday

But I actually get:

Now: 2023-11-16 15:48:54
Now: Thursday

Sunday: 2023-11-12 00:00:00
Sunday: Sunday

Monday: 2023-11-13 00:00:00
Monday: Sunday

Wednesday: 2023-11-15 00:00:00
Wednesday: Sunday

It seems that the ToWeekString() function does not take into account the SetWeekStartsAt(carbon.Wednesday) function. (The same is also true of the ToShortWeekString() function.

Thanks!