PostgreSQL更新时区偏移量

时间:2011-06-27 12:11:35

标签: sql postgresql timezone timestamp

如何快速简便地纠正在错误的时区内意外输入的时区TIMESTAMP WITH TIME ZONE字段?

在我的情况下,以下记录错误地以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

幸运的是,没有任何错误的记录跨越夏令时的边界,所以UTC的2点可以简单地纠正为太平洋2点。

2 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:6)

有一些演员表,更重要的是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)