dougekui1518 2019-03-23 09:26
浏览 1417
已采纳

如何在postgres中查询timestamptz并与time.Time中的时间进行比较? [重复]

This question already has an answer here:

I'm using Postgres 10 and with a dummy table

CREATE TABLE demo (now timestamptz);

When using jmoiron/sqlx, I can't compare time.Time with timestamptz. The sample code is

db, _ := sqlx.Connect("postgres", dsn)
now, _ := time.Parse(time.RFC3339, "2019-03-22T00:00:00Z")
db.Exec("INSERT INTO demo (now) VALUES ($1)", now)

query := "SELECT timezone('utc', now) as now FROM demo"
type row struct{ Now time.Time }
var rows []row
if err := db.Select(&rows, query); err != nil {
    log.Fatal(err)
}
want := []row{ { Now: now } }
if !reflect.DeepEqual(rows, want) {
    log.Printf("want: %v; got: %v", want, rows)
}

I got results saying they are not equal in the last step

 want: [{0 2019-03-22 00:00:00 +0000 UTC}]
 got: [{0 2019-03-22 00:00:00 +0000 +0000}]

What should I do make reflect.DeepEqual think both are equal?

</div>
  • 写回答

1条回答 默认 最新

  • dongwo8827523 2019-03-23 09:40
    关注

    You should not use DeepEqual for time comparison. You should compare times using the time package. You should do:

    now.Equal(rows[0].Now)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器