在postgres查询

时间:2017-07-07 19:25:21

标签: sql postgresql timestamp

我正试图在postgres查询中自动考虑时区。我的目标是在EST或UTC - 5:00选择0:00到23:59之间的记录。

当前查询仅返回UTC时间0:00到23:59之间的记录。

select path, start_time from routes where routes.network_id = 1 and routes.start_time between '2017-06-13 00:00:00'::timestamp AND '2017-06-13 23:59:59'::timestamp

start_time是没有时区的时间戳,因此默认情况下它是UTC

SELECT pg_typeof("start_time") from routes limit 1;

返回timestamp without timezone

如何编写查询以解决5小时差异并将start_time转换为UTC - 5?

1 个答案:

答案 0 :(得分:2)

试试这个:

select path, start_time - interval '5 hours' as start_time_est
  from routes
 where routes.network_id = 1 
       and routes.start_time between '2017-06-13 00:00:00-5'::timestamp  with time zone 
           AND '2017-06-13 23:59:59-5'::timestamp with time zone;