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 1kMjc8-00089n-MH for pgsql-general@arkaria.postgresql.org; Mon, 28 Sep 2020 03:14:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kMjba-0002BY-T8 for pgsql-general@arkaria.postgresql.org; Mon, 28 Sep 2020 03:13:34 +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 1kMjba-000295-Gz for pgsql-general@lists.postgresql.org; Mon, 28 Sep 2020 03:13:34 +0000 Received: from mail-pf1-x443.google.com ([2607:f8b0:4864:20::443]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kMjbU-0001D1-1e for pgsql-general@lists.postgresql.org; Mon, 28 Sep 2020 03:13:33 +0000 Received: by mail-pf1-x443.google.com with SMTP id x22so8031130pfo.12 for ; Sun, 27 Sep 2020 20:13:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ardentperf-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:references:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=wVTAK3lzRo489IfCEhVL6ssWSenARukGfh5WOH07jPw=; b=IfazEUdL4PBN5S7TfCaHl2WUNO/C86x9jm4nz9DXD75JTszpy9hbTj8u52oIhx2Dn8 fKpmvf+dlxx8A5D1cW51kR08ja2dzsUUX0xM3NkVGob1EiRfMQ7EleqcgWHYBqBf1Six jiGOE7DVEIR1H/uzWV9R1WB5LR1ppCjbV3/DXWbpcJP9XHbqz5LObSxKAgpFjWjVO2ib LATUTYeZA2flbPSiBOrikncTcMtaOwTH47wnZEnReIwXCO852kyRqdUho7ZrnIhoaqKa AfYfzKK/adn75VpNWHSutwo8SXNgJ1MZcmxoZu3hjVmaTxTxlncFjnV3GCMRMro9bxd6 dtFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=wVTAK3lzRo489IfCEhVL6ssWSenARukGfh5WOH07jPw=; b=bKTJkQ5YqBSVOTMDSvWXArLJvzgEXyOzC0YO6Zw+Li7MlUqqzm/QX8C29wm8+lKruM Sv+32nXltiup+4aCsOl4iy0EDT0Dvhpk0vyIO2HwG/g2Idk0XYq18ZMTRJ94pBzmXBPC rTEFQjrSoqA3y3+DzM11dFwTtVNwDA0feqnWpDddL9LRwePccAtN6CQN4Dj25iVVaVvc mR9wPa/Shfgp4t5mUi9HCFiOo7GZWY6wg9/TU73pcvBFjGAdpzArpIprycXsae634Wlz 9027qrVbZ8/E/zZsunYpc5VGJgOnq9yRgM0/g7MxNPWUaY63MSgp6XTOG/Bqxj6K30OQ OvWQ== X-Gm-Message-State: AOAM531QSQDbGZr0l+Y7nbG08D9bwz7uHRDN7M7e9pgLAMiLPkPTHsex +dP4/c761Cx/mHFXB2VoUG/ljIzfULdhuQ== X-Google-Smtp-Source: ABdhPJwgVZ/+qUf4MCM7Wm+VR3iyKeE33eZH4pfg5bYKy95vyKjqPWwawWaJiS7rHBsS1rOWfWxoLg== X-Received: by 2002:a17:902:bf06:b029:d2:8abd:c8f3 with SMTP id bi6-20020a170902bf06b02900d28abdc8f3mr2097413plb.13.1601262806741; Sun, 27 Sep 2020 20:13:26 -0700 (PDT) Received: from 186590ce269b.ant.amazon.com ([205.251.233.51]) by smtp.gmail.com with ESMTPSA id g1sm9579501pfm.124.2020.09.27.20.13.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 27 Sep 2020 20:13:26 -0700 (PDT) Subject: Re: Returning timestamp with timezone at specified timezone irrespective of client timezone From: Jeremy Schneider To: Ron , pgsql-general@lists.postgresql.org References: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com> <6b269cb9-5572-acaa-de9c-fe1c705fed11@gmail.com> Message-ID: <4199d0ba-564d-c8a1-5d34-17cf06e022bc@ardentperf.com> Date: Sun, 27 Sep 2020 20:13:25 -0700 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/27/20 16:42, Jeremy Schneider wrote: > On 9/27/20 16:13, Ron wrote: >> On 9/27/20 4:16 PM, aNullValue (Drew Stemen) wrote: >>> 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. >> >> Are you really asking what the TZ offset was on a specific date (Like >> DST or not)? > > in lieu of having built-in support, a PL/pgSQL function to set the > session-level timezone in between processing each record is the best > approach i've thought of so far FYI, here's the hack approach I was thinking of. I intentionally didn't preserve the session's timezone in the transaction, but that could easily be done with a few more lines of PL/pgSQL. create or replace function to_char( v_tstz timestamp with time zone ,v_format text ,v_tz text ) returns text language plpgsql immutable parallel safe as $$ begin perform set_config('timezone',v_tz,true); return to_char(v_tstz,v_format); end; $$ ; SELECT id ,to_char(l.loc_date+l.loc_time::time ,'YYYY-MM-DD HH24:MI:SSOF' ,timezone) tsw FROM loc l ORDER BY timezone, loc_date, loc_time ; id | tsw ----+------------------------ 3 | 2020-10-31 03:00:00-05 4 | 2020-11-03 08:00:00-06 1 | 2020-10-31 09:00:00-04 2 | 2020-11-03 08:00:00-05 https://gist.github.com/aNullValue/ba838d6b40495695df0daa11c2748248 -- http://about.me/jeremy_schneider