Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fH2QA-00031S-P2 for pgsql-general@arkaria.postgresql.org; Fri, 11 May 2018 07:24:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fH2Q9-0005QI-Br for pgsql-general@arkaria.postgresql.org; Fri, 11 May 2018 07:24:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fH2Q9-0005Q5-37 for pgsql-general@lists.postgresql.org; Fri, 11 May 2018 07:24:53 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fH2Q5-0001az-V8 for pgsql-general@lists.postgresql.org; Fri, 11 May 2018 07:24:52 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 468ED20FEB; Fri, 11 May 2018 03:24:48 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 11 May 2018 03:24:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=relops.com; h= content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= mesmtp; bh=Ze/nZ156tALVTLRAuxxdxh4n1qs09+KKgjwWYBpYl1I=; b=Ropd4 heCKDIy7+DZTyeEepPy8I33FWuYYnD3xltTf+DW6ZUsntxBBrbDF4US/+G6dpjxY Rz+FY6klPBOGqB8zVOLowlUMFlK52ZoJ8FKk2FpVg5JQ1GWgGs3x4lED0JGxk14s HYDyMEM4gZWmPT56q3Nf2ZvifpGjbZuxMvGXn4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=Ze/nZ156tALVTLRAuxxdxh4n1qs09 +KKgjwWYBpYl1I=; b=k4FhSzjb0wJQLOg2YmydneIsxAY6LqddD3ZFyy5kV6I7V PtPjmpkqp2xlKuqAyaO+bxX+QbRZniGteT2QV+Q/bCT1WDUanqDOUrdWdR1w3sC2 OdBj6iH+z8k7YaWYB0Ht0uo1CCVLHwMGii4CeZO/gBR9nrkMFihfZypHMNoiLkfj Zepdpju8QMSJDaSDrMrcQjZMENTaQhPATqAZh+b/Vo29Mq0d5xw+EK5toAYEk30M U0L16kGaND28SOwZw6yb9D9kiHHdANFigif61Px9nfBXIFhyTX5OOYlUrrYx3Ppb LnFZJ/h3Ssz924AjX6+26M+Rptu4OwDXoh5DnJDCg== X-ME-Sender: Received: from [172.20.10.3] (unknown [148.252.129.148]) by mail.messagingengine.com (Postfix) with ESMTPA id 46327E46C2 for ; Fri, 11 May 2018 03:24:47 -0400 (EDT) From: Ben Hood Content-Type: multipart/alternative; boundary="Apple-Mail=_2C2D431B-492E-4441-8B29-E8C4D52ABA97" Mime-Version: 1.0 (Mac OS X Mail 11.3 \(3445.6.18\)) Subject: Re: Domain based on TIMEZONE WITH TIME ZONE Date: Fri, 11 May 2018 08:24:41 +0100 References: <06EAD729-DC35-4E48-AB25-C42F7FA19C28@relops.com> <188A0270-0A9A-4ADE-A928-9067E99AE7B7@relops.com> <71aafbb9-f3d1-a20b-5ebc-0162e04a952a@aklaver.com> To: pgsql-general@lists.postgresql.org In-Reply-To: <71aafbb9-f3d1-a20b-5ebc-0162e04a952a@aklaver.com> Message-Id: <1465CD01-C08A-49BF-BCF3-193E6509A703@relops.com> X-Mailer: Apple Mail (2.3445.6.18) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_2C2D431B-492E-4441-8B29-E8C4D52ABA97 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On 10 May 2018, at 23:43, Adrian Klaver = wrote: >=20 > Trying to tame time and time zones is maybe quixotic, but not weird. Quixotic is a very good description, I=E2=80=99d happily admit that = using the UTC domain in this way is not as pragmatic as I thought it = would when I introduced it. > While I was working on my response to Peter I realized that the below: >=20 > CREATE DOMAIN UTC_TIMESTAMP AS TIMESTAMP WITH TIME ZONE CHECK = (EXTRACT(TIMEZONE FROM VALUE) =3D 0); >=20 > might not work the way you expect if you have your server set to = 'UTC=E2=80=99. =E2=80=9CMight not work the way you expect it to=E2=80=9D is the = underlying theme and the overall motivation for trying to master time = zones from disparate sources. >=20 > show timezone; > TimeZone > ---------- > UTC > (1 row) >=20 > CREATE DOMAIN UTC_TIMESTAMP AS TIMESTAMP WITH TIME ZONE CHECK = (EXTRACT(TIMEZONE FROM VALUE) =3D 0); >=20 > create table ts_check(id integer, ts_fld utc_timestamp); >=20 > insert into ts_check values (1, now()); > insert into ts_check values (2, '05/10/18 15:23'); > insert into ts_check values (3, '05/10/18 15:23+07'); >=20 >=20 > test=3D> select * from ts_check ; > id | ts_fld > ----+------------------------------- > 1 | 2018-05-10 22:37:58.745263+00 > 2 | 2018-05-10 15:23:00+00 > 3 | 2018-05-10 08:23:00+00 >=20 > In fact I am not sure how it works:): As Peter and Francisco has previously demonstrated, I believe that = inserting a timestamp into the UTC_TIMESTAMP column: (1) Checks the client time zone is UTC (2) Converts the timestamp to UTC, whether it was presented as UTC or = not. Going to back to the original intention of enforcing UTC, the end result = is correct, but for the wrong reason. In this example, row 3 contains the UTC value of '05/10/18 15:23+07=E2=80= =99, which is the end result I wanted (pure UTC everywhere). But, the way that it is actually happens under the covers is masked by = the use of the UTC_TIMESTAMP domain. In the scenario, there is a belief that only UTC timestamps are being = inserted, which is not true. The way that Postgres stores any timestamp leads to the desired result, = fooling me into believing that the UTC_TIMESTAMP domain is casual for = the correct result. I would have need check whether the behavior is the same using a network = client driver (maybe there is some implicit coercion going on when psql = parses the statement). But I think the point still stands that Postgres timestamp handling is = doing the real work and is casual for the desired outcome, not the = domain. --Apple-Mail=_2C2D431B-492E-4441-8B29-E8C4D52ABA97 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

