gpt4 book ai didi

ruby - 从 Postgres 时间戳中添加或减去时间

转载 作者:太空宇宙 更新时间:2023-11-03 17:24:01 27 4
gpt4 key购买 nike

我正在使用以下带有 PG gem 的 ruby​​ 代码来返回采用 Postgres“无时区时间戳”格式的时间戳。我希望它返回值(-1 小时),而不是返回当前值。

def get_latest_timestamp(id)
conn1 = PGconn.open(:dbname => 'testdb')
res = conn1.exec("SELECT MAX(time_of_search) FROM listings WHERE id=#{id}")
res.values[0][0]
end

最佳答案

在 Postgres 中,您可以在时间戳中添加或减去时间间隔以生成另一个时间戳,如所述 here .

所以在这种情况下,假设 time_of_search 是一个时间戳,您可以这样做

"SELECT MAX(time_of_search) - INTERVAL '1 hour' FROM listings WHERE id=#{id}"

关于ruby - 从 Postgres 时间戳中添加或减去时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19944399/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com