时间粗糙的行为 [英] time.Parse behaviour

查看:208
本文介绍了时间粗糙的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Go中,尝试将字符串转换为 time.Time 时,使用时间包的Parse方法不会返回预期的结果。似乎问题是与时区。我想更改为ISO 8601结合日期和时间在UTC。

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.

package main

import (
    "fmt"
    "time"
)

func main() {
    const longForm = "2013-05-13T18:41:34.848Z"
    //even this is not working
    //const longForm = "2013-05-13 18:41:34.848 -0700 PDT"
    t, _ := time.Parse(longForm, "2013-05-13 18:41:34.848 -0700 PDT")
    fmt.Println(t)
    //outputs 0001-01-01 00:00:00 +0000 UTC
}

提前谢谢

推荐答案

time.Parse 使用特别时间格式的值,并且期望使用这些值传递格式。

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.

所以通过2006年的一个月,一个月为01,继续这样...

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

package main

import (
    "fmt"
    "time"
)

func main() {
    const longForm = "2006-01-02 15:04:05.000 -0700 PDT"
    t, err := time.Parse(longForm, "2013-05-13 18:41:34.848 -0700 PDT")
    fmt.Println(t.UTC(), err)
    //outputs 2013-05-14 01:41:34.848 +0000 UTC <nil>
}

这篇关于时间粗糙的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