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 1pGU6O-0001MS-FP for pgsql-general@arkaria.postgresql.org; Sat, 14 Jan 2023 00:08:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pGU6N-0008TD-BP for pgsql-general@arkaria.postgresql.org; Sat, 14 Jan 2023 00:08:51 +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 1pGU6M-0008T2-8u for pgsql-general@lists.postgresql.org; Sat, 14 Jan 2023 00:08:51 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pGU6I-00071e-PL for pgsql-general@lists.postgresql.org; Sat, 14 Jan 2023 00:08:49 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id A83453200922; Fri, 13 Jan 2023 19:08:43 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Fri, 13 Jan 2023 19:08:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1673654923; x= 1673741323; bh=RuJv9WeRf07tUQ0UKyGVj9JXwGbKEN1b9Co2sCDvTDY=; b=m FiNbMMyMfQHlx28AS1/HNtWrys447Ta42S/JOOcKZBEwVZB4q3dxhdNwIMiWxW4w ec0+s2MXbXALpJJQy942J+mG20lDM1Y/R2j2DWrd+T05YrRSy9JDLvsNfU4OOrQS HuY5d5vn3sMM6SjVetP3UU/0jPIJKUYMqqNFzYelmDvN0KDWLdZs9fiI6oSggPgh RcV9S++n+xn01nZylfj9bRc0DhBZXy9blaywLdazPJwSiZPpuV+G2Cn7waO9hXHg ddP94SUpADOBSIrZwDk7p1t932++8u5tooa4PK1yCoE3Yrynb/OzdBSyHqLi2Fz4 +XeH8yIF+yR6wpwpVjBYA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1673654923; x= 1673741323; bh=RuJv9WeRf07tUQ0UKyGVj9JXwGbKEN1b9Co2sCDvTDY=; b=d x1rRXE/YdAyS4XB3GcBAsjeiGmqwuOnUQVfJUx+YGtNnAl/YR0+1s8tsTUSUf0Hs aEELdiYghqv+v5i2sVCjCm9rzYCcXaQtEuxaMF54XhyK19pr7SqW/v5DGyvwYoTK 1z7IN1PACLOw8AUVSILpVu1wZNpsO6AMSxxXI61rQDjZKFq0IRX83p8VJavvXzOr Xt9VQ93oeXxZfGqwOIJ3kt2B3oqsSL4fKxB+CTEwEfub9b245x2oksnPSkbmgnd2 TfApc4ZowfpkyvEyf3cQLfbJbgsdhpmwmVzEG1sRv3llJvOGAFSYQ+DUY2Hs94Lf cYHs9AmSNoMCl9qiIkwIQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrleelgddvtdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefkffggfgfuvfevfhfhjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpefhgeevkeefleffvdfgvdefffdtueejheehueeifffh feeihfdvhffgtefgvdeugfenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgne cuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhi rghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 13 Jan 2023 19:08:42 -0500 (EST) Message-ID: Date: Fri, 13 Jan 2023 16:08:41 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: Intervals and ISO 8601 duration Content-Language: en-US To: Bryn Llewellyn , Ken Tanzer Cc: Tom Lane PostgreSQL , david.g.johnston@gmail.com, Sebastien Flaesch , pgsql-general list References: <953416.1673536094@sss.pgh.pa.us> <53c0b4d4-b80a-a35b-ce18-61058794c9bb@aklaver.com> From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 1/13/23 16:03, Bryn Llewellyn wrote: >> ken.tanzer@gmail.com wrote: >> > I struggled to understand this whole murky area when I was writing the > “Date and time data types and functionality” section for the YugabyteDB > doc. (YugabyteDB uses the Postgres SQL processing code “as is” 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 “1 day 2 hours” the > same as the interval “26 hours”? It might seem that the answer is > “yes”—as it surely must be. But, sorry to say, that the answer is > actually “no”. Confused? You will be. Most people are until they’ve > wrapped their head in a towel and puzzled it through for a few days. Or read the docs: https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-OUTPUT "Internally interval values are stored as months, days, and microseconds. This is done because the number of days in a month varies, and a day can have 23 or 25 hours if a daylight savings time adjustment is involved." > This shows you what I mean: > > set timezone = '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 “trick” here is that the time of the original > appointment and the postponed times straddle the 2023 “spring forward” > 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. -- Adrian Klaver adrian.klaver@aklaver.com