我有一张带有没有时区的时间戳
列的表格(假设输入的数据在澳大利亚/悉尼
时区)。
在美国/纽约
时区内查询一个时间范围的数据(即上午8点到下午4点)。
有没有简单的方法来实现这个?
谢谢,p。
我有一张带有没有时区的时间戳
列的表格(假设输入的数据在澳大利亚/悉尼
时区)。
在美国/纽约
时区内查询一个时间范围的数据(即上午8点到下午4点)。
有没有简单的方法来实现这个?
谢谢,p。
明白了。
首先需要将时间转换为其带有时区的版本,即my_ts at time zone 'Australia/Sydney'
,然后通过at time zone 'America/New_York'
将其转换为其纽约对应版本。
select
my_ts as "Default(syd)",
my_ts at time zone 'Australia/Sydney' as "SYD",
my_ts at time zone 'Australia/Sydney' at time zone 'America/New_York' as "NY",
date_part('hour', my_ts at time zone 'Australia/Sydney' at time zone 'America/New_York') as "NY-hr"
from my_table
where date_part('hour', my_ts at time zone 'Australia/Sydney' at time zone 'America/New_York')>=8
and date_part('hour', my_ts at time zone 'Australia/Sydney' at time zone 'America/New_York')<16
select now()::time, ((now()::time + '3:00':: interval) at time zone 'America/Vancouver' at time zone 'America/New_York')::time;
我的默认时区是美国/温哥华。这给了我相差6个小时的时间。它使差异加倍而不是平衡它们。 - nate cnow()
已经返回带有时区的时间戳了?我不确定,但我的查询对我来说百分之百有效,我已经彻底测试过了,而你的查询并不适用于我的问题。 - pstantonselect current_time, current_time at time zone 'gmt';
timetz | timezone
-------------------+-------------------
20:50:51.07742-07 | 03:50:51.07742+00
select now()::time, now()::time + '+8:00'::interval;
now | ?column?
-----------------+-----------------
20:57:49.420742 | 04:57:49.420742
一旦您按照自己的需求获取了时间,只需提取小时并使用简单条件选择适当的时间即可。
select *
from
(select extract(hour from now()::time + '+8:00'::interval) as hour) as t
where hour between 8 and 16;