Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse方法发现个bug #47

Closed
ayamzh opened this issue Mar 17, 2021 · 9 comments
Closed

parse方法发现个bug #47

ayamzh opened this issue Mar 17, 2021 · 9 comments
Labels
Bug Report a reproducible bug or regression Fixed Bug fixed

Comments

@ayamzh
Copy link

ayamzh commented Mar 17, 2021

parse方法使用的是本地时区,而没有读取carbon示例里设置的时区,carbon.sertimezone().parse("").timestamp()不正确无法根据时区变化

@gouguoyin
Copy link
Member

timestamp()是获取时间戳的,时间戳定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。地球上任何时区在任意时间点的时间戳都是相同的。但是不同时区,相同时间戳对应的当前时间不同。

@ayamzh
Copy link
Author

ayamzh commented Mar 17, 2021

举得例子可能说的不太明确
fmt.Println(carbon.SetTimezone(carbon.LosAngeles).Parse("2020-01-01").ToTimestamp())
fmt.Println(carbon.SetTimezone(carbon.Shanghai).Parse("2020-01-01").ToTimestamp())
期望的结果应该是上面打印的是洛杉矶1月1号对应的时间戳,下面是北京时间的1月1号对应的时间戳,实际结果2条数据一样都是取得本地时间。。。
因为底层都走的这段代码,这段代码写死了时区是local了

// parseByLayout 通过布局模板解析
func parseByLayout(value string, layout string) (time.Time, error) {
	loc, _ := time.LoadLocation(Local)
	tt, err := time.ParseInLocation(layout, value, loc)
	if err != nil {
		err = errors.New("the value \"" + value + "\" can't parse string as time")
	}
	return tt, err
}

@gouguoyin
Copy link
Member

这里没问题,虽然parse()方法确实使用了parseBy Layout()方法,但是并没有将接收的carbon对象的Loc设置为Local。实际上,真正时区起作用的是display. go和getter. go里的方法,这些方法调用的是c. Loc

@gouguoyin gouguoyin reopened this Mar 17, 2021
@gouguoyin
Copy link
Member

你举的例子中,洛杉矶和北京1月1号对应的时间戳本来就是一样的,任何时区对应的时间戳都是一样的。时间戳不随时区改变而改变。

@ayamzh
Copy link
Author

ayamzh commented Mar 17, 2021 via email

@gouguoyin
Copy link
Member

不同时区对应的时间戳是相同的,但是同一时间戳对应不同时区的日期不同

@ayamzh
Copy link
Author

ayamzh commented Mar 17, 2021 via email

@open-git
Copy link
Contributor

open-git commented Mar 17, 2021 via email

@gouguoyin
Copy link
Member

已修复

@golang-module golang-module deleted a comment from ayamzh Apr 5, 2021
@golang-module golang-module deleted a comment from ayamzh Apr 5, 2021
@gouguoyin gouguoyin added the Bug Report a reproducible bug or regression label Jul 24, 2021
@gouguoyin gouguoyin added the Fixed Bug fixed label Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Report a reproducible bug or regression Fixed Bug fixed
Development

No branches or pull requests

3 participants