araddon / dateparse

GoLang Parse many date strings without knowing format in advance.
MIT License
2.03k stars 164 forks source link

Date Parse Fails on dates returned by Workfront in the European timezone #117

Closed haskovec closed 3 years ago

haskovec commented 3 years ago

When I use the Workfront API to retrieve data from our instance the dates on some of the objects come back in this format:

2020-08-17T17:00:00:000+0100

Date parse can't parse the offsets at a positive offset to GMT as shown in the test code below:

func TestWorkfrontDateFormat(t *testing.T) {
    workfrontDate := "2020-08-17T17:00:00:000+0100"  //Set for European timezone

    _, err := dateparse.ParseAny(workfrontDate)
    if err != nil {
        t.Errorf("Error Parsing date: %s with error: %#v", workfrontDate, err)
    }
}
araddon commented 3 years ago

Thanks for the report. It appears golang time.Parse() doesn't handle this either, or i was un-able to figure out how to create a layout that works anyway. If you can find a layout that works it should be easy. https://play.golang.org/p/dXdeUPTkLD7

Its possible that the actual string passed in could be manipulated to replace the last colon with a period, but in general i try not to re-write the date-string as much as create a layout for parsing that works.

package main

import (
    "fmt"
    "time"
)

func main() {
    t, err := time.Parse("2006-01-02T15:04:05:000-0700", "2020-08-17T17:00:00:897+0100")
    fmt.Printf("t=%v   err=%v\n\n", t, err)
    t, err = time.Parse("2006-01-02T15:04:05.000-0700", "2020-08-17T17:00:00.897+0100")
    fmt.Printf("t2=%v   err=%v\n\n", t, err)
}
t=0001-01-01 00:00:00 +0000 UTC   err=parsing time "2020-08-17T17:00:00:897+0100" as "2006-01-02T15:04:05:000-0700": cannot parse "897+0100" as ":000"

t2=2020-08-17 17:00:00.897 +0100 +0100   err=<nil>
araddon commented 3 years ago

Not happy with the solution i came up with, pretty hacky but works now.