douyi9447
2015-07-01 15:24
浏览 111
已采纳

前往:time.Parse()问题

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

图片转代码服务由CSDN问答提供 功能建议

我有以下代码:

  timeLocal:=“  2015年7月1日:18:12:25 +0900“ 
inFormat:=” 02年1月2日:15:04:05 -0700“ 
parsed,错误:= time.Parse(inFormat,timeLocal)
if错误 != nil {
 log.Fatal(“无法识别时间格式!”)
} 
   
 
 

现在,解析正常。 但是当我运行时:

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

输出为:

  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 /一月/ 2006:15:04:05 -0700“ 
已解析,错误:= time.Parse(inFormat,timeLocal)
如果错误!= nil {  
 log.Fatal(“无法识别时间格式!”)
} 
 
 fmt.Println(timeLocal)
 fmt.Println(inFormat)
 fmt.Println(已解析)
} 
 <  / code>  
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyue9864 2015-07-01 15:36
    已采纳

    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

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题