On 10 May 2018, at 23:43, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

Trying to tame time and time = zones is maybe quixotic, but not weird.

Quixotic is a very good description, I=E2=80=99d = happily admit that using the UTC domain in this way is not as pragmatic = as I thought it would when I introduced it.

While I was working on my response to Peter I realized that = the below:

CREATE DOMAIN UTC_TIMESTAMP AS = TIMESTAMP WITH TIME ZONE CHECK (EXTRACT(TIMEZONE FROM VALUE) =3D 0);

might not work the way you expect if you have = your server set to 'UTC=E2=80=99.

=E2=80=9CMight not work the way you expect it = to=E2=80=9D is the underlying theme and the overall motivation for = trying to master time zones from disparate sources.


show timezone;
TimeZone
----------
UTC
(1 row)

CREATE DOMAIN UTC_TIMESTAMP AS TIMESTAMP WITH = TIME ZONE CHECK (EXTRACT(TIMEZONE FROM VALUE) =3D 0);

create table ts_check(id integer, ts_fld utc_timestamp);

insert into ts_check values (1, now());
insert into ts_check values (2, '05/10/18 15:23');
insert into ts_check values (3, '05/10/18 15:23+07');


test=3D> select * from = ts_check ;
id | =            ts_fld----+-------------------------------
 1 = | 2018-05-10 22:37:58.745263+00
 2 | 2018-05-10 = 15:23:00+00
 3 | 2018-05-10 08:23:00+00

In fact I am not sure how it works:):

As = Peter and Francisco has previously demonstrated, I believe that = inserting a timestamp into the UTC_TIMESTAMP column:

(1) Checks the client time zone is = UTC
(2) Converts the timestamp to UTC, whether it was = presented as UTC or not.

Going to = back to the original intention of enforcing UTC, the end result is = correct, but for the wrong reason.

In = this example, row 3 contains the UTC value of  '05/10/18 = 15:23+07=E2=80=99, which is the end result I wanted (pure UTC = everywhere).

But, the way that it is = actually happens under the covers is masked by the use of the = UTC_TIMESTAMP domain.

In the = scenario, there is a belief that only UTC timestamps are being inserted, = which is not true.

The way that = Postgres stores any timestamp leads to the desired result, fooling me = into believing that the UTC_TIMESTAMP domain is casual for the correct = result.

I would have need check = whether the behavior is the same using a network client driver (maybe = there is some implicit coercion going on when psql parses the = statement).

But I think the point = still stands that Postgres timestamp handling is doing the real work and = is casual for the desired outcome, not the domain.


= --Apple-Mail=_2C2D431B-492E-4441-8B29-E8C4D52ABA97--