From aa65add4357ab9d5f73055c715a2e7182504960d Mon Sep 17 00:00:00 2001 From: erezrokah Date: Thu, 2 Mar 2023 13:39:16 +0200 Subject: [PATCH] fix(types-timestamp): Ensure timestamp is UTC --- schema/timestamptz.go | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/schema/timestamptz.go b/schema/timestamptz.go index 1430f416f3..6850d81bdb 100644 --- a/schema/timestamptz.go +++ b/schema/timestamptz.go @@ -81,13 +81,13 @@ func (dst *Timestamptz) Set(src any) error { switch value := src.(type) { case int: - *dst = Timestamptz{Time: time.Unix(int64(value), 0), Status: Present} + *dst = Timestamptz{Time: time.Unix(int64(value), 0).UTC(), Status: Present} case int64: - *dst = Timestamptz{Time: time.Unix(value, 0), Status: Present} + *dst = Timestamptz{Time: time.Unix(value, 0).UTC(), Status: Present} case uint64: - *dst = Timestamptz{Time: time.Unix(int64(value), 0), Status: Present} + *dst = Timestamptz{Time: time.Unix(int64(value), 0).UTC(), Status: Present} case time.Time: - *dst = Timestamptz{Time: value, Status: Present} + *dst = Timestamptz{Time: value.UTC(), Status: Present} case *time.Time: if value == nil { *dst = Timestamptz{Status: Null} @@ -162,12 +162,12 @@ func (dst *Timestamptz) DecodeText(src []byte) error { // there is no good way of detecting format so we just try few of them tim, err = time.Parse(time.RFC3339, sbuf) if err == nil { - *dst = Timestamptz{Time: normalizePotentialUTC(tim), Status: Present} + *dst = Timestamptz{Time: tim.UTC(), Status: Present} return nil } tim, err = time.Parse(defaultStringFormat, sbuf) if err == nil { - *dst = Timestamptz{Time: normalizePotentialUTC(tim), Status: Present} + *dst = Timestamptz{Time: tim.UTC(), Status: Present} return nil } return &ValidationError{Type: TypeTimestamp, Msg: "cannot parse timestamp", Value: sbuf, Err: err} @@ -175,18 +175,3 @@ func (dst *Timestamptz) DecodeText(src []byte) error { return nil } - -// Normalize timestamps in UTC location to behave similarly to how the Golang -// standard library does it: UTC timestamps lack a .loc value. -// -// Reason for this: when comparing two timestamps with reflect.DeepEqual (generally -// speaking not a good idea, but several testing libraries (for example testify) -// does this), their location data needs to be equal for them to be considered -// equal. -func normalizePotentialUTC(timestamp time.Time) time.Time { - if timestamp.Location().String() != time.UTC.String() { - return timestamp - } - - return timestamp.UTC() -}