我正在尝试测试我插入到 postgres 数据库中的 time.Time
值是否与我要查询的值相同。不过,Postgres 删除了时区,所以我想知道如何才能通过这个作证/断言测试?
s.Equal(want.Date, got.Date)
两者都是 dame 数据类型 time.Time
,但第一个具有时区:
2020-10-31 00:00:00 +0000 UTC
我像这样创建了这个值 - time.Date()
必须取一个位置,所以我不能通过传递 nil 来创建它:
want.Date := time.Date(2020, 10, 31, 00, 00, 00, 0000, time.UTC)
got.Date
看起来像:
2020-10-31 00:00:00 +0000 +0000
我无法更改删除时区的数据库,那么如何更改创建日期或删除时区的方式?或者也许有另一种方法可以断言年、月和日是相同的?
答案 0 :(得分:3)
使用 time.Time.Equal
测试 time.Time
类型的两个值是否相等。
$ 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.