2013-12-11 42 views
2

我在Postgres中的time with time zone平等方面遇到了一些麻煩。 timestamp with time zone平等工作的我怎麼會想到它,在那裏,如果時間正常化時區後一樣,它應該是真實的:帶時區平等的Postgres時間

postgres=# select '2013-06-27 12:00:00 -0800'::timestamp with time zone = '2013-06-27 14:00:00 -0600'::timestamp with time zone; 
?column? 
---------- 
t 

然而,同樣似乎並不適用於time with time zone

postgres=# select '12:00:00 -0800'::time with time zone = '14:00:00 -0600'::time with time zone; 
?column? 
---------- 
f 

然而不平等工作,​​我怎麼會想到他們:

postgres=# select '12:00:00 -0800'::time with time zone < '14:01:00 -0600'::time with time zone; 
?column? 
---------- 
t 

postgres=# select '12:00:00 -0800'::time with time zone > '13:59:00 -0600'::time with time zone; 
?column? 
---------- 
t 

有什麼事我誤解約time with time zone?我怎樣才能以相同的方式處理時區的方式來評估平等呢?timestamp with time zone平等性如何?

回答

3

這裏有兩種方法來評估timetz平等:通過將其添加到date

SELECT a, b, a = b AS plain_equality 
     ,'2000-1-1'::date + a = '2000-1-1'::date + b AS ts_equality 
     ,a AT TIME ZONE 'UTC', b AT TIME ZONE 'UTC' AS timetz_equality 
FROM (
    SELECT '12:00:00 -0800'::timetz AS a, '14:00:00 -0600'::timetz AS b 
    ) sub; 

第一第二個通過使用AT TIME ZONE construct

但只是不要使用time with time zone。永遠。
Postgres僅支持該類型,因爲它在SQL標準中。但它被設計打破了(不能考慮DST!),並且它的使用是不鼓勵的。

引述手冊here

類型time with time zone由SQL標準定義的,但 定義會導致有問題的用法。 在大多數情況下,date,time,timestamp without time zonetimestamp with time zone的組合應提供任何應用程序所需的完整範圍的日期/時間功能 。