time.Parse行为

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

time.Parse behaviour

问题

在Go语言中,尝试将字符串转换为time.Time时,使用time包的Parse方法无法返回预期的结果。问题似乎与时区有关。我想要改为使用ISO 8601格式的日期和时间,并且使用UTC时间。

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. const longForm = "2013-05-13T18:41:34.848Z"
  8. //即使这样也不起作用
  9. //const longForm = "2013-05-13 18:41:34.848 -0700 PDT"
  10. t, _ := time.Parse(longForm, "2013-05-13 18:41:34.848 -0700 PDT")
  11. fmt.Println(t)
  12. //输出结果为 0001-01-01 00:00:00 +0000 UTC
  13. }

提前感谢!

英文:

In Go, while trying to convert string to time.Time, using time package's Parse method doesn't return expected result. It seems problem is with the timezone. I want to change to ISO 8601 combined with date and time in UTC.

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. const longForm = "2013-05-13T18:41:34.848Z"
  8. //even this is not working
  9. //const longForm = "2013-05-13 18:41:34.848 -0700 PDT"
  10. t, _ := time.Parse(longForm, "2013-05-13 18:41:34.848 -0700 PDT")
  11. fmt.Println(t)
  12. //outputs 0001-01-01 00:00:00 +0000 UTC
  13. }

thanks in advance!

答案1

得分: 6

你的格式字符串longForm不正确。如果你没有忽略返回的错误,你就会知道这一点。引用文档

这些是用于Time.Format和Time.Parse的预定义布局。布局中使用的参考时间是:

  1. Mon Jan 2 15:04:05 MST 2006

这相当于Unix时间1136239445。由于MST是GMT-0700,参考时间可以被认为是

  1. 01/02 03:04:05PM '06 -0700

要定义自己的格式,请写下参考时间按你的方式格式化后的样子;参考常量值如ANSIC、StampMicro或Kitchen的示例。

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. )
  7. func main() {
  8. const longForm = "2006-01-02 15:04:05 -0700"
  9. t, err := time.Parse(longForm, "2013-05-13 18:41:34.848 -0700")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(t)
  14. }

Playground


输出:

  1. 2013-05-13 01:41:34.848 +0000 UTC
英文:

Your format string longForm is not correct. You would know that if you would have not been ignoring the returned error. Quoting the docs:

> These are predefined layouts for use in Time.Format and Time.Parse. The reference time used in the layouts is:

  1. Mon Jan 2 15:04:05 MST 2006

> which is Unix time 1136239445. Since MST is GMT-0700, the reference time can be thought of as

  1. 01/02 03:04:05PM '06 -0700

> To define your own format, write down what the reference time would look like formatted your way; see the values of constants like ANSIC, StampMicro or Kitchen for examples.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. )
  7. func main() {
  8. const longForm = "2006-01-02 15:04:05 -0700"
  9. t, err := time.Parse(longForm, "2013-05-13 18:41:34.848 -0700")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(t)
  14. }

Playground


Output:

  1. 2013-05-13 01:41:34.848 +0000 UTC

答案2

得分: 2

time.Parse使用特殊的时间格式值,并期望将格式与这些值一起传递。

如果传递正确的值,它将以正确的方式解析时间。

因此,将年份传递为2006,月份传递为01,以此类推...

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. const longForm = "2006-01-02 15:04:05.000 -0700 PDT"
  8. t, err := time.Parse(longForm, "2013-05-13 18:41:34.848 -0700 PDT")
  9. fmt.Println(t.UTC(), err)
  10. //输出 2013-05-14 01:41:34.848 +0000 UTC <nil>
  11. }
英文:

time.Parse uses special values for time formatting, and expecting the format to be passed with those values.

If you pass correct values, it will parse the time in the correct manner.

So passing year as 2006, month as 01 and goes on like that...

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;time&quot;
  5. )
  6. func main() {
  7. const longForm = &quot;2006-01-02 15:04:05.000 -0700 PDT&quot;
  8. t, err := time.Parse(longForm, &quot;2013-05-13 18:41:34.848 -0700 PDT&quot;)
  9. fmt.Println(t.UTC(), err)
  10. //outputs 2013-05-14 01:41:34.848 +0000 UTC &lt;nil&gt;
  11. }

huangapple
  • 本文由 发表于 2013年5月14日 14:07:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/16536216.html
匿名

发表评论

匿名网友

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

确定