初始化为0 UTC时间,在打印时会生成一个负时间。

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

Initalizing 0 UTC Time generates a negative time when printed

问题

在提供的代码片段中,当打印出来时,显式初始化为0 UTC的t2时间是负数。

我不明白为什么会这样,有人可以为我解释一下吗?

func main() {
    const IsoDatetimeFormat = "2006-01-02T15:04:05"
    t1 := time.Time{}
    t2 := time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
    fmt.Println(t1.Format(IsoDatetimeFormat))
    fmt.Println(t2.Format(IsoDatetimeFormat))
}

输出结果:

0001-01-01T00:00:00

-0001-11-30T00:00:00

playground链接

英文:

In the provided code snippet the t2 time initialized explicitly to 0 UTC is negative when printed.

I don't understand why that is, could someone explain it for me?

func main() {
	const IsoDatetimeFormat = "2006-01-02T15:04:05"
	t1 := time.Time{}
	t2 := time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
	fmt.Println(t1.Format(IsoDatetimeFormat))
	fmt.Println(t2.Format(IsoDatetimeFormat))
}

Output:

0001-01-01T00:00:00

-0001-11-30T00:00:00

playground link

答案1

得分: 3

time.Time文档说明time.Time的零值表示/意味着公元1年1月1日00:00:00.000000000 UTC:

> Time类型的零值是公元1年1月1日00:00:00.000000000 UTC。由于这个时间在实践中不太可能出现,IsZero方法提供了一种简单的方法来检测未显式初始化的时间。

不存在公元0年,因此如果使用小于1的年、月和日值,它们将会回滚为负数。time包接受并规范化超出有效范围的值。引用自time.Date()

> 月、日、小时、分钟、秒和纳秒的值可能超出它们通常的范围,并在转换过程中进行规范化。例如,10月32日转换为11月1日。

将零值设为公元1年1月1日是一个任意的选择。至于选择公元1年的原因:

// Time类型的零值被定义为
// 公元1年1月1日00:00:00.000000000 UTC
// 这个时间(1-1-1 00:00:00 UTC)看起来像一个零,或者尽可能接近一个日期的零
// 它在实践中不太可能出现,因此适合作为“未设置”的标记,不像1970年1月1日,而且
// 即使在UTC以西的时区,年份也是非负的,不像0年1月1日
// 00:00:00 UTC,在纽约将会是12-31-(-1) 19:00:00。
英文:

time.Time documents that the zero value for time.Time represents / means January 1, year 1, 00:00:00.000000000 UTC:

> The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC. As this time is unlikely to come up in practice, the IsZero method gives a simple way of detecting a time that has not been initialized explicitly.

There is no year 0, so if you use year, month and day values smaller than 1, they will roll over to negative. The time package accepts and normalizes values given outside of valid ranges. Quoting from time.Date():

> The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.

The zero value to be January 1, year 1 was an arbitrary choice. As to reasoning why year 1 was chosen:

// The zero value for a Time is defined to be
//	January 1, year 1, 00:00:00.000000000 UTC
// which (1) looks like a zero, or as close as you can get in a date
// (1-1-1 00:00:00 UTC), (2) is unlikely enough to arise in practice to
// be a suitable "not set" sentinel, unlike Jan 1 1970, and (3) has a
// non-negative year even in time zones west of UTC, unlike 1-1-0
// 00:00:00 UTC, which would be 12-31-(-1) 19:00:00 in New York.

huangapple
  • 本文由 发表于 2021年9月2日 02:08:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/69018708.html
匿名

发表评论

匿名网友

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

确定