如何在考虑时区差异的情况下比较两个时间戳?
时间:2024-03-14
浏览:402
作者:佚名
学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《如何在考虑时区差异的情况下比较两个时间戳?》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!
问题内容
我正在尝试测试插入到 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
我无法更改数据库以删除时区,那么如何更改创建日期的方式或如何删除时区?或者也许还有另一种方法可以断言年、月、日是相同的?
解决方案
使用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.理论要掌握,实操不能落!以上关于《如何在考虑时区差异的情况下比较两个时间戳?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
