Repository: go-openapi/strfmt
Pull request #107 (2023-04-03 02:23:36)

Adding support for short form of ISO8601
I'm currently working with an API that was giving me a date/time field that varies between ISO8601 long form (i.e. with a time value) and ISO8601 short (i.e. just the date) and kept getting this error:
```
 parsing time "2023-03-31" as "2006-01-02 15:04:05": cannot parse "" as "15"
```
turned out that the short form of ISO8601 was not implemented as one of the accepted formats for `strfmt.ParseDateTime()`. This format is used in the example given for the native golang function [`time.ParseInLocation()`](https://pkg.go.dev/time#example-ParseInLocation), which in turn is the underlying function used to parse the time constants in this package.

This PR adds this format, and modifies the subsequently related tests such that they all pass with the new modifications.


Patch:
diff --git a/time.go b/time.go
index 9bef4c3..682235d 100644
--- a/time.go
+++ b/time.go
@@ -76,6 +76,8 @@ const (
 	ISO8601TimeWithReducedPrecisionLocaltime = "2006-01-02T15:04"
 	// ISO8601TimeUniversalSortableDateTimePattern represents a ISO8601 universal sortable date time pattern.
 	ISO8601TimeUniversalSortableDateTimePattern = "2006-01-02 15:04:05"
+	// short form of ISO8601TimeUniversalSortableDateTimePattern
+	ISO8601TimeUniversalSortableDateTimePatternShortForm = "2006-01-02"
 	// 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}))$`
 )
@@ -84,7 +86,7 @@ var (
 	rxDateTime = regexp.MustCompile(DateTimePattern)
 
 	// DateTimeFormats is the collection of formats used by ParseDateTime()
-	DateTimeFormats = []string{RFC3339Micro, RFC3339MicroNoColon, RFC3339Millis, RFC3339MillisNoColon, time.RFC3339, time.RFC3339Nano, ISO8601LocalTime, ISO8601TimeWithReducedPrecision, ISO8601TimeWithReducedPrecisionLocaltime, ISO8601TimeUniversalSortableDateTimePattern}
+	DateTimeFormats = []string{RFC3339Micro, RFC3339MicroNoColon, RFC3339Millis, RFC3339MillisNoColon, time.RFC3339, time.RFC3339Nano, ISO8601LocalTime, ISO8601TimeWithReducedPrecision, ISO8601TimeWithReducedPrecisionLocaltime, ISO8601TimeUniversalSortableDateTimePattern, ISO8601TimeUniversalSortableDateTimePatternShortForm}
 
 	// MarshalFormat sets the time resolution format used for marshaling time (set to milliseconds)
 	MarshalFormat = RFC3339Millis


Test patch:
diff --git a/format_test.go b/format_test.go
index 5e06b47..525f19e 100644
--- a/format_test.go
+++ b/format_test.go
@@ -240,7 +240,7 @@ func TestDecodeDateTimeHook(t *testing.T) {
 		},
 		{
 			"invalid non empty datetime",
-			"2019-01-01",
+			"2019-01-01abc",
 		},
 	}
 	registry := NewFormats()
diff --git a/time_test.go b/time_test.go
index 67c3d48..c2babb1 100644
--- a/time_test.go
+++ b/time_test.go
@@ -33,6 +33,7 @@ var (
 		str    string    // its marshalled representation
 		utcStr string    // the marshaled representation as utc
 	}{
+		{[]byte("2014-12-15"), time.Date(2014, 12, 15, 0, 0, 0, 0, time.UTC), "2014-12-15T00:00:00.000Z", "2014-12-15T00:00:00.000Z"},
 		{[]byte("2014-12-15 08:00:00"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z", "2014-12-15T08:00:00.000Z"},
 		{[]byte("2014-12-15T08:00:00"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z", "2014-12-15T08:00:00.000Z"},
 		{[]byte("2014-12-15T08:00"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z", "2014-12-15T08:00:00.000Z"},