dongxiejie9387 2017-04-03 04:24 采纳率: 100%
浏览 417

Go time.Parse()出现“月份超出范围”错误

I'm new to Go and I was parsing a nginx time format string. You can check my code here:

package main

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

func main(){
    //nginx time format
    nginx_time :="03/Apr/2017:08:29:05 +0800"
    t,err:=time.Parse("02/Jan/2016:15:04:05 MST",nginx_time)

    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(t.Format("2006-01-02 15:04:05"))
}

I received the following error:

GOROOT=/usr/local/go
GOPATH=/usr/local/share/go:/Users/sinopex/goroot:/Users/sinopex/goroot
/usr/local/go/bin/go build -o "/private/var/folders/5b/yf1f_9lj06bfvqvcq2h9myph0000gn/T/Build 1.go and rungo" /Users/sinopex/goroot/src/github.com/sinopex/golang/example/test/1.go
"/private/var/folders/5b/yf1f_9lj06bfvqvcq2h9myph0000gn/T/Build 1.go and rungo"
2017/04/03 12:17:12 parsing time "03/Apr/2017:08:29:05 +0800": month out of range
http://stackoverflow.com/questions/ask#
Process finished with exit code 1

Any ideas on what I am doing incorrectly?

Thank you.

</div>
  • 写回答

1条回答 默认 最新

  • dongxia2030 2017-04-03 04:33
    关注

    Your parsing format does not match the reference format and the input format in 2 places:

    1. The year in the reference format should be 2006

    2. The timezone offset is to be specified by numbers, not by timezone name: -0700

    So:

    t, err := time.Parse("02/Jan/2006:15:04:05 -0700", nginx_time)
    

    play.golang: https://play.golang.org/p/enJY0VBt3a

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line