如何在Go中格式化本地时间对象,并确保分钟正确?

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

How to format a local time object in Go with correct minutes?

问题

我正在尝试将UTC(+0000)时间格式化为本地时间(在我这种情况下是东部时间),而不硬编码任何时区偏移量(以避免实现夏令时校正)。

我有以下代码展示了我遇到的问题:

package main

import (
	"fmt"
	"time"
)

func main() {
	// 这里加载时区
	timezone, _ := time.LoadLocation("America/New_York")

	// 解析时间
	t, _ := time.Parse("Mon Jan 2 15:04:05 +0000 2006", "Tue Jul 07 10:38:18 +0000 2015")

	// 这看起来是正确的,它仍然是一个UTC时间
	fmt.Println(t)
	// 2015-07-07 10:38:18 +0000 UTC

	// 这似乎没问题 - 减去4小时以转换为东部时间
	t = t.In(timezone)
	fmt.Println(t)
	// 2015-07-07 06:38:18 -0400 EDT

	// 这打印的是上午6:07,完全不正确,应该是上午6:38
	fmt.Println(t.Format("Monday Jan 2, 3:01pm"))
	// Tuesday Jul 7, 6:07am
}

所以在我看来,它解析和转换时区都没问题,但是当我使用格式化输出时,它会去掉分钟并使用07。无论我将分钟设置为什么,它总是显示为07。

英文:

Edit: I have updated the question with code that highlights why the alleged duplicate's solution doesn't work for me

I am trying to take UTC (+0000) times and format them into local times (eastern time in my case) without hard coding any timezone offsets (as to avoid implementing dst correction).

I have the following code which demonstrates the problem I am having

package main

import (
	"fmt"
	"time"
)

func main() {
	// Here I load the timezone
	timezone, _ := time.LoadLocation("America/New_York")

	// I parse the time
	t, _ := time.Parse("Mon Jan 2 15:04:05 +0000 2006", "Tue Jul 07 10:38:18 +0000 2015")

	// This looks correct, it's still a utc time
	fmt.Println(t)
	// 2015-07-07 10:38:18 +0000 UTC

	// This seems to be fine - -4 hours to convert to est
	t = t.In(timezone)
	fmt.Println(t)
	// 2015-07-07 06:38:18 -0400 EDT

	// This prints 6:07am, completely incorrect as it should be 6:38am
	fmt.Println(t.Format("Monday Jan 2, 3:01pm"))
	// Tuesday Jul 7, 6:07am
}

(https://play.golang.org/p/e57slFhWFk)

So to me it seems that it parses and converts timezones fine, but when I output it using format, it gets rid of the minutes and uses 07. It doesn't matter what I set the minutes to, it always comes out as 07.

答案1

得分: 1

你的布局(格式)字符串是错误的。如 time 包的文档中所述,布局字符串必须表示以下时间格式:

Mon Jan 2 15:04:05 MST 2006

在解析时,使用以下格式字符串:

t, _ := time.Parse("Mon Jan 02 15:04:05 -0700 2006", "Tue Jul 07 10:38:18 +0000 2015")

在打印时,使用以下格式字符串:

fmt.Println(t.Format("Monday Jan 2, 3:04pm"))

这将得到你期望的输出:

Tuesday Jul 7, 6:38am

Go Playground 上试一试。

英文:

Your layout (format) strings are incorrect. As noted in the doc of the time package, the layout string must denote this time:

Mon Jan 2 15:04:05 MST 2006

When parsing, use the following format string:

t, _ := time.Parse("Mon Jan 02 15:04:05 -0700 2006", "Tue Jul 07 10:38:18 +0000 2015")

And when printing, use this format string:

fmt.Println(t.Format("Monday Jan 2, 3:04pm"))

This will result in your expected output:

Tuesday Jul 7, 6:38am

Try it on the Go Playground.

huangapple
  • 本文由 发表于 2015年7月8日 04:46:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/31278948.html
匿名

发表评论

匿名网友

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

确定