2011-03-15 99 views
3

我有一個表timestamp without time zone列(輸入的數據假定爲Australia/Sydney時區)。postgresql:無時區時間戳的時區敏感查詢

查詢America/New_York時區的時間範圍(即上午8點至下午4點)的數據。

有沒有簡單的方法來實現這一目標?

謝謝,p。

+0

我想你的意思時沒有時間戳。時區作爲一種數據類型只存在於時間上。 (你可以有一個間隔列,你可以添加到時間比較它們在一起雖然) – 2011-03-15 05:06:40

+0

固定謝謝... – pstanton 2011-03-15 05:19:52

回答

3

想通了。

你需要先轉換的時間是with time zone版本即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 
+0

如果我現在添加3小時(),這將是我在紐約的時間,然後做你是如何做到的:'select now():: time,((now():: time +'3:00' ::時區'America/Vancouver'時區'America/New_York'):: time;'我的默認區域是America/Vancouver。這給了我相隔6個小時的時間。它的差距翻倍了,並沒有使它們相等。 – 2011-03-15 05:25:28

+0

也許'現在()'已經返回一個TS已經?我不知道,但我的查詢爲我工作100%,我已經徹底測試過,而你的問題不適用於我的問題。 – pstanton 2011-03-15 05:40:05

0

您可以將所有的都在相同的時區,所以你可以將其與比較(如果該時區設置):

select 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; 
+0

但我如何查詢時間範圍,即08:00 - 16:00時間範圍是在不同的時區? – pstanton 2011-03-15 04:50:16

+0

謝謝,但你沒有回答我的問題......也許我沒有正確解釋 - 我正在處理悉尼tz/locale系統上的真實日期。 – pstanton 2011-03-15 05:12:25

+0

也許我是這麼解釋的,但答案在第一行。如果您將專欄更改爲帶有時區的專欄並將其存儲(因爲您已經知道某種方式來自紐約),請將相關內容更改爲適當的時區。一旦你這樣做,你的問題就會消失,因爲PG會做正確的事情,所有的轉換消失。 current_time和now(),並且與日期一樣真實,它們只是用作示例。 – 2011-03-15 05:40:20

相關問題