将PST时间转换为UTC时间的Golang代码解析。

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

PST to UTC parsing of time in Golang

问题

我正在尝试将时间从PST转换为UTC时区,但是发现一些意外的结果,而将IST转换为UTC则正常工作:

package main

import (
	"fmt"
	"time"
)

func main() {

	const longForm = "2006-01-02 15:04:05 MST"
	t, err := time.Parse(longForm, "2016-01-17 20:04:05 IST")
	fmt.Println(t, err)
	fmt.Printf("IST to UTC: %v\n\n", t.UTC())

	s, err1 := time.Parse(longForm, "2016-01-17 23:04:05 PST")
	fmt.Println(s, err1)
	fmt.Printf("PST to UTC: %v\n\n", s.UTC())

}

输出结果为:

2016-01-17 20:04:05 +0530 IST <nil>
IST to UTC: 2016-01-17 14:34:05 +0000 UTC

2016-01-17 23:04:05 +0000 PST <nil>
PST to UTC: 2016-01-17 23:04:05 +0000 UTC

当解析IST时,显示为+0530,而解析PST时显示为+0000,并且在UTC中打印与PST相同的HH:MM:SS值(23:04:05)。我是否漏掉了什么?

英文:

I am trying to convert the time from PST to UTC timezone but seeing some unexpected result, while IST to UTC is working fine:

package main

import (
	&quot;fmt&quot;
	&quot;time&quot;
)

func main() {

    const longForm = &quot;2006-01-02 15:04:05 MST&quot;
    t, err := time.Parse(longForm, &quot;2016-01-17 20:04:05 IST&quot;)
    fmt.Println(t, err)
    fmt.Printf(&quot;IST to UTC: %v\n\n&quot;, t.UTC())

    s, err1 := time.Parse(longForm, &quot;2016-01-17 23:04:05 PST&quot;)
    fmt.Println(s, err1)
    fmt.Printf(&quot;PST to UTC: %v\n\n&quot;, s.UTC())

}

Output is :

2016-01-17 20:04:05 +0530 IST &lt;nil&gt;
IST to UTC: 2016-01-17 14:34:05 +0000 UTC

2016-01-17 23:04:05 +0000 PST &lt;nil&gt;
PST to UTC: 2016-01-17 23:04:05 +0000 UTC

When parsing is done for IST, it shows +0530, while for PST shows +0000 and in UTC it print same value of HH:MM:SS (23:04:05) as in PST. Am i missing anything here?

答案1

得分: 8

time.Parse() 的文档中提到:

如果时区缩写未知,Parse 将记录时间为位于给定时区缩写和零偏移量的虚构位置。这种选择意味着可以无损地解析和重新格式化此类时间,但表示中使用的确切时刻将与实际时区偏移量不同。为避免此类问题,请优先使用使用数字时区偏移量的时间布局,或使用 ParseInLocation。

以下是使用 ParseInLocation 的示例代码:

IST, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
    fmt.Println(err)
    return
}
PST, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
    fmt.Println(err)
    return
}

const longForm = "2006-01-02 15:04:05 MST"
t, err := time.ParseInLocation(longForm, "2016-01-17 20:04:05 IST", IST)
fmt.Println(t, err)
fmt.Printf("IST to UTC: %v\n\n", t.UTC())

s, err1 := time.ParseInLocation(longForm, "2016-01-17 23:04:05 PST", PST)
fmt.Println(s, err1)
fmt.Printf("PST to UTC: %v\n\n", s.UTC())

输出结果为:

2016-01-17 20:04:05 +0530 IST <nil>
IST to UTC: 2016-01-17 14:34:05 +0000 UTC

2016-01-17 23:04:05 -0800 PST <nil>
PST to UTC: 2016-01-18 07:04:05 +0000 UTC

完整代码请参考 Go Playground

英文:

The documentation for time.Parse() says:

> If the zone abbreviation is unknown, Parse records the time as being in a fabricated location with the given zone abbreviation and a zero offset. This choice means that such a time can be parsed and reformatted with the same layout losslessly, but the exact instant used in the representation will differ by the actual zone offset. To avoid such problems, prefer time layouts that use a numeric zone offset, or use ParseInLocation.

Here is how to use ParseInLocation:

IST, err := time.LoadLocation(&quot;Asia/Kolkata&quot;)
if err != nil {
	fmt.Println(err)
	return
}
PST, err := time.LoadLocation(&quot;America/Los_Angeles&quot;)
if err != nil {
	fmt.Println(err)
	return
}

const longForm = &quot;2006-01-02 15:04:05 MST&quot;
t, err := time.ParseInLocation(longForm, &quot;2016-01-17 20:04:05 IST&quot;, IST)
fmt.Println(t, err)
fmt.Printf(&quot;IST to UTC: %v\n\n&quot;, t.UTC())

s, err1 := time.ParseInLocation(longForm, &quot;2016-01-17 23:04:05 PST&quot;, PST)
fmt.Println(s, err1)
fmt.Printf(&quot;PST to UTC: %v\n\n&quot;, s.UTC())

Output:

2016-01-17 20:04:05 +0530 IST &lt;nil&gt;
IST to UTC: 2016-01-17 14:34:05 +0000 UTC

2016-01-17 23:04:05 -0800 PST &lt;nil&gt;
PST to UTC: 2016-01-18 07:04:05 +0000 UTC

Full code on the Go Playground

答案2

得分: 4

time.Parse() 的文档中提到:

如果时区缩写未知,Parse 将记录时间为位于一个虚构的位置,该位置具有给定的时区缩写和零偏移量。这种选择意味着可以使用相同的布局无损地解析和重新格式化这样的时间,但在表示中使用的确切时刻将因实际时区偏移而异。为避免此类问题,请优先使用使用数字时区偏移的时间布局,或使用 ParseInLocation。

所以,系统不知道"PST"是什么。对我来说,系统也不知道"IST"是什么。你可以这样检查支持的位置:

package main

import (
    "fmt"
    "time"
)

func main() {
    for _, name := range []string{"MST", "UTC", "IST", "PST", "EST", "PT"} {
        loc, err := time.LoadLocation(name)
        if err != nil {
            fmt.Println("No location", name)
        } else {
            fmt.Println("Location", name, "is", loc)
        }
    }
}

在我的系统上输出为:

Location MST is MST
Location UTC is UTC
No location IST
No location PST
Location EST is EST
No location PT
英文:

The documentation for time.Parse() says:

> If the zone abbreviation is unknown, Parse records the time as being in a fabricated location with the given zone abbreviation and a zero offset. This choice means that such a time can be parsed and reformatted with the same layout losslessly, but the exact instant used in the representation will differ by the actual zone offset. To avoid such problems, prefer time layouts that use a numeric zone offset, or use ParseInLocation.

So, the system doesn't know what "PST" is. For me, the system also doesn't know what IST is. You can check for supported locations like so:

package main

import (
    &quot;fmt&quot;
    &quot;time&quot;
)

func main() {
    for _, name := range []string{&quot;MST&quot;, &quot;UTC&quot;, &quot;IST&quot;, &quot;PST&quot;, &quot;EST&quot;, &quot;PT&quot;} {
        loc, err := time.LoadLocation(name)
        if err != nil {
            fmt.Println(&quot;No location&quot;, name)
        } else {
            fmt.Println(&quot;Location&quot;, name, &quot;is&quot;, loc)
        }
    }
}

Output on my system:

Location MST is MST
Location UTC is UTC
No location IST
No location PST
Location EST is EST
No location PT

huangapple
  • 本文由 发表于 2017年2月17日 15:11:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/42291569.html
匿名

发表评论

匿名网友

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

确定