使用Go解析Go时间戳

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

Parse Go time stamp using Go

问题

Go使用time.Now().String()打印时间,结果可能是以下两种格式之一:

2012-12-18 06:09:18.6155554 +0200 FLEST

或者

2009-11-10 23:00:00 +0000 UTC

我想FLEST代表的是芬兰-拉脱维亚-爱沙尼亚标准时间。我不在这些国家,而且我可能会遇到各种各样的时区。我找不到一种统一的方式或模式来使用time.Parse解析它。

英文:

Go prints time with

time.Now().String()

as

2012-12-18 06:09:18.6155554 +0200 FLEST

or

2009-11-10 23:00:00 +0000 UTC

http://play.golang.org/p/8qwq9U_Ri5

How do I parse it?

I guess FLEST is Finland Latvian Estonian Standard Time
I am not in these countries and I guess I can get all kind of time zones.
I can't find one unified way or pattern to parse it with time.Parse

答案1

得分: 12

尽管time.Parse()接受格式字符串,例如2006-01-02 15:04:05 -0700 MST,但使用time中定义的常量可能更简单。

const (
    ANSIC       = "Mon Jan _2 15:04:05 2006"
    UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
    RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
    RFC822      = "02 Jan 06 15:04 MST"
    RFC822Z     = "02 Jan 06 15:04 -0700" // 带有数字时区的RFC822
    RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
    RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
    RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // 带有数字时区的RFC1123
    RFC3339     = "2006-01-02T15:04:05Z07:00"
    RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
    Kitchen     = "3:04PM"
    // 方便的时间戳。
    Stamp      = "Jan _2 15:04:05"
    StampMilli = "Jan _2 15:04:05.000"
    StampMicro = "Jan _2 15:04:05.000000"
    StampNano  = "Jan _2 15:04:05.000000000"
)

如果您将字符串用作存储或编码时间的方式(例如使用限制性编码格式),您可能希望考虑使用Unix时间。这样,您只需存储一个int64(或两个,如果保留纳秒数)。

英文:

Though time.Parse() accepts a format string such as 2006-01-02 15:04:05 -0700 MST, it may be simpler to use one of the constants defined in time.

const (
    ANSIC       = "Mon Jan _2 15:04:05 2006"
    UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
    RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
    RFC822      = "02 Jan 06 15:04 MST"
    RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
    RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
    RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
    RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
    RFC3339     = "2006-01-02T15:04:05Z07:00"
    RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
    Kitchen     = "3:04PM"
    // Handy time stamps.
    Stamp      = "Jan _2 15:04:05"
    StampMilli = "Jan _2 15:04:05.000"
    StampMicro = "Jan _2 15:04:05.000000"
    StampNano  = "Jan _2 15:04:05.000000000"
)

If you're using the strings as a way to store or encode time, (such as with a restrictive encoding format,) you may want to consider using Unix time. That way, you could just store an int64 (or two, if you keep the number of nanoseconds.

答案2

得分: 10

package main

import (
"fmt"
"time"
)

func main() {
fmt.Println(time.Now())
date := "2009-11-10 23:00:00 +0000 UTC"
t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", date)
if err != nil {
fmt.Println("parse error", err.Error())
}
fmt.Println(t.Format(time.ANSIC))
}

英文:
package main

import (
"fmt"
"time"
)

func main() {
	fmt.Println(time.Now())
	date := "2009-11-10 23:00:00 +0000 UTC"
	t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", date)
        if err != nil {
                fmt.Println("parse error", err.Error())
        }
        fmt.Println(t.Format(time.ANSIC))
}

Playground: http://play.golang.org/p/hvqBgtesLd

See the source code at http://golang.org/src/pkg/time/format.go?s=15404:15450#L607

答案3

得分: 5

time.String的文档提供了它使用的格式:“2006-01-02 15:04:05.999999999 -0700 MST”。一个开始的方法是使用相同的格式进行解析。

时区可能会成为一个问题。如果你必须解析你知道是由time.String生成的时间,但是生成在其他时区的时间,你必须拥有其他时区的zoneinfo。请参阅LoadLocation下的文档。如果你无法获取zoneinfo,无法在系统上安装它,或者无法冒险在一些新的未知时区上失败,那么time.String格式不适合你。你将不得不以不同的格式获取时间戳,或者从字符串中删除时区,并使用修改后的格式解析修改后的字符串。

英文:

The documentation for time.String gives the format it uses: "2006-01-02 15:04:05.999999999 -0700 MST". A start would be to use the same format for parsing.

Time zones may be a problem for you though. If you must parse times that you know were produced with time.String, but were produced in other time zones, you must have the zoneinfo for the other time zones. See the documentation under LoadLocation. If you cannot obtain the zoneinfo, cannot install it on your system, or cannot risk failing on some new unknown time zone, then the time.String format is not for you. You will have to obtain time stamps in a different format, or remove the time zone from the strings and parse the modified strings with a modified format.

huangapple
  • 本文由 发表于 2012年12月31日 04:10:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/14094228.html
匿名

发表评论

匿名网友

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

确定