Go编程语言如何处理日期和时间?

huangapple go评论94阅读模式
英文:

How does the Go programming language handle dates and times?

问题

整个日期和时间的概念非常不稳定,不断更新,所以从一开始就是一项非常困难的任务。从闰年到闰秒,有很多变量需要考虑。

John Skeet的库看起来不错,但他花了3年时间才将其发展到这一步,而且仍然远未完善。

有人能告诉我Go编程语言在处理日期时间方面与其他语言/库有何不同或相似吗?这纯粹是出于好奇。

这个问题更多地涉及当前可用库之间的相似性和差异性 - 最好用英语回答,而不是一大堆文档页面。

英文:

The whole concept of Dates and Times is incredibly flaky and constantly being updated so from the beginning it's an incredibly difficult task. From leap years to leap seconds there are so many variables to take into account.

John Skeet's library looks good but has taken him 3 years to get it this far and it's still far from perfect.

Could someone give me an indication on how the Go programming language handled DateTime's differently or similarly compared to other languages/libraries? This is purely for curiosity's sake.

This question is more about the similarities and differences between current available libraries - preferably in english, not pages and pages of documentation.

答案1

得分: 7

查看Go time package的文档和源代码。Time表示具有纳秒精度且没有闰秒的时间点。IANA时区数据库用于处理时区和夏令时。时区数据库包含代表全球许多代表性位置的本地时间历史的代码和数据。它定期更新以反映政治机构对时区边界、UTC偏移和夏令时规则的更改。

type Time struct {
	// sec表示自公元1年1月1日00:00:00 UTC以来经过的秒数。
	sec int64

	// nsec指定了秒数中的非负纳秒偏移量。
	// 它必须在[0, 999999999]范围内。
	nsec int32

	// loc指定应该用于确定与此Time对应的分钟、小时、月份、日期和年份的位置。
	// 只有零Time具有nil Location。
	// 在这种情况下,它被解释为UTC。
	loc *Location
}
英文:

See the Go time package documentation and source code. A Time represents an instant in time with nanosecond precision without leap seconds. The IANA Time Zone Database is used for time zone and daylight savings time. The time zone database contains code and data that represent the history of local time for many representative locations around the globe. It is updated periodically to reflect changes made by political bodies to time zone boundaries, UTC offsets, and daylight-saving rules.

type Time struct {
	// sec gives the number of seconds elapsed since
	// January 1, year 1 00:00:00 UTC.
	sec int64

	// nsec specifies a non-negative nanosecond
	// offset within the second named by Seconds.
	// It must be in the range [0, 999999999].
	nsec int32

	// loc specifies the Location that should be used to
	// determine the minute, hour, month, day, and year
	// that correspond to this Time.
	// Only the zero Time has a nil Location.
	// In that case it is interpreted to mean UTC.
	loc *Location
}

huangapple
  • 本文由 发表于 2013年3月18日 14:16:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/15470768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定