2011-06-27 160 views
8

什麼是快速和簡單的方法來糾正時區TIMESTAMP WITH TIME ZONE意外進入錯誤的時區?PostgreSQL更新時區偏移

在我的情況下,以下記錄被誤輸入UTC,而不是在美國/太平洋時間:

  t0   |   t1   |  what 
------------------------+------------------------+--------------- 
2011-06-01 13:00:00+00 | 2011-06-01 13:10:00+00 | recalibrating 
2011-06-01 13:10:00+00 | 2011-06-01 13:45:00+00 | verifying 
2011-06-01 13:45:00+00 | 2011-06-01 13:55:00+00 | FAULT 

幸運的是,沒有一個錯誤的記錄跨越夏令界限,讓2點UTC可以簡單地被糾正爲太平洋上午2點。

回答

9
UPDATE <table> SET <timestamptz_field> = (<timestamptz_field> AT TIME ZONE 'UTC') AT TIME ZONE '<correct_time_zone>'; 
5

有管型,更重要的是,運營商at time zone,這是有益的這種事情,例如:

test=# select now(), 
       now()::timestamp; 

       now    |   now    
-------------------------------+---------------------------- 
2011-06-27 14:32:04.169292+02 | 2011-06-27 14:32:04.169292 
(1 row) 

test=# select now() at time zone 'utc', 
      (now() at time zone 'utc')::timestamp with time zone; 

      timezone   |   timezone    
----------------------------+------------------------------- 
2011-06-27 12:32:28.430479 | 2011-06-27 12:32:28.430479+02 
(1 row)