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 1kMpI1-0002sZ-AC for pgsql-general@arkaria.postgresql.org; Mon, 28 Sep 2020 09:17:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kMpI0-0001p8-9M for pgsql-general@arkaria.postgresql.org; Mon, 28 Sep 2020 09:17:44 +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 1kMpI0-0001of-1Y for pgsql-general@lists.postgresql.org; Mon, 28 Sep 2020 09:17:44 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kMpHx-0003Zt-La for pgsql-general@lists.postgresql.org; Mon, 28 Sep 2020 09:17:43 +0000 Received: by mail-wr1-x444.google.com with SMTP id z1so419449wrt.3 for ; Mon, 28 Sep 2020 02:17:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=oVIibGo9XGNtHrUQjMXy3WHBe9cC4BB2RRC117QFdEI=; b=a7CQfWL29S8Br2jQ6vZNvpkPFsc8Yj2Iw+iOPz9VOTvVMytBRU6iYXkV50S/jXGYWI Zmvwi1hOSPz7o+q9kkbYEU0DzpC9IRY7nXYJ7cKRvOxReXWkQbFaWi0HSrdvCZFDaY07 JUEVQ/0/jF/T2OLzGAxNQHGTZHZrYoQoMfQB0nvGW+e8DVsMBuzhNhUI2HBaMKjdTQOm kzyfgQIpAY2ZSvnPyB/GbPCje0ZlaL9TZR4k6N1ekLO1YAVU6eaG0tN16YwmZFgnNvg1 uH7XUNgvteGmX5MY7UdfsxLX9sADFbUqILtC4y0E7oFMEJI2uplAcNDdU1/5ntLSkihI 2Ojg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=oVIibGo9XGNtHrUQjMXy3WHBe9cC4BB2RRC117QFdEI=; b=fKSQobb7pDX+agM8gQ9/Ok3F1rweBNMkB7kdZnYx4grOg1B/1pTHKnF0T+ahMb/73n vCj7+UBIzB6P8xr+ZnaiqSskQVww+T1R3MzzYUm+vhKcQ5ixjSa0/0lXnFJdwELQyOvA 6Dr6c/j5sWuwPdTat6zVEeDNTKucamc7QmZaGzgmA2kiP1CletBUSTBP7UWXZ3V3anji LGogWcp8Yl38Z7egNqcc0Zu8K7nPRtNZ0WzpwQl64CBIwEkkrUU20dP8uWb/aOli6TmT TX2A39e9R3PyOWrdoKyoDSNxgYEd6JoWdD4OytYfWqu7MUQK8bWqmgA8JFYpQ6psc3fN iRVA== X-Gm-Message-State: AOAM53209qUXmepJqI+AXyekpyYwoi9zd0rEws3YsMw5mMPcv6DCTS2t W4SADOya3yneCMKSQgVtlyGcag== X-Google-Smtp-Source: ABdhPJwrNXNVTUuNlypdLQpdKPlBvXPIQ3LaQkQT1kMkj22buc3XesZfDy5y6QrjG266J+CZuW+XVQ== X-Received: by 2002:a05:6000:1141:: with SMTP id d1mr616719wrx.350.1601284660375; Mon, 28 Sep 2020 02:17:40 -0700 (PDT) Received: from localhost.localdomain ([212.186.219.234]) by smtp.gmail.com with ESMTPSA id w15sm644807wro.46.2020.09.28.02.17.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 02:17:39 -0700 (PDT) Message-ID: <634911a0f87add08d1ba44b5ceadc49bc10fe010.camel@cybertec.at> Subject: Re: Returning timestamp with timezone at specified timezone irrespective of client timezone From: Laurenz Albe To: "aNullValue (Drew Stemen)" , pgsql-general@lists.postgresql.org Date: Mon, 28 Sep 2020 11:17:39 +0200 In-Reply-To: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com> References: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-1.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, 2020-09-27 at 17:16 -0400, aNullValue (Drew Stemen) wrote: > 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. Try a function like this: CREATE FUNCTION format_timestamp( ts timestamp with time zone, time_zone text ) RETURNS text LANGUAGE plpgsql IMMUTABLE STRICT AS $$DECLARE tz text; result text; BEGIN tz := current_setting('timezone'); PERFORM set_config('timezone', time_zone, TRUE); result := ts AT TIME ZONE 'UTC' AT TIME ZONE 'UTC'; PERFORM set_config('timezone', tz, TRUE); RETURN result; END;$$; SELECT format_timestamp(current_timestamp, '+08'); format_timestamp ------------------------------- 2020-09-28 17:15:25.083677+08 (1 row) Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com