英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论