Fix the time parsing code to handle things seen in the real world
This commit is contained in:
parent
344f2bcb2b
commit
313c79f3cc
|
@ -4,20 +4,36 @@ import "time"
|
|||
|
||||
import "C"
|
||||
|
||||
var times []string = []string{
|
||||
"2006-01-02T15:04:05.000000-07:00",
|
||||
"2006-01-02T15:04:05-07:00",
|
||||
time.RFC3339,
|
||||
"2006-01-02T15:04-7000",
|
||||
"2006-01-02T15:04:05-0700",
|
||||
"2006-01-02T15:04:05.000000",
|
||||
}
|
||||
|
||||
func tryTimes(inp string) *time.Time {
|
||||
var t time.Time
|
||||
var err error
|
||||
|
||||
for _, fmtString := range times {
|
||||
t, err = time.Parse(fmtString, inp)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return &t
|
||||
}
|
||||
|
||||
//export fixTime
|
||||
func fixTime(inp *C.char) *int64 {
|
||||
gostr := C.GoString(inp)
|
||||
|
||||
t, err := time.Parse("2006-01-02T15:04:05.000000-07:00", gostr)
|
||||
if err != nil {
|
||||
t, err = time.Parse("2006-01-02T15:04:05-07:00", gostr)
|
||||
t := tryTimes(gostr)
|
||||
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
res := t.Unix()
|
||||
res := (*t).Unix()
|
||||
|
||||
return &res
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue