如何检查现在(utc)是否在postgres的时间范围内(根据时区)

时间:2017-10-23 11:49:42

标签: java sql postgresql timezone utc

我正在使用postgres,我有这两个表:

             TIME_FRAMES
ID | TIME_ZONE | DAY_OF_WEEK | START | END | LECTURE_ID

      LECTURES
ID | CLASS | TEACHER 

我想运行一个查询来获取现在发生的所有讲座(运行查询的时间)。

我看到有list of time zones Postgres支持,所以这些值是我坚持的。

谢谢,

1 个答案:

答案 0 :(得分:1)

使用日期算术:

SELECT *
FROM time_frames
WHERE current_timestamp BETWEEN
            (CAST (current_date || ' ' || start
                  AS timestamp without time zone
            ) AT TIME ZONE time_zone)
         AND
            (CAST (current_date || ' ' || "end"
                  AS timestamp without time zone
           ) AT TIME ZONE time_zone)
  AND extract(dow FROM current_date) = day_of_week;