Skip to content

use StartOfHour and SetLocation #292

@kamly

Description

@kamly

Hello,

I encountered an issue with the following code:

func Demo(t time.Time) {
	fmt.Println(t.Location())

	tt := carbon.CreateFromStdTime(t).
		SetLocation(t.Location()).
		StartOfHour().
		StdTime()

	fmt.Println(tt)

	fmt.Println(time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()))
}

func Test_demo2(t *testing.T) {
	loc, _ := time.LoadLocation("Asia/Shanghai")
	Demo(time.Date(2024, 3, 15, 14, 30, 45, 123456789, loc))
}

golang version: go version go1.23.4 darwin/amd64

carbon version: v2.6.1

time zone: Asia/Shanghai

I expected to get:

Asia/Shanghai
2024-03-15 14:00:00 +0800 CST
2024-03-15 14:00:00 +0800 CST

But I actually get:

Asia/Shanghai
2024-03-15 14:00:00 +0000 UTC  --  error, timezone not right
2024-03-15 14:00:00 +0800 CST

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReport a reproducible bug or regression

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions