Golang时间 – 时区显示两次

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

Golang time - time zone showing twice

问题

运行此代码后,结果应该显示日期、时间和时区。

令人惊讶的是,结果显示时区两次,我无法弄清楚为什么。

package main

import (
	"fmt"
	"time"
)

func main() {

	mytime, _ := time.Parse("02/Jan/2006:15:04:05 -0700", "07/Apr/2017:01:26:05 +0530")

	fmt.Println(mytime)

}

运行结果为:

2017-04-07 01:26:05 +0530 +0530

所以我的问题是为什么时区会显示两次?

英文:

On running this code the result should show date time and zone

Surprisingly the result shows time zone twice and am not able to figure out why

package main

import (
	"fmt"
	"time"
)

func main() {

	mytime, _ := time.Parse("02/Jan/2006:15:04:05 -0700", "07/Apr/2017:01:26:05 +0530")

	fmt.Println(mytime)

}

Output of this is

2017-04-07 01:26:05 +0530 +0530

So my question is why timezone showing twice ?

答案1

得分: 12

fmt.Println调用Time.String()函数,该函数以以下格式返回时间:

"2006-01-02 15:04:05.999999999 -0700 MST"

如您所见,它包含时区偏移量和时区名称。

在您的情况下,时间没有已知的时区名称,因此它会输出两次偏移量。

参考资料:

英文:

The fmt.Println invokes the Time's .String() function that returns the time in the following format:

"2006-01-02 15:04:05.999999999 -0700 MST"

Which as you see contains both the timezone offset and the timezone name.

In your case there is no timezone name known for the time, so it outputs the offset twice.

References:

huangapple
  • 本文由 发表于 2017年4月7日 04:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/43264783.html
匿名

发表评论

匿名网友

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

确定