Postgres 时间与时区相等 [英] Postgres time with time zone equality

查看:28
本文介绍了Postgres 时间与时区相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理 Postgres 中的 time with time zone 等式时遇到了一些麻烦.timestamp with time zone 等式按照我的预期工作,如果在规范化时区后时间相同,则应该为真:

I'm having some trouble with time with time zone equalities in Postgres. timestamp with time zone equality works how I would expect it to, where if the times are the same after normalizing the timezones, it should be true:

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:

However, the same does not seem to apply to time with time zone:

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

然而,不平等的运作方式是我所期望的:

Yet inequalities work how I would expect them to:

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 相等的方式处理时区的方式评估相等?

Is there something I'm misunderstanding about time with time zone? How can I evaluate for equality in a way that handles time zones the same way timestamp with time zone equality does?

推荐答案

这里有两种方法来评估 timetz 相等性:

Here are two ways to evaluate timetz equality:

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;

首先通过将其添加到 date.
第二个使用 AT TIME ZONE 构造.

The first by adding it to a date.
The second by using the AT TIME ZONE construct.

但完全不要使用time with time zone.
Postgres 支持该类型只是因为它在 SQL 标准中.它被设计破坏(不能考虑 DST!),不鼓励使用.

But rather don't use time with time zone at all.
Postgres supports the type only because it is in the SQL standard. It is broken by design (cannot consider DST!) and its use is discouraged.

此处引用手册:

类型 time with time zone 由 SQL 标准定义,但是定义展示了导致可疑用途的特性.在大多数情况下,datetimetimestamp without time zonetimestamp with time zone 的组合应该提供完整的范围任何应用程序所需的日期/时间功能.

The type time with time zone is defined by the SQL standard, but the definition exhibits properties which lead to questionable usefulness. In most cases, a combination of date, time, timestamp without time zone, and timestamp with time zone should provide a complete range of date/time functionality required by any application.

这篇关于Postgres 时间与时区相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