与自定义布局的时间 [英] time.Parse with custom layout

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

问题描述

我试图将这个字符串模式4-JAN-12 9:30:14解析为一个 time.Time code>。



试过 time.Parse(2-JAN-06 15:04:05,inputString)和其他许多人,但不能得到它的工作。



我读过 http://golang.org/pkg/time/#Parse https://gobyexample.com/time-formatting-parsing ,但似乎没有任何这样的例子。



谢谢!

p>

编辑:
完整代码:

 键入CustomTime时间。时间

func(t * CustomTime)UnmarshalJSON(b []字节)错误{
auxTime,err:= time.Parse(2-JAN-06 15:04:05,字符串(b))
* t = CustomTime(auxTime)
返回err
}




解析时间10-JAN-12 11:20:41as 2 -JAN-06 15:04:05:can
解析 24-JAN-15 1 0:27:44as2



解决方案

不知道你做错了(应该发布你的代码),但它实际上只是一个简单的函数调用:

  s:=4-JAN -12 9:30:14
t,err:= time.Parse(2-JAN-06 15:04:05,s)
fmt.Println(t,err)

输出:

  2012-01-04 09:30:14 +0000 UTC< nil> 

试试 Go Goground



请注意, time.Parse() 返回2个值:解析后的 time.Time 值(如果解析成功)以及可选的 错误 的值(如果解析失败)。

请参阅下面的示例,我故意指定了错误的输入字符串:

  s:=34-JAN-12 9:30:14

如果t,err:=时间。解析(2-JAN-06 15:04:05,s); err == nil {
fmt.Println(Success:,t)
} else {
fmt.Println(Failure:,err)
}

输出:

 失败:解析时间34-JAN-12 9:30:14:天超出范围

去游乐场试试它。



编辑:

现在您发布了代码和错误消息,您的问题是您的输入字符串包含前导尾随引号!



删除前导引号和尾引号,它将起作用。这是你的情况:

  s:=`4-JAN-12 9:30:14`

s = s [1:len(s)-1]
if t,err:= time.Parse(2-JAN-06 15:04:05,s); err == nil {
fmt.Println(Success:,t)
} else {
fmt.Println(Failure:,err)
}

输出(在 Go Playground ):

 成功:2012-01-04 09:30 :14 +0000 UTC 


I'm trying to parse this string pattern "4-JAN-12 9:30:14" into a time.Time.

Tried time.Parse("2-JAN-06 15:04:05", inputString) and many others but cannot get it working.

I've read http://golang.org/pkg/time/#Parse and https://gobyexample.com/time-formatting-parsing but it seems there aren't any examples like this.

Thanks!

Edit: full code:

type CustomTime time.Time

func (t *CustomTime) UnmarshalJSON(b []byte) error {
    auxTime, err := time.Parse("2-JAN-06 15:04:05", string(b))
    *t = CustomTime(auxTime)
    return err
}

parsing time ""10-JAN-12 11:20:41"" as "2-JAN-06 15:04:05": cannot parse ""24-JAN-15 10:27:44"" as "2"

解决方案

Don't know what you did wrong (should post your code), but it is really just a simple function call:

s := "4-JAN-12 9:30:14"
t, err := time.Parse("2-JAN-06 15:04:05", s)
fmt.Println(t, err)

Outputs:

2012-01-04 09:30:14 +0000 UTC <nil>

Try it on the Go Playground.

Note that time.Parse() returns 2 values: the parsed time.Time value (if parsing succeeds) and an optional error value (if parsing fails).

See the following example where I intentionally specify a wrong input string:

s := "34-JAN-12 9:30:14"

if t, err := time.Parse("2-JAN-06 15:04:05", s); err == nil {
    fmt.Println("Success:", t)
} else {
    fmt.Println("Failure:", err)
}

Output:

Failure: parsing time "34-JAN-12 9:30:14": day out of range

Try it on the Go Playground.

EDIT:

Now that you posted code and error message, your problem is that your input string contains a leading and trailing quotation mark!

Remove the leading and trailing quotation mark and it will work. This is your case:

s := `"4-JAN-12 9:30:14"`

s = s[1 : len(s)-1]
if t, err := time.Parse("2-JAN-06 15:04:05", s); err == nil {
    fmt.Println("Success:", t)
} else {
    fmt.Println("Failure:", err)
}

Output (try it on the Go Playground):

Success: 2012-01-04 09:30:14 +0000 UTC

这篇关于与自定义布局的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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