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 1kMfD9-0003Vu-KY for pgsql-general@arkaria.postgresql.org; Sun, 27 Sep 2020 22:32:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kMfD5-0006rD-GG for pgsql-general@arkaria.postgresql.org; Sun, 27 Sep 2020 22:31:59 +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 1kMfD3-0006r6-Vv for pgsql-general@lists.postgresql.org; Sun, 27 Sep 2020 22:31:59 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kMfCz-0005ZR-8b for pgsql-general@lists.postgresql.org; Sun, 27 Sep 2020 22:31:56 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 228605C003D; Sun, 27 Sep 2020 18:31:51 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Sun, 27 Sep 2020 18:31:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=Z lUjf9UX/EXaAYmK+y1QgqdIoneYkcWE9SdHhri3ot0=; b=GDzQB+WPDLns3SoXK 48h3ZIBCofRhjKiiSHOkl9zVeUzfnqPS0Rv3cX1WH5BZXg1L4ERgAfL+dpXbDZGM Elx9sz4VapFexOeFqAX4CKTVcD+SlVpvEmYwFJJnsRAj+xqmXxBuRrMkseQUjqp/ kOBiwV1h7pSIxsuNcgCjTI/7y0R4fSaXiDFngDfiqJvTKBDxTSldCnxNtb0akijr 2Q5h4MJcj/5nDgDuZr1g6A+rMaNqrgTzg3oRSl4jqug3VOqUoBQnt5cFuj/c8iGx VenC8ki6A3Zlvo/jqCqS/ZXbyZy2dwidFMIWMR/klrANTHSVQXI17jbXHt01RNiP KCZlg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=ZlUjf9UX/EXaAYmK+y1QgqdIoneYkcWE9SdHhri3o t0=; b=sWqk8L276P52zOtIDdHFTek3XCzA1XoeDFGO864uyAoB43ZuT1S9oaoDs BogWK8J38Rp26HQLuT25Sa+9CkQaZH8HIuyOrwt2C7xQEW//m/yyH+SZr4qB790k RukT63fVXIH9UTNXQ8gbPTqxPe88jie4uS3A743lz13ZQxxLf+Nqy1LEBVP8h6mD 7RM8CTU3aB8mXqK1V4p5Ur5Tq1BflouHPQew6frlPKF1KDvoF6Cin9DslvW/wwUJ zXsHK5n2XHYeiNdc3iGr4obqx7z2B4QkvkGOVrr013LW8B/vLmVETl1jvwa0tnCF oQ6FJnmmmndmYLKHnC91/YQUQWuAw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvdehgdduudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepveevfeevfeeugeffkeekhfegieeufffgteeujedvgffh ueejjefhkeejleegtdegnecukfhppeejhedrudejvddrhedrvdefudenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghv vghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id 694273280059; Sun, 27 Sep 2020 18:31:50 -0400 (EDT) Subject: Re: Returning timestamp with timezone at specified timezone irrespective of client timezone To: "aNullValue (Drew Stemen)" , pgsql-general@lists.postgresql.org References: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com> From: Adrian Klaver Message-ID: <0c452adb-9047-09ec-7086-e804bf381c69@aklaver.com> Date: Sun, 27 Sep 2020 15:31:49 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/27/20 2:16 PM, aNullValue (Drew Stemen) wrote: > Hello, > > I've attempted to obtain help with this problem from several other > places, but numerous individuals recommended I ask this mailing list. > > What I need is for the ability to return a timestamp with timezone, > using the UTC offset that corresponds to a column-defined timezone, > irrespective of the client/session configured timezone. > > I have three columns in a table: > Timezone: 'US/Eastern' > Date: 2020-10-31 > Time: 08:00 > > The output I'm able to find includes these possibilities: > '2020-10-31 08:00:00' > '2020-10-31 12:00:00+00' > > Whereas what I actually need is: > '2020-10-31 08:00:00-05' > > Using the postgresql session-level timezone configuration won't work > because I need multiple timezones to be handled in a single set. > > Example code follows. I'm not using to_char in the examples as I likely > would in the production code, but I haven't found any way that it could > be helpful here regardless. > > --------------------------------------------------- > > SET TIME ZONE 'UTC'; > > CREATE TABLE loc > ( >     id serial not null, > timezone text not null, >     loc_date date NOT NULL, >     loc_time text NOT NULL, >     CONSTRAINT loc_pkey PRIMARY KEY (id), >     CONSTRAINT loc_loc_time_check CHECK (loc_time ~ > '(^(2[0-3]|[01][0-9]|[0-9]):?([0-5][0-9]):?([0-5][0-9])?$)|(^(1[012]|0[1-9]|[1-9]):[0-5][0-9] > [AaPp][Mm]$)'::text) > ) > ; > > INSERT INTO loc (timezone, loc_date, loc_time) VALUES > ('US/Eastern', '2020-10-31', '08:00'), > ('US/Eastern', '2020-11-03', '08:00'), > ('US/Central', '2020-10-31', '08:00'), > ('US/Central', '2020-11-03', '08:00'); > > SELECT * > , timezone(l.timezone, l.loc_date + l.loc_time::time without time zone) > tswtz > , (l.loc_date + l.loc_time::time without time zone) tswotz > FROM loc l > ORDER BY timezone, loc_date, loc_time > ; > > --------------------------------------------------- > > id |  timezone  |  loc_date  | loc_time |         tswtz          | > tswotz > ----+------------+------------+----------+------------------------+--------------------- >   7 | US/Central | 2020-10-31 | 08:00    | 2020-10-31 13:00:00+00 | > 2020-10-31 08:00:00 >   8 | US/Central | 2020-11-03 | 08:00    | 2020-11-03 14:00:00+00 | > 2020-11-03 08:00:00 >   5 | US/Eastern | 2020-10-31 | 08:00    | 2020-10-31 12:00:00+00 | > 2020-10-31 08:00:00 >   6 | US/Eastern | 2020-11-03 | 08:00    | 2020-11-03 13:00:00+00 | > 2020-11-03 08:00:00 > (4 rows) > > What I actually need is, in example id=7, '2020-10-31 08:00:00-05'. > > Is this even possible? Several people have proposed that I write a > custom function to do this on a per-row basis, which... I suppose I can > do... I'm just blown away that this isn't something that just works "out > of the box". > Something like?: select '2020-10-31' || ' 08:00 ' || utc_offset from pg_timezone_names where name = 'US/Eastern'; ?column? ---------------------------- 2020-10-31 08:00 -04:00:00 -- Adrian Klaver adrian.klaver@aklaver.com