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 1oLWpM-0003Y4-MY for pgsql-sql@arkaria.postgresql.org; Tue, 09 Aug 2022 21:31:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oLWpK-000499-UR for pgsql-sql@arkaria.postgresql.org; Tue, 09 Aug 2022 21:31:50 +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 1oLWpJ-00047u-OG for pgsql-sql@lists.postgresql.org; Tue, 09 Aug 2022 21:31:50 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oLWpG-0002NB-RT for pgsql-sql@lists.postgresql.org; Tue, 09 Aug 2022 21:31:48 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 09C433200996 for ; Tue, 9 Aug 2022 17:31:43 -0400 (EDT) Received: from imap44 ([10.202.2.94]) by compute4.internal (MEProxy); Tue, 09 Aug 2022 17:31:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnericson.me; h=cc:content-type:date:date:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to; s=fm3; t= 1660080703; x=1660167103; bh=ZucVc2mmm2T7hPDMrs6i7DAmxsXUKmlQZSL zUGPsTLg=; b=oUX14FpldXuT4kMsrQeqsOG4gzy1bO0KQ1fDTlddTGz0h2bIvvA 7HgdVx5zRupaVdAY5B/oO+4WVezBhxOCl7sTp9Q4c9lMCXCRbgUjcFgvQQJlULHj 68JHd8MA+JGXvwVFt7V4Lu6VWaJWEq5aegwzBMAiiIht6g5OrRfkRqAxnfgWu6Td BqY8HWzdJXawRCloo72nUASzNsYtjt/dUSc1y6zDYvRzcC5f63tT8H+6Yp9q1JOZ TwDlpbVtVGbe0URWi2up1J3YBi+aQw8KBZIIxLsnm8sLfaaqo06aiED7QLcPd5PF gSfv2onsb9ZsOG4j5oTnEGW/IgxRmdXeRiw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:message-id:mime-version :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1660080703; x= 1660167103; bh=ZucVc2mmm2T7hPDMrs6i7DAmxsXUKmlQZSLzUGPsTLg=; b=k jSu5CTQyq4Tw1MjD6+bZFInB3E6L9rHYr41SCmbvODYbZ1eT1u7uPEDT2mD05Uiw OmKOR6SJE14D2Cpn8q1cFJ6zXwo+TotH1BfL/FW+cbO/dY5/WO14tWFMJsgrNjOk eMkelkmjjSGTMcppaMVsMDFoqzJEhLoxtT5FN4w9kN+7RhekCvMm/JGYEYSrB6e6 lH7D3fzyo4xFJKP8l9VmpqQFWbbjNtsL7axmWcool4731LYAhPOdGWsuyrjc/6F6 MEJ7G3MV01VM8vEu6mIsKnrvVvKR8JP/mKS2tqpVFBiBVLaIccaKrmOsWSnSMWjt hNYt8YAUgThxmS6iQIWWg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdegtddgudeivdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkfffhvffutgesrgdtre erreertdenucfhrhhomhepfdflohhhnhcugfhrihgtshhonhdfuceolhhishhtsehjohhh nhgvrhhitghsohhnrdhmvgeqnecuggftrfgrthhtvghrnhephedtteehteehtefhveeiff dvueduudegleeltdekfeduheekgfeivdegheeluddtnecuffhomhgrihhnpehpohhsthhg rhgvshhqlhdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpehlihhsthesjhhohhhnvghrihgtshhonhdrmhgv X-ME-Proxy: Feedback-ID: id3f144f1:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 6C79836A0071; Tue, 9 Aug 2022 17:31:43 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-811-gb808317eab-fm-20220801.001-gb808317e Mime-Version: 1.0 Message-Id: Date: Tue, 09 Aug 2022 17:31:23 -0400 From: "John Ericson" To: pgsql-sql Subject: Alternative to "AT TIME ZONE" that is less of a foot-gun? Content-Type: multipart/alternative; boundary=93ba17433ceb46c898faa48e7ae9b83a List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --93ba17433ceb46c898faa48e7ae9b83a Content-Type: text/plain According to the docs , the semantics of "`AT TIME ZONE" on timestamps is convert back and forth between their "with timezone" and "without timezone" variants.` This "toggling" behavior caught us by surprise. https://www.postgresql.org/message-id/2f2cb1690604200631l47c2a5b4xabe92bb6d22b4f2c@mail.gmail.com is an email from years ago with someone else having a similar confusion. As I see it, the issue is in most programming languages --- informal English, "as" would seem to imply an idempotent conversion operation, not an inverse operation! As the reply to that email says, "It's really two distinct operations depending on whether the input is a timestamp with or without timezone." That avoids a foot-gun of confusing "toggling" behavior. `I understand that `the semantics of "AT TIME ZONE" are fixed `per the SQL spec`, and so we are stuck with them. But on the model of ``timezone`(*`zone`*, *`timestamp`*) which `PostgreSQL also supports, might we add two new functions which only do half the semantics for safer programming? They could be something like this: * ``with_timezone(*zone, timestamp*``* without time zone*) returns *timestamp with time zone* * ``without_timezone(*zone, timestamp*``* with time zone*) returns *timestamp without time zone* where giving them arguments with of the wrong type (e.g. the wrong with-timezone-ness) is simply and error. SQL has lots of accumulated warts, and this hardly scratches the surface of what would be needed to make it truely "idiot proof", but it strikes me a simple, easy to implement new feature with clear benefits. Curious what you all think, John --93ba17433ceb46c898faa48e7ae9b83a Content-Type: text/html Content-Transfer-Encoding: quoted-printable
According to th= e docs, the semantics of "AT TIME ZONE" on timestamps is convert back and forth b= etween their "with timezone" and "without timezone" variants.
=

This "toggling" behavior caught us by surprise= . https://www.postgresql.org/messa= ge-id/2f2cb1690604200631l47c2a5b4xabe92bb6d22b4f2c@mail.gmail.com is= an email from years ago with someone else having a similar confusion.

As I see it, the issue is in most programmin= g languages --- informal English, "as" would seem to imply an idempotent= conversion operation, not an inverse operation! As the reply to that em= ail says, "It's really two distinct operations depending on whether the = input is a timestamp with or without timezone." That avoids a foot-gun o= f confusing "toggling" behavior.

I understand that the semantics of "AT TIME ZONE" a= re fixed per the SQL spec, and so we are = stuck with them. But on the model of timezone(zone, <= /span>timestamp<= /i>) which PostgreSQL also supports, might we a= dd two new functions which only do half the semantics for safer programm= ing? They could be something like this:
  • with_timezone(zone, timestamp without time zone) returns timestamp with time zo= ne
  • with= out_timezone(zone, timestamp with time zone)= returns timestamp without time zone
where giving t= hem arguments with of the wrong type (e.g. the wrong with-timezone-ness)= is simply and error.

SQL has lots of accum= ulated warts, and this hardly scratches the surface of what would be nee= ded to make it truely "idiot proof", but it strikes me a simple, easy to= implement new feature with clear benefits.

Curious what you all think,

John
= --93ba17433ceb46c898faa48e7ae9b83a--