我试图使用Postgres函数extract()
从timestamptz
列中获取年份,但得到意外的结果。我预计它会使用UTC,但似乎使用任何系统的本地时区(在我的情况下,EST)。无论时间戳或系统的时区是什么,我如何让extract
函数返回UTC?如何从时间戳中提取一致的年份
实施例1:
testdb=# create table foo (t timestamptz check(extract(year from t)::int = 2013));
testdb=# \d foo
Table "public.foo"
Column | Type | Modifiers
--------+--------------------------+-----------
t | timestamp with time zone |
Check constraints:
"foo_t_check" CHECK (date_part('year'::text, t)::integer = 2013)
testdb=# insert into foo values ('2013-01-01 01:49:05.048+00');
ERROR: new row for relation "foo" violates check constraint "foo_t_check"
DETAIL: Failing row contains (2012-12-31 20:49:05.048-05).
实施例2:
testdb=# SELECT EXTRACT(year FROM '01-01-1970 00:00:00 UTC+01'::timestamp with time zone);
date_part
-----------
1969
(1 row)
是的,这是工作 - 谢谢。 – Petriborg 2013-03-26 14:05:52