如何在go中打印基于时间的时区的确切时间



嗨,这次我想转换2021-04-13 14:00:00 +0700 WIB

2021-04-13 21:00:00

基本上我需要根据一些时区位置打印准确的时间,我不能使用这种方法

time.ParseInLocation("02-Jan-2006 15:04:05", someTime.Format("02-Jan-2006 15:04:05"), location)

因为它将返回yy-mm-dd- hh:mm:ss offset timezone

如果您仔细观察,通过在位置解析获得的时间就是在该位置的时间。这不像2021-04-13 14:00:00 +0700 WIB2021-04-13 21:00:00,但事实上,在给定的位置,时间是2021-04-13 14:00:00+0700只显示偏移量,您不必使用偏移量更新时间。

func main() {
now, _ := time.Parse("02-01-2006 15:04:05 -0700", "07-05-2021 12:00:00 +0530")
loc, _ := time.LoadLocation("UTC")
fmt.Printf("UTC Time:       %sn", now.In(loc))
loc, _ = time.LoadLocation("Europe/Berlin")
fmt.Printf("Berlin Time:    %sn", now.In(loc))
loc, _ = time.LoadLocation("America/New_York")
fmt.Printf("New York Time:  %sn", now.In(loc))
loc, _ = time.LoadLocation("Asia/Kolkata")
fmt.Printf("India Time:     %sn", now.In(loc))
loc, _ = time.LoadLocation("Asia/Singapore")
fmt.Printf("Singapore Time: %sn", now.In(loc))
}

上述代码的输出为:

UTC Time:       2021-05-07 06:30:00 +0000 UTC
Berlin Time:    2021-05-07 08:30:00 +0200 CEST
New York Time:  2021-05-07 02:30:00 -0400 EDT
India Time:     2021-05-07 12:00:00 +0530 IST
Singapore Time: 2021-05-07 14:30:00 +0800 +08

每个地点的时间都是指当地时间。偏移量仅显示与GMT的偏移量。

最新更新