Question

I am using the following ruby code, with the PG gem, to return a timestamp that is in Postgres "timestamp without timezone" format. Instead of returning the current value, I would like it to return the value (-1 hour).

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
Was it helpful?

Solution

In Postgres, you can add or subtract an interval to or from a timestamp to produce another timestamp, as described here.

So in this case, assuming time_of_search is a timestamp, you could do

"SELECT MAX(time_of_search) - INTERVAL '1 hour' FROM listings WHERE id=#{id}"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top