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 1pGU14-00012B-4L for pgsql-general@arkaria.postgresql.org; Sat, 14 Jan 2023 00:03:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pGU13-00066e-0U for pgsql-general@arkaria.postgresql.org; Sat, 14 Jan 2023 00:03:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pGU12-00066L-CX for pgsql-general@lists.postgresql.org; Sat, 14 Jan 2023 00:03:20 +0000 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pGU0y-0002CQ-Pl for pgsql-general@lists.postgresql.org; Sat, 14 Jan 2023 00:03:19 +0000 Received: by mail-pj1-x102b.google.com with SMTP id z9-20020a17090a468900b00226b6e7aeeaso25960653pjf.1 for ; Fri, 13 Jan 2023 16:03:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=CRDiNZlthxJkK6QMjIxhCHPNpm4jPnno4ytkTSMZ0pQ=; b=NYiFDcQa0f33GEx70pHIcuzg01BdgCVBoB+iTa8nmZZDAWPz7KBvxrPMw5v0v2e6fu elSDjxvdmSwJZB79Yh8Yrwn79ByU0z89NLsFu+ry6zM5scMgbtmV9SdIILNkMtevN8sn SThwW8P4l8oDnAwQipG03V4geRW0p+kkhni58= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CRDiNZlthxJkK6QMjIxhCHPNpm4jPnno4ytkTSMZ0pQ=; b=Yn9ltOx6bB5IXVgJDnCd/MweCPaPS/iaRzbPmIcYWv19S++EtWP86CIHV+zVeuWeQ5 ITVqaWN2D1SyU1ftDPOD1pep5VwQfq8EoFFnpTVnDYij5Ms7YMBKivJtQ/KxoS8pfxrJ n7DyPiq6u1aD2waS+Hwf5TDSei0Yx9RYB6UQpn5lpBf2nW+xnF/lBNy2rIWvtdVJOfhZ tC1zm6b9S8Bef1DyE1U68s/ABNE8PI3FpOXf3u03nmFmJUQuN7VfS83KjWwrQqyThdr5 T0d7GjRrBsvPnCcRlKcBgmS/i1obQ3co1zQY1wwqwUru17DWrZtf+6Xt5kYzhpuC0vwH y+nQ== X-Gm-Message-State: AFqh2koEQTTmJufnAh5bKuE0B5EdPhqzv4unx2dbcx2hCWLqGaF8i/s3 Z4Z1hrx7KKUE77yooO8mKWwFJg== X-Google-Smtp-Source: AMrXdXuc3CeRYDM0FW+P5WV+Hn4ZmKVlrdoGDlhoyjWYscU9a7XwjTJtiCeRtgXu7+5t90dTV1sgRg== X-Received: by 2002:a17:902:ccd0:b0:188:d405:63c0 with SMTP id z16-20020a170902ccd000b00188d40563c0mr87457560ple.6.1673654595545; Fri, 13 Jan 2023 16:03:15 -0800 (PST) Received: from smtpclient.apple ([2601:646:101:7fda:5539:e632:70d1:6153]) by smtp.gmail.com with ESMTPSA id b13-20020a170902650d00b00187197c499asm14706501plk.164.2023.01.13.16.03.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Jan 2023 16:03:15 -0800 (PST) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_BFD05007-9B0A-4A0C-BC02-98F1E15EB57C" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: Intervals and ISO 8601 duration Date: Fri, 13 Jan 2023 16:03:14 -0800 In-Reply-To: Cc: Tom Lane PostgreSQL , david.g.johnston@gmail.com, Adrian Klaver , Sebastien Flaesch , pgsql-general list To: Ken Tanzer References: <953416.1673536094@sss.pgh.pa.us> <53c0b4d4-b80a-a35b-ce18-61058794c9bb@aklaver.com> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_BFD05007-9B0A-4A0C-BC02-98F1E15EB57C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > ken.tanzer@gmail.com wrote: >=20 > Here's an example. Note that they come out formatted differently with = to_char, but evaluate as equal. The explanation(1) was that they were = Equal but not Identical. I was thinking getting the raw data about how = they are stored would get at the identicality issue: >=20 > WITH inters AS ( > SELECT > '1 day 2 hours'::interval AS i1, > '26 hours'::interval AS i2 > ) > SELECT > *, > to_char(i1,'HH24:MM:SS') AS i1_char, > to_char(i2,'HH24:MM:SS') AS i2_char, > i1=3Di2 AS "Equal?" > FROM inters; >=20 > i1 | i2 | i1_char | i2_char | Equal? > ----------------+----------+----------+----------+-------- > 1 day 02:00:00 | 26:00:00 | 02:00:00 | 26:00:00 | t I struggled to understand this whole murky area when I was writing the = =E2=80=9CDate and time data types and functionality=E2=80=9D section for = the YugabyteDB doc. (YugabyteDB uses the Postgres SQL processing code = =E2=80=9Cas is=E2=80=9D on top of its own distributed storage layer. All = the examples in my doc work identically in vanilla PG.) The implied question here is this: is the interval =E2=80=9C1 day 2 = hours=E2=80=9D the same as the interval =E2=80=9C26 hours=E2=80=9D? It = might seem that the answer is =E2=80=9Cyes=E2=80=9D=E2=80=94as it surely = must be. But, sorry to say, that the answer is actually =E2=80=9Cno=E2=80=9D= . Confused? You will be. Most people are until they=E2=80=99ve wrapped = their head in a towel and puzzled it through for a few days. This shows = you what I mean: set timezone =3D 'America/Los_Angeles'; with c as ( select '2023-03-11 20:00 America/Los_Angeles'::timestamptz as = original_appointment) select original_appointment::text as "original appointment", (original_appointment + '1 day 2 hours'::interval)::text as "postponed = by '1_day 2 hours'", (original_appointment + '26 hours'::interval)::text as "postponed by = '24_hours'" from c; This is the result: original appointment | postponed by '1_day 2 hours' | postponed by = '24_hours'=20 = ------------------------+------------------------------+------------------= ------- 2023-03-11 20:00:00-08 | 2023-03-12 22:00:00-07 | 2023-03-12 = 23:00:00-07 Two different answers! The =E2=80=9Ctrick=E2=80=9D here is that the time = of the original appointment and the postponed times straddle the 2023 = =E2=80=9Cspring forward=E2=80=9D moment (at least as it happens in the = America/Los_Angeles timezone). And the resolution of what at first might = seem to be a bug come when you realized that you must make a distinction = between clock time and calendar time. This query sheds a bit more light on the matter: with c(i1, i2) as ( select '1 day 2 hours'::interval, '26 hours'::interval) select interval_mm_dd_ss(i1)::text as i1,=20 interval_mm_dd_ss(i2)::text as i2, (i1 =3D i2)::text as "i1 =3D i2", (i1=3D=3Di2)::text as "i1 =3D=3D i2" from c; I defined the =E2=80=9Cinterval_mm_dd_ss()=E2=80=9D function and the = =E2=80=9C=3D=3D=E2=80=9D operator. (I called it the =E2=80=9Cstrict = equality operator for interval values=E2=80=9D.) I believe that your question implies that you want my = =E2=80=9Cinterval_mm_dd_ss()=E2=80=9D function. I can=E2=80=99t be sure = what you want. But I dare to speculate that you might find it helpful to = read (at least) the references that I=E2=80=99ve listed below. Start = with the informal treatment in my blog post. Tom, Adrian, and David might remember my endless questions in this = general space in March 2021. This, from Tom, answers the present = question: = https://www.postgresql.org/message-id/DAABCBE6-6DFF-4C28-9CED-0AA8053FE12C= %40yugabyte.com=20 p.s. Some other posts came in while I was writing this. My advice on = =E2=80=9Cjustify_interval()=E2=80=9D is to avoid it. ____________________________________________________________ PostgreSQL Timestamps and Timezones: How to Navigate the Interval = Minefield = https://www.yugabyte.com/blog/postgresql-timestamps-timezones-interval-min= efield/ Two ways of conceiving of time: calendar-time and clock-time = https://docs.yugabyte.com/preview/api/ysql/datatypes/type_datetime/concept= ual-background/#two-ways-of-conceiving-of-time-calendar-time-and-clock-tim= e type interval_mm_dd_ss_t as (mm, dd, ss)=20 = https://docs.yugabyte.com/preview/api/ysql/datatypes/type_datetime/date-ti= me-data-types-semantics/type-interval/interval-utilities/#type-interval-mm= -dd-ss-t-as-mm-dd-ss The user-defined "strict equals" interval-interval "=3D=3D=E2=80=9C = operator = https://docs.yugabyte.com/preview/api/ysql/datatypes/type_datetime/date-ti= me-data-types-semantics/type-interval/interval-utilities/#the-user-defined= -strict-equals-interval-interval-operator --Apple-Mail=_BFD05007-9B0A-4A0C-BC02-98F1E15EB57C Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
ken.tanzer@gmail.com wrote:

