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 1kMgDj-00066k-1e for pgsql-general@arkaria.postgresql.org; Sun, 27 Sep 2020 23:36:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kMgDg-0004Rv-OC for pgsql-general@arkaria.postgresql.org; Sun, 27 Sep 2020 23:36:40 +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 1kMgDg-0004Ro-GJ for pgsql-general@lists.postgresql.org; Sun, 27 Sep 2020 23:36:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kMgDd-0006B3-Vz for pgsql-general@lists.postgresql.org; Sun, 27 Sep 2020 23:36:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 08RNaYru170448; Sun, 27 Sep 2020 19:36:34 -0400 From: Tom Lane To: "aNullValue (Drew Stemen)" cc: pgsql-general@lists.postgresql.org Subject: Re: =?UTF-8?Q?Returning_timestamp_with_timezone_at_specified_timezone_irresp?= =?UTF-8?Q?ective_of_client_timezone?= In-reply-to: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com> References: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com> Comments: In-reply-to "aNullValue (Drew Stemen)" message dated "Sun, 27 Sep 2020 17:16:54 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <170446.1601249794.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 27 Sep 2020 19:36:34 -0400 Message-ID: <170447.1601249794@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "aNullValue (Drew Stemen)" writes: > What I need is for the ability to return a timestamp with timezone, usin= g the UTC offset that corresponds to a column-defined timezone, irrespecti= ve of the client/session configured timezone. I might be confused, but I think that the way to get the timestamptz values you want is # SELECT * , ((l.loc_date || ' ' || l.loc_time)::timestamp) at time zone timezone tsw= tz FROM loc l ORDER BY timezone, loc_date, loc_time ; id | timezone | loc_date | loc_time | tswtz = ----+------------+------------+----------+------------------------ 3 | US/Central | 2020-10-31 | 08:00 | 2020-10-31 13:00:00+00 4 | US/Central | 2020-11-03 | 08:00 | 2020-11-03 14:00:00+00 1 | US/Eastern | 2020-10-31 | 08:00 | 2020-10-31 12:00:00+00 2 | US/Eastern | 2020-11-03 | 08:00 | 2020-11-03 13:00:00+00 (4 rows) These are the correct timestamptz values, as displayed with the session timezone set to UTC as per your example. If what you're asking for is that the *presentation* vary per the timezone column, then you have to fake it, because timestamptz_out simply will not do that for you. However, it's not very clear to me why you don't just concatenate the loc_date, loc_time, and timezone columns if that's the presentation you want. Alternatively, if this was just a dummy example and you really mean you've done a timestamptz calculation and now want to present it in a varying timezone, you could do something like this, using now() as a placeholder for some timestamptz expression: # select timezone, now(), (now() at time zone timezone) || ' ' || timezone= tswtz from loc l; timezone | now | tswtz = = ------------+-------------------------------+-----------------------------= ---------- US/Eastern | 2020-09-27 23:32:19.321202+00 | 2020-09-27 19:32:19.321202 U= S/Eastern US/Eastern | 2020-09-27 23:32:19.321202+00 | 2020-09-27 19:32:19.321202 U= S/Eastern US/Central | 2020-09-27 23:32:19.321202+00 | 2020-09-27 18:32:19.321202 U= S/Central US/Central | 2020-09-27 23:32:19.321202+00 | 2020-09-27 18:32:19.321202 U= S/Central (4 rows) The key thing to understand here is that AT TIME ZONE either rotates from local time to UTC, or vice versa, depending on whether its input is timestamp or timestamptz. regards, tom lane