简体   繁体   English

前往:time.Parse()问题

[英]Go: time.Parse() issue

I have the following code: 我有以下代码:

timeLocal := "01/July/2015:18:12:25 +0900"
inFormat := "02/January/2006:15:04:05 -0700"
parsed, err := time.Parse(inFormat, timeLocal)
if err != nil {
    log.Fatal("Time format was not recognized!")
}

Now, parsing works fine. 现在,解析工作正常。 But when I run: 但是当我跑步时:

fmt.Println(timeLocal)
fmt.Println(inFormat)
fmt.Println(parsed)

The output is: 输出为:

01/July/2015:18:12:25 +0900
02/January/2006:15:04:05 -0700
2015-07-01 18:12:25 +0900 +0900

Should the second +0900 be there? 第二个+0900应该存在吗? What is the stupid thing that I'm doing? 我在做什么愚蠢的事情? Sorry, it was really a long day, I don't see what I'm missing. 抱歉,这真是漫长的一天,我看不到我所缺少的。

Oh, and the whole file is here: 哦,整个文件在这里:

package main

import (
    "fmt"
    "time"
    "log"
)

func main() {
    timeLocal := "01/July/2015:18:12:25 +0900"
    inFormat := "02/January/2006:15:04:05 -0700"
    parsed, err := time.Parse(inFormat, timeLocal)
    if err != nil {
        log.Fatal("Time format was not recognized!")
    }

    fmt.Println(timeLocal)
    fmt.Println(inFormat)
    fmt.Println(parsed)
}

The default format used by Time.String is this: Time.String使用的默认格式是:

2006-01-02 15:04:05.999999999 -0700 MST

Notice the "MST" part. 注意“ MST”部分。 Since you're not providing the name of the zone, the format just "names" it the same as offset, that is "+0900". 由于您未提供区域名称,因此格式只是将其“名称”与偏移相同,即“ +0900”。 If you change that to "+0000", you'll see that this is indeed a time zone name: 如果将其更改为“ +0000”,则会看到这确实是时区名称:

2015-07-01 18:12:25 +0000 UTC

If you don't want that, just use a separate format for printing: 如果您不想这样做,只需使用另一种格式进行打印:

myFmt := "2006-01-02 15:04:05.999999999 -0700"
fmt.Println(parsed.Format(myFmt))

If you look at documentation of time.Time you will see what the default output format is: 如果你看的文档time.Time你会看到默认的输出格式是什么:

String returns the time formatted using the format string: 字符串返回使用格式字符串格式化的时间:

"2006-01-02 15:04:05.999999999 -0700 MST" “ 2006-01-02 15:04:05.999999999 -0700 MST”

Now you should see what the second +0900 is doing there – this is a location (time zone) name. 现在,您应该看到第二个+0900在做什么-这是一个位置(时区)名称。 Since your input format doesn't have a name it will simply repeat an offset. 由于您的输入格式没有名称,因此只需重复一个偏移量即可。

You can provide name by altering your input format to parse location name. 您可以通过更改输入格式以解析位置名称来提供名称。 Alternatively you can provide an output format which doesn't print the name, if you don't need it. 另外,如果不需要,您可以提供不打印名称的输出格式。

Your modified example: 您修改的示例:

package main

import (
    "fmt"
    "time"
    "log"
)

func main() {
    timeLocal := "01/July/2015:18:12:25 +0900 XYZ"
    inFormat := "02/January/2006:15:04:05 -0700 MST"
    parsed, err := time.Parse(inFormat, timeLocal)
    if err != nil {
        log.Fatal("Time format was not recognized!")
    }

    fmt.Println(timeLocal)
    fmt.Println(inFormat)
    fmt.Println(parsed) // 2015-07-01 18:12:25 +0900 XYZ
    fmt.Println(parsed.Format("02/January/2006:15:04:05 -0700"))
}

http://play.golang.org/p/xVGvlt-M5B http://play.golang.org/p/xVGvlt-M5B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM