Go: time.Parse()问题

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

Go: time.Parse() issue

问题

我有以下代码:

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("时间格式无法识别!")
}

现在,解析工作正常。但是当我运行以下代码时:

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

输出结果为:

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

第二个 +0900 应该在那里吗?我在做什么愚蠢的事情?抱歉,今天真的很长,我看不到我漏掉了什么。

哦,整个文件在这里:

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("时间格式无法识别!")
    }

    fmt.Println(timeLocal)
    fmt.Println(inFormat)
    fmt.Println(parsed)
}
英文:

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? 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)
}

答案1

得分: 2

Time.String使用的默认格式是这样的:

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

注意"MST"部分。由于您没有提供时区的名称,格式将其“命名”为与偏移量相同,即"+0900"。如果您将其更改为"+0000",您将看到这确实是一个时区名称:

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

如果您不想要这个,只需使用单独的格式进行打印:

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

The default format used by Time.String is this:

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

Notice the "MST" part. Since you're not providing the name of the zone, the format just "names" it the same as offset, that is "+0900". If you change that to "+0000", you'll see that this is indeed a time zone name:

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))

答案2

得分: 2

如果你查看time.Time的文档,你会看到默认的输出格式是:

> String函数使用格式字符串对时间进行格式化:
>
>"2006-01-02 15:04:05.999999999 -0700 MST"

现在你应该明白第二个+0900是什么意思了——这是一个位置(时区)名称。由于你的输入格式没有名称,它将简单地重复一个偏移量。

你可以通过修改输入格式来提供名称以解析位置名称。或者,如果你不需要名称,你可以提供一个不打印名称的输出格式。

你修改后的示例代码:

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("时间格式无法识别!")
    }

    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

英文:

If you look at documentation of time.Time you will see what the default output format is:

> String returns the time formatted using the format string:
>
>"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. 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

huangapple
  • 本文由 发表于 2015年7月1日 23:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/31165516.html
匿名

发表评论

匿名网友

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

确定