Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lPlVp-0001M4-RK for pgsql-general@arkaria.postgresql.org; Fri, 26 Mar 2021 12:24:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lPlVo-0000IB-M3 for pgsql-general@arkaria.postgresql.org; Fri, 26 Mar 2021 12:24:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lPd8r-0007mt-Ur for pgsql-general@lists.postgresql.org; Fri, 26 Mar 2021 03:28:10 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lPd8q-0007wB-38 for pgsql-general@lists.postgresql.org; Fri, 26 Mar 2021 03:28:09 +0000 Received: by mail-pj1-x1034.google.com with SMTP id nh23-20020a17090b3657b02900c0d5e235a8so1831358pjb.0 for ; Thu, 25 Mar 2021 20:28:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:mime-version:subject:message-id:date:to; bh=aTuSB2QKjp2v/6SnGOnhrKIgD+qE8F9w70zpSYKAckA=; b=hjtmWE6n6nDvEO03AaGfVtD/9g6FRnYZNxUzBYChAbz4UZ5FSKVkf3J+rxt567x99C u6aPp/d/NLsU7t6HlBK1JqTTaebRIOEUDIIi6mC864KEQq3Q8XMQyYA982xN6qjx2qEH MMbvBm3Xx314tr9100sdyV2Ng/3cG/Ro5wH70= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:mime-version:subject:message-id:date:to; bh=aTuSB2QKjp2v/6SnGOnhrKIgD+qE8F9w70zpSYKAckA=; b=WYATbByMUhbVqoePsPQ6l/seD3UPa3x36xF4rwC/2RfmLwCE+PbPO/4XbjHe83oB13 h/R+JPKCvzrEi1ZBwkpyQXsNp92FwnWICyq7QMwfoBxCR9GNsyGQOXct3m/xEpsFTGTu 82RI1H/f5aYf/PU8csUKdT6DjgMpOq3yac5N8UCsqJ13dZX2dlITlmU8hUerjNJN1Vau kgk2cx/lTt4jimH5RqbqOJU6eJxsHOR/X6EUXXd8jkTrI79PWXe3wX6uV4jQJ+yvGVSG vf7YJ6cr8hpHFSg68b8tmafprBG4kz+EZGxruh5jIdKjbk6bTUtuuXJ9Kb0sna4xNIiS 9qEQ== X-Gm-Message-State: AOAM5334hZKh1KA9y77U9KhjDiG8uHMlgBlBBrcRx76liFyw7i5XNW7y rX5gux6of2cEWHOIAMy6TGAoUqaDtAqXH3ol X-Google-Smtp-Source: ABdhPJynOW4GaycXK5j4LvEyg5kXoojHJc1vtknaREHi1c9QcPc29PWI2rQ3zKmvxrHvYhqNipLgiA== X-Received: by 2002:a17:90a:fa89:: with SMTP id cu9mr12103052pjb.204.1616729285360; Thu, 25 Mar 2021 20:28:05 -0700 (PDT) Received: from ?IPv6:2601:646:101:7fda:c1f2:4503:a84c:951a? ([2601:646:101:7fda:c1f2:4503:a84c:951a]) by smtp.gmail.com with ESMTPSA id i14sm6849555pjh.17.2021.03.25.20.28.04 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Mar 2021 20:28:05 -0700 (PDT) From: Bryn Llewellyn Content-Type: multipart/alternative; boundary="Apple-Mail=_E3FF3530-9A2E-4DCA-AC28-5FD8AEF85D36" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) Subject: i := t1 - t0 but t0 + i <> t1 when t1 and t2 timestamptz values and i is an interval value Message-Id: <402F8EFB-3FE9-421A-B3BA-F736AB8E3ABB@yugabyte.com> Date: Thu, 25 Mar 2021 20:28:03 -0700 To: pgsql-general list X-Mailer: Apple Mail (2.3654.60.0.2.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_E3FF3530-9A2E-4DCA-AC28-5FD8AEF85D36 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Given these, where the timestamptz values span the start of US/Pacific = DST: t0 timestamptz :=3D ...; t1 timestamptz :=3D ...; i_by_subtraction interval second :=3D t1 - t0; then this expression tests FALSE: t0 + i_by_subtraction i =3D t1 There's more. We see that while the two "interval second=E2=80=9D values = '1 day 01:00:00' and '25 hours' test as equal, the results of adding = each to the same timestamptz value are different. PLEASE STATE THE RULES THAT ALLOW THE BEHAVIOR OF THE BLOCK TO BE = PREDICTED. (I searched the doc but found nothing.) = --------------------------------------------------------------------------= ---------------- =E2=80=94 Self-contained test case. Finishes silently in PG 13.2 showing = that all assertions hold. do $body$ declare t0_text constant text :=3D '2021-03-13 20:00:00'; t_dst constant text :=3D '2021-03-14 02:00:00'; t1_text constant text :=3D '2021-03-14 22:00:00'; t_surprise constant text :=3D '2021-03-14 21:00:00'; -- i_expected because we spring forward to US DST between t0_text and = t1_text. i_expected constant interval second :=3D '1 day 01:00:00'; i_25_hours constant interval second :=3D '25 hours'; t0 timestamptz; t1 timestamptz; i_by_subtraction interval second; begin assert t0_text < t_dst and t_dst < t1_text, 'assert #1 failed'; assert t_surprise <> t1_text, 'assert #2 failed'; ------------------------------------------------------------ -- Look! assert i_expected =3D i_25_hours, 'assert #3 failed'; ------------------------------------------------------------ set time zone 'US/Pacific'; t0 :=3D t0_text::timestamptz; t1 :=3D t1_text::timestamptz; assert t0::text =3D t0_text||'-08', 'assert #4 failed'; assert t1::text =3D t1_text||'-07', 'assert #5 failed'; --<< compare i_by_subtraction :=3D t1 - t0; assert i_by_subtraction =3D i_expected, 'assert #6 failed'; ------------------------------------------------------------ -- HERE IS THE PARADOX. t1 :=3D t0 + i_expected; assert t1::text =3D t_surprise||'-07', 'assert #7 failed'; -- Meanwhile... t1 :=3D t0 + i_25_hours; assert t1::text =3D t1_text||'-07', 'assert #8 failed';--<< compare = (same) ------------------------------------------------------------ end; $body$; --Apple-Mail=_E3FF3530-9A2E-4DCA-AC28-5FD8AEF85D36 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
Given these, where = the timestamptz values span the start of US/Pacific DST:
  t0 = timestamptz :=3D ...;
  t1 timestamptz :=3D ...;
  i_by_subtraction interval second :=3D t1 - t0;

then this expression tests = FALSE:

 t0 + i_by_subtraction i =3D t1

There's more. We see that = while the two "interval second=E2=80=9D values '1 day 01:00:00' and = '25 hours' test as equal, the results of adding each to the same = timestamptz value are different.

PLEASE STATE THE RULES THAT ALLOW THE BEHAVIOR OF THE BLOCK = TO BE PREDICTED.

(I searched the doc = but found nothing.)

---------------------------------------------------------------= ---------------------------
=E2=80=94 Self-contained test = case. Finishes silently in PG 13.2 showing that all assertions hold.

do $body$
declare
  t0_text    constant text :=3D '2021-03-13 = 20:00:00';
  t_dst      constant text = :=3D '2021-03-14 02:00:00';
  t1_text   =  constant text :=3D '2021-03-14 22:00:00';
  = t_surprise constant text :=3D '2021-03-14 21:00:00';

  -- i_expected because we spring forward to US DST = between t0_text and t1_text.
  i_expected constant = interval second :=3D '1 day 01:00:00';
  i_25_hours = constant interval second :=3D '25 hours';

  t0 timestamptz;
  t1 = timestamptz;
  i_by_subtraction interval second;
begin
  assert t0_text < t_dst and = t_dst < t1_text, 'assert #1 failed';
  assert = t_surprise <> t1_text, 'assert #2 failed';

  = ------------------------------------------------------------
  -- Look!
  assert i_expected =3D = i_25_hours, 'assert #3 failed';
  = ------------------------------------------------------------

  set time zone 'US/Pacific';
  t0 :=3D t0_text::timestamptz;
  t1 = :=3D t1_text::timestamptz;

  assert = t0::text =3D t0_text||'-08', 'assert #4 failed';
  = assert t1::text =3D t1_text||'-07', 'assert #5 failed'; --<< = compare

  i_by_subtraction :=3D t1 - = t0;
  assert i_by_subtraction =3D i_expected, 'assert = #6 failed';

  = ------------------------------------------------------------
  -- HERE IS THE PARADOX.
  t1 :=3D = t0 + i_expected;
  assert t1::text =3D = t_surprise||'-07', 'assert #7 failed';

 = -- Meanwhile...
  t1 :=3D t0 + i_25_hours;
  assert t1::text =3D t1_text||'-07', 'assert #8 = failed';--<< compare (same)
  = ------------------------------------------------------------
end;
$body$;

= --Apple-Mail=_E3FF3530-9A2E-4DCA-AC28-5FD8AEF85D36--