-1

I'm trying to test that the time.Time value I inserted into a postgres database is the same one I am querying out. Postgres drops the timezone though, so I'm wondering how can I get this testify/assert test to pass?

s.Equal(want.Date, got.Date)

Both are the dame datatype time.Time but the first has the timezone:

2020-10-31 00:00:00 +0000 UTC

I created this value like this - time.Date() must take a location, so I couldn't create it without one by passing nil:

want.Date := time.Date(2020, 10, 31, 00, 00, 00, 0000, time.UTC)

got.Date coming from the database looks like:

2020-10-31 00:00:00 +0000 +0000

I can't change the database dropping the timezone, so how can I change how I create the date or how can I drop the timezone? Or perhaps there is another way I can assert that the year, month, and day, are the same?

user9503053
  • 107
  • 3
  • 15

1 Answers1

3

Use time.Time.Equal to test if two values of time.Time type are equal or not.

$ go doc time.Time.Equal
package time // import "time"

func (t Time) Equal(u Time) bool
    Equal reports whether t and u represent the same time instant. Two times can
    be equal even if they are in different locations. For example, 6:00 +0200
    and 4:00 UTC are Equal. See the documentation on the Time type for the
    pitfalls of using == with Time values; most code should use Equal instead.

Shang Jian Ding
  • 1,931
  • 11
  • 24
  • Ah, I was trying the approach of removing the timezone from one, which I couldn't find how to do and keep it a time.Time value still. Thanks! – user9503053 Mar 18 '21 at 14:20