diff --git a/v5/core/datetime.go b/v5/core/datetime.go index 4b07124..11bd679 100644 --- a/v5/core/datetime.go +++ b/v5/core/datetime.go @@ -71,3 +71,17 @@ func init() { func NormalizeDateTimeUTC(t time.Time) time.Time { return t.UTC() } + +// ParseDate parses the specified RFC3339 full-date string (YYYY-MM-DD) and returns a strfmt.Date instance. +func ParseDate(dateString string) (fmtDate strfmt.Date, err error) { + formattedTime, err := time.Parse(strfmt.RFC3339FullDate, dateString) + if err == nil { + fmtDate = strfmt.Date(formattedTime) + } + return +} + +// ParseDateTime parses the specified date-time string and returns a strfmt.DateTime instance. +func ParseDateTime(dateString string) (strfmt.DateTime, error) { + return strfmt.ParseDateTime(dateString) +} diff --git a/v5/core/datetime_test.go b/v5/core/datetime_test.go index 7a3f6f8..dbdde0e 100644 --- a/v5/core/datetime_test.go +++ b/v5/core/datetime_test.go @@ -21,6 +21,7 @@ package core import ( "encoding/json" "testing" + "time" "github.com/go-openapi/strfmt" "github.com/stretchr/testify/assert" @@ -159,5 +160,25 @@ func TestModelsDate(t *testing.T) { roundTripTestDate(t, `{"ws_victory":"2007-10-29"}`, `{"ws_victory":"2007-10-29"}`) roundTripTestDate(t, `{"ws_victory":"2013-10-31"}`, `{"ws_victory":"2013-10-31"}`) roundTripTestDate(t, `{"ws_victory":"2018-10-29"}`, `{"ws_victory":"2018-10-29"}`) +} + +func TestDateTimeUtil(t *testing.T) { + dateVar := strfmt.Date(time.Now()) + fmtDate, err := ParseDate(dateVar.String()) + assert.Nil(t, err) + assert.Equal(t, dateVar.String(), fmtDate.String()) + + fmtDate, err = ParseDate("not a date") + assert.Equal(t, strfmt.Date{}, fmtDate) + assert.NotNil(t, err) + + dateTimeVar := strfmt.DateTime(time.Now()) + var fmtDTime strfmt.DateTime + fmtDTime, err = ParseDateTime(dateTimeVar.String()) + assert.Nil(t, err) + assert.Equal(t, dateTimeVar.String(), fmtDTime.String()) + fmtDTime, err = ParseDateTime("not a datetime") + assert.Equal(t, strfmt.DateTime{}, fmtDTime) + assert.NotNil(t, err) } diff --git a/v5/core/utils.go b/v5/core/utils.go index 0baf3bb..a86512c 100644 --- a/v5/core/utils.go +++ b/v5/core/utils.go @@ -26,6 +26,7 @@ import ( "strings" "time" + "github.com/go-openapi/strfmt" validator "gopkg.in/go-playground/validator.v9" ) @@ -109,6 +110,11 @@ func Float64Ptr(literal float64) *float64 { return &literal } +// UUIDPtr returns a pointer to strfmt.UUID literal. +func UUIDPtr(literal strfmt.UUID) *strfmt.UUID { + return &literal +} + // IsJSONMimeType Returns true iff the specified mimeType value represents a // "JSON" mimetype. func IsJSONMimeType(mimeType string) bool { diff --git a/v5/core/utils_test.go b/v5/core/utils_test.go index d5c46aa..f282ec9 100644 --- a/v5/core/utils_test.go +++ b/v5/core/utils_test.go @@ -225,6 +225,9 @@ func TestPointers(t *testing.T) { var float64Var = float64(23) assert.Equal(t, &float64Var, Float64Ptr(float64Var)) + + var uuidVar = strfmt.UUID("12345678-1234-1234-1234-123456123456") + assert.Equal(t, &uuidVar, UUIDPtr(uuidVar)) } func TestConvertSliceFloat64(t *testing.T) {