我正在使用以下带有 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/