Go-swagger: Cannot parse time formats that look like this: 2017-01-17T22:58:45

Created on 18 Jan 2017  路  2Comments  路  Source: go-swagger/go-swagger

Problem statement

When deserializing JSON from a server that has date formats that look like this: 2017-01-17T22:58:45, I get a parsing error:

parsing time \"\"2017-01-17T22:58:45\"\" as \"\"2006-01-02T15:04:05Z07:00\"\": cannot parse \"\"\" as \"Z07:00\"

Steps to reproduce

Try to deserialize 2017-01-17T22:58:45 into a strfmt.DateTime field

Suggestions? I tried manually changing the generated code to a time.Time, but still get the error.

Most helpful comment

If anyone else gets here from Google, here are the changes I made based on casualjim's comment:

Edit $GOPATH/github.com/go-openapi/strfmt/time.go lines 54-68, add a "NoTimeZone" constant, and add NoTimeZone to the "dateTimeFormats" slice.

const (
        // RFC3339Millis represents a ISO8601 format to millis instead of to nanos
        RFC3339Millis = "2006-01-02T15:04:05.000Z07:00"
        // RFC3339Micro represents a ISO8601 format to micro instead of to nano
        RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00"
        // NoTimeZone time format that excludes the time zone
        NoTimeZone = "2006-01-02T15:04:05"
        // DateTimePattern pattern to match for the date-time format from http://tools.ietf.org/html/rfc3339#section-5.6
        DateTimePattern = `^([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]+)?(z|([+-][0-9]{2}:[0-9]{2}))$`
)

var (
        dateTimeFormats = []string{RFC3339Micro, RFC3339Millis, NoTimeZone, time.RFC3339, time.RFC3339Nano}
        rxDateTime      = regexp.MustCompile(DateTimePattern)
        // MarshalFormat sets the time resolution format used for marshaling time (set to milliseconds)
        MarshalFormat = RFC3339Millis
)

All 2 comments

This time stamp is missing zone info, it should end with Z for UTC IIRC
It can be fixed by adding more patterns here: https://github.com/go-openapi/strfmt/blob/master/time.go#L60

If anyone else gets here from Google, here are the changes I made based on casualjim's comment:

Edit $GOPATH/github.com/go-openapi/strfmt/time.go lines 54-68, add a "NoTimeZone" constant, and add NoTimeZone to the "dateTimeFormats" slice.

const (
        // RFC3339Millis represents a ISO8601 format to millis instead of to nanos
        RFC3339Millis = "2006-01-02T15:04:05.000Z07:00"
        // RFC3339Micro represents a ISO8601 format to micro instead of to nano
        RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00"
        // NoTimeZone time format that excludes the time zone
        NoTimeZone = "2006-01-02T15:04:05"
        // DateTimePattern pattern to match for the date-time format from http://tools.ietf.org/html/rfc3339#section-5.6
        DateTimePattern = `^([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]+)?(z|([+-][0-9]{2}:[0-9]{2}))$`
)

var (
        dateTimeFormats = []string{RFC3339Micro, RFC3339Millis, NoTimeZone, time.RFC3339, time.RFC3339Nano}
        rxDateTime      = regexp.MustCompile(DateTimePattern)
        // MarshalFormat sets the time resolution format used for marshaling time (set to milliseconds)
        MarshalFormat = RFC3339Millis
)
Was this page helpful?
0 / 5 - 0 ratings