2017-07-28 33 views
2

Postgresql在處理時區時使用傳統的tzdata數據庫相當出色。postgresql - 如何提取過去和未知的時區偏移更改列表

服務器可以將過去與不同的時區之間未來的時間戳,在之後的tzdata規則(偏移量,DST更改,..)

有一個簡單而有效的方式,對於給定的時區和給定日期範圍,在發生時區修改事件時提取該範圍內的所有時間戳?

結果應該或多或少包含與linux命令的輸出相同的結果。

zdump -v /usr/share/zoneinfo/America/Los_Angeles | grep 2017 

Sun Mar 12 09:59:59 2017 UTC = Sun Mar 12 01:59:59 2017 PST isdst=0 gmtoff=-28800 
Sun Mar 12 10:00:00 2017 UTC = Sun Mar 12 03:00:00 2017 PDT isdst=1 gmtoff=-25200 
Sun Nov 5 08:59:59 2017 UTC = Sun Nov 5 01:59:59 2017 PDT isdst=1 gmtoff=-25200 
Sun Nov 5 09:00:00 2017 UTC = Sun Nov 5 01:00:00 2017 PST isdst=0 gmtoff=-28800 
+0

它必須在postrgres?在應用程序代碼中執行此操作可能會更容易。 –

+0

嗯,我寧願將所有東西都放在一個地方,並且能夠輕鬆地進行JOIN。 –

回答

1
select d::date 
from (
    select 
     d at time zone 'America/Los_Angeles' as la, 
     lead(d at time zone 'America/Los_Angeles') over (order by d) as la_, 
     d 
    from generate_series (
     '2017-01-01'::timestamp, 
     '2017-12-31', '1 day' 
    ) gs (d) 
) s 
where la::time <> la_::time; 
    d  
------------ 
2017-03-12 
2017-11-05 
+0

有趣。這給你的日期(它是否適用於正面和負面的偏移?),但不是修改發生時的確切時間戳。在系列中使用小時/秒時會有效嗎? –