Here's an example. Note that they come out formatted = differently with to_char, but evaluate as equal.  The = explanation(1) was that they were Equal but not Identical. I was = thinking getting the raw data about how they are stored would get at the = identicality issue:

WITH inters AS = (
    SELECT
      =   '1 day 2 hours'::interval AS i1,
    =     '26 hours'::interval AS i2
)
SELECT
    *,
  =   to_char(i1,'HH24:MM:SS') AS i1_char,
    = to_char(i2,'HH24:MM:SS') AS i2_char,
    i1=3Di2 = AS "Equal?"
FROM inters;

       i1       |   =  i2    | i1_char  | i2_char  | = Equal?
----------------+----------+----------+----------+--------
 1 day 02:00:00 | 26:00:00 | 02:00:00 | 26:00:00 | t

I struggled to understand this = whole murky area when I was writing the =E2=80=9CDate and time data = types and functionality=E2=80=9D section for the YugabyteDB doc. = (YugabyteDB uses the Postgres SQL processing code =E2=80=9Cas is=E2=80=9D = on top of its own distributed storage layer. All the examples in my doc = work identically in vanilla PG.)

The implied question here is this: is the interval =E2=80=9C1 = day 2 hours=E2=80=9D the same as the interval =E2=80=9C26 hours=E2=80=9D? = It might seem that the answer is =E2=80=9Cyes=E2=80=9D=E2=80=94as it = surely must be. But, sorry to say, that the answer is actually =E2=80=9Cno= =E2=80=9D. Confused? You will be. Most people are until they=E2=80=99ve = wrapped their head in a towel and puzzled it through for a few days. = This shows you what I mean:

set timezone =3D= 'America/Los_Angeles';
with c as (
  select '2023-03-11 20:00 = America/Los_Angeles'::timestamptz as original_appointment)
select
  original_appointment::text = as "original appointment",
  (original_appointment= + '1 day 2 hours'::interval)::text as "postponed by '1_day 2 = hours'",
  (original_appointment + '26 = hours'::interval)::text as "postponed by '24_hours'"
from = c;

This = is the result:

  original appointment  | postponed by = '1_day 2 hours' | postponed by '24_hours' 
------------------------+------------------------------+-------= ------------------
 2023-03-11 20:00:00-08 | = 2023-03-12 22:00:00-07       | 2023-03-12 = 23:00:00-07

Two = different answers! The =E2=80=9Ctrick=E2=80=9D here is that the time of = the original appointment and the postponed times straddle the 2023 = =E2=80=9Cspring forward=E2=80=9D moment (at least as it happens in = the America/Los_Angeles timezone). And the resolution of what at = first might seem to be a bug come when you realized that you must make a = distinction between clock time and calendar time.
This query sheds a bit more light on = the matter:

with c(i1, i2) as = (
  select '1 day 2 hours'::interval, '26 = hours'::interval)
select
  = interval_mm_dd_ss(i1)::text as i1, 
  = interval_mm_dd_ss(i2)::text as i2,
  (i1 =3D = i2)::text as "i1 =3D i2",
  (i1=3D=3Di2)::text as "i1 = =3D=3D i2"
from c;

I defined the = =E2=80=9Cinterval_mm_dd_ss()=E2=80=9D function and the =E2=80=9C=3D=3D=E2=80= =9D operator. (I called it the =E2=80=9Cstrict equality operator for = interval values=E2=80=9D.)

I believe that your question implies that you want = my =E2=80=9Cinterval_mm_dd_ss()=E2=80=9D function. I can=E2=80=99t = be sure what you want. But I dare to speculate that you might find it = helpful to read (at least) the references that I=E2=80=99ve listed = below. Start with the informal treatment in my blog post.

Tom, Adrian, and David = might remember my endless questions in this general space in March 2021. = This, from Tom, answers the present question:


p.s. Some other posts came in while I = was writing this. My advice on =E2=80=9Cjustify_interval()=E2=80=9D is = to avoid it.
____________________________________________________________
PostgreSQL Timestamps and Timezones: How to = Navigate the Interval Minefield
https://www.yugabyte.com/blog/postgresql-timestamps-timezones-i= nterval-minefield/

Two ways of = conceiving of time: calendar-time and clock-time
https://docs.yugabyte.com/preview/api/ysql/datatypes/type_datet= ime/conceptual-background/#two-ways-of-conceiving-of-time-calendar-time-an= d-clock-time

type interval_mm_dd_ss_t as (mm, dd, ss) 
https://docs.yugabyte.com/preview/api/ysql/datatypes/type_datet= ime/date-time-data-types-semantics/type-interval/interval-utilities/#type-= interval-mm-dd-ss-t-as-mm-dd-ss

The user-defined "strict equals" = interval-interval "=3D=3D=E2=80=9C operator
https://docs.yugabyte.com/preview/api/ysql/datatypes/type_datet= ime/date-time-data-types-semantics/type-interval/interval-utilities/#the-u= ser-defined-strict-equals-interval-interval-operator



= --Apple-Mail=_BFD05007-9B0A-4A0C-BC02-98F1E15EB57C--