-3

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.

Mo.Y
  • 15
  • 2
Sinopex
  • 21
  • 1
  • 4
  • Possible duplicate of [Parsing date string in golang](http://stackoverflow.com/questions/25845172/parsing-date-string-in-golang) – RayfenWindspear Apr 03 '17 at 17:34

1 Answers1

7

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

zerkms
  • 249,484
  • 69
  • 436
  • 539