Coder Social home page Coder Social logo

parse方法发现个bug about carbon HOT 9 CLOSED

golang-module avatar golang-module commented on May 22, 2024
parse方法发现个bug

from carbon.

Comments (9)

gouguoyin avatar gouguoyin commented on May 22, 2024

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

from carbon.

ayamzh avatar ayamzh commented on May 22, 2024

举得例子可能说的不太明确
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
}

from carbon.

gouguoyin avatar gouguoyin commented on May 22, 2024

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

from carbon.

gouguoyin avatar gouguoyin commented on May 22, 2024

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

from carbon.

ayamzh avatar ayamzh commented on May 22, 2024

from carbon.

gouguoyin avatar gouguoyin commented on May 22, 2024

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

from carbon.

ayamzh avatar ayamzh commented on May 22, 2024

from carbon.

open-git avatar open-git commented on May 22, 2024

from carbon.

gouguoyin avatar gouguoyin commented on May 22, 2024

已修复

from carbon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.