Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: go-openapi/strfmt
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.19.9
Choose a base ref
...
head repository: go-openapi/strfmt
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.19.10
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Nov 15, 2020

  1. Verified

    This commit was signed with the committer’s verified signature.
    casualjim Ivan Porto Carrero
    Copy the full SHA
    b08369e View commit details
Showing with 40 additions and 19 deletions.
  1. +7 −4 time.go
  2. +33 −15 time_test.go
11 changes: 7 additions & 4 deletions time.go
Original file line number Diff line number Diff line change
@@ -77,6 +77,9 @@ var (
DateTimeFormats = []string{RFC3339Micro, RFC3339MicroNoColon, RFC3339Millis, RFC3339MillisNoColon, time.RFC3339, time.RFC3339Nano, ISO8601LocalTime, ISO8601TimeWithReducedPrecision, ISO8601TimeWithReducedPrecisionLocaltime}
// MarshalFormat sets the time resolution format used for marshaling time (set to milliseconds)
MarshalFormat = RFC3339Millis

// NormalizeTimeForMarshal
NormalizeTimeForMarshal = func(t time.Time) time.Time { return t }
)

// ParseDateTime parses a string that represents an ISO8601 time or a unix epoch
@@ -111,7 +114,7 @@ func NewDateTime() DateTime {

// String converts this time to a string
func (t DateTime) String() string {
return time.Time(t).Format(MarshalFormat)
return NormalizeTimeForMarshal(time.Time(t)).Format(MarshalFormat)
}

// MarshalText implements the text marshaller interface
@@ -155,7 +158,7 @@ func (t DateTime) Value() (driver.Value, error) {

// MarshalJSON returns the DateTime as JSON
func (t DateTime) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(t).Format(MarshalFormat))
return json.Marshal(NormalizeTimeForMarshal(time.Time(t)).Format(MarshalFormat))
}

// UnmarshalJSON sets the DateTime from JSON
@@ -204,7 +207,7 @@ func (t *DateTime) UnmarshalBSON(data []byte) error {
func (t DateTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
// UnixNano cannot be used, the result of calling UnixNano on the zero
// Time is undefined.
i64 := time.Time(t).Unix() * 1000
i64 := NormalizeTimeForMarshal(time.Time(t)).Unix() * 1000
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(i64))

@@ -250,7 +253,7 @@ func (t *DateTime) GobDecode(data []byte) error {

// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (t DateTime) MarshalBinary() ([]byte, error) {
return time.Time(t).MarshalBinary()
return NormalizeTimeForMarshal(time.Time(t)).MarshalBinary()
}

// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
48 changes: 33 additions & 15 deletions time_test.go
Original file line number Diff line number Diff line change
@@ -28,22 +28,23 @@ var (
p, _ = time.Parse(time.RFC3339Nano, "2011-08-18T19:03:37.000000000+01:00")

testCases = []struct {
in []byte // externally sourced data -- to be unmarshalled
time time.Time // its representation in time.Time
str string // its marshalled representation
in []byte // externally sourced data -- to be unmarshalled
time time.Time // its representation in time.Time
str string // its marshalled representation
utcStr string // the marshaled representation as utc
}{
{[]byte("2014-12-15T08:00:00"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "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"},
{[]byte("2014-12-15T08:00Z"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z"},
{[]byte("2018-01-28T23:54Z"), time.Date(2018, 01, 28, 23, 54, 0, 0, time.UTC), "2018-01-28T23:54:00.000Z"},
{[]byte("2014-12-15T08:00:00.000Z"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z"},
{[]byte("2011-08-18T19:03:37.000000000+01:00"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00"},
{[]byte("2011-08-18T19:03:37.000000+0100"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00"},
{[]byte("2011-08-18T19:03:37.000+0100"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00"},
{[]byte("2014-12-15T19:30:20Z"), time.Date(2014, 12, 15, 19, 30, 20, 0, time.UTC), "2014-12-15T19:30:20.000Z"},
{[]byte("0001-01-01T00:00:00Z"), time.Time{}.UTC(), "0001-01-01T00:00:00.000Z"},
{[]byte(""), time.Unix(0, 0).UTC(), "1970-01-01T00:00:00.000Z"},
{[]byte(nil), time.Unix(0, 0).UTC(), "1970-01-01T00: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"},
{[]byte("2014-12-15T08:00Z"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z", "2014-12-15T08:00:00.000Z"},
{[]byte("2018-01-28T23:54Z"), time.Date(2018, 01, 28, 23, 54, 0, 0, time.UTC), "2018-01-28T23:54:00.000Z", "2018-01-28T23:54:00.000Z"},
{[]byte("2014-12-15T08:00:00.000Z"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z", "2014-12-15T08:00:00.000Z"},
{[]byte("2011-08-18T19:03:37.000000000+01:00"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00", "2011-08-18T18:03:37.000Z"},
{[]byte("2011-08-18T19:03:37.000000+0100"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00", "2011-08-18T18:03:37.000Z"},
{[]byte("2011-08-18T19:03:37.000+0100"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00", "2011-08-18T18:03:37.000Z"},
{[]byte("2014-12-15T19:30:20Z"), time.Date(2014, 12, 15, 19, 30, 20, 0, time.UTC), "2014-12-15T19:30:20.000Z", "2014-12-15T19:30:20.000Z"},
{[]byte("0001-01-01T00:00:00Z"), time.Time{}.UTC(), "0001-01-01T00:00:00.000Z", "0001-01-01T00:00:00.000Z"},
{[]byte(""), time.Unix(0, 0).UTC(), "1970-01-01T00:00:00.000Z", "1970-01-01T00:00:00.000Z"},
{[]byte(nil), time.Unix(0, 0).UTC(), "1970-01-01T00:00:00.000Z", "1970-01-01T00:00:00.000Z"},
}
)

@@ -184,6 +185,23 @@ func TestDateTime_MarshalJSON(t *testing.T) {
assert.EqualValues(t, esc([]byte(example.str)), bb)
}
}
func TestDateTime_MarshalJSON_Override(t *testing.T) {
oldNormalizeMarshal := NormalizeTimeForMarshal
defer func() {
NormalizeTimeForMarshal = oldNormalizeMarshal
}()

NormalizeTimeForMarshal = func(t time.Time) time.Time {
return t.UTC()
}
for caseNum, example := range testCases {
t.Logf("Case #%d", caseNum)
dt := DateTime(example.time.UTC())
bb, err := dt.MarshalJSON()
assert.NoError(t, err)
assert.EqualValues(t, esc([]byte(example.utcStr)), bb)
}
}

func TestDateTime_Scan(t *testing.T) {
for caseNum, example := range testCases {