Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qo3Hf-00Cvhj-Tg for pgsql-general@arkaria.postgresql.org; Wed, 04 Oct 2023 14:55:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qo3He-00Gf56-7h for pgsql-general@arkaria.postgresql.org; Wed, 04 Oct 2023 14:55:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qo3Hd-00Gf4K-OL for pgsql-general@lists.postgresql.org; Wed, 04 Oct 2023 14:55:30 +0000 Received: from sonic301-21.consmr.mail.ir2.yahoo.com ([77.238.176.98]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qo3Ha-008AmO-Mk for pgsql-general@lists.postgresql.org; Wed, 04 Oct 2023 14:55:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1696431324; bh=YQGnPSuCR9d51mM1LAVg/akNkJDTebRO1hXHMrLi6xo=; h=Date:Subject:To:References:From:In-Reply-To:From:Subject:Reply-To; b=KRi6+iBiimEGk0uroJASqYOV4rYXwJ39OwwERv6oR6SHbI2tto00B4q32g0x+hz9HiT1OzGLdo4N5fqSpvjOLCAHvURBQxBzebJm+OIf4XI2Bi1uG0LqtzglIRI3HCn4v5IL/vvmqL855KhSWiX83YtsscCRPwbUlACrwYmhge4jB6mmM7PME2S2TWvENSI7oWY65T9dy7hKszRNIcClV+No5/fdywF94dfq5GU4xQJFt5R/13VD/IbXLct8wgdVgkIZ+OgNB3ttdsYPwhZdZCXCUnisSZe7m3J9Ft5I3Zs6OySPKWVgnkzJTqSsKEjF7NcY0yl5c1Lf5SylFsIqxw== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1696431324; bh=55F4GXHmZYmAZPCrRr+r0Mrag7i7P19ELkRLlNw2low=; h=X-Sonic-MF:Date:Subject:To:From:From:Subject; b=bBW5jePW+kmby949XfZNMSPDqKLO3y0DTb3onyF+6umC8C1Twpp/XenYE3zdjwe+YgS0IsUyImxwFWWeO7aRgHWn7LFrE/Kj0TNNqJ0V8ZvNu89LP1gIwqo/Faswhk4vm1n0IU/uL+gDmkeinES0p5Dte2q2QSiciqFCn1H20/xcO0ze83NRl6grGAL8erZ5ertQvO0nLdAQxFXVB1vnrDcuuUfx9aJtOS+aMmH/BB3Hsi2CQXzuGTJSe0ivXII4+jEUZlkyo6rp2pytrdHp0kjaGqCQg2oybx2AcZ27/81Ji27q51DclPokKmyCbN1MC+F9P4RTdNTm2Zh25XCVEw== X-YMail-OSG: ObM8oBEVM1kva3uobKgktJY_uipRJoYjEef_gf4vRiqveZPVvhAw7OkJ2ALPmds AKcoZ7alwySxkCwmkt6Zek4.iIkNUVKrns2AsVlVf0Iov50Ci2lkGiJDvmlbo2D9GlXH7FBzRb6w spQFJYwF6LMsVSplylSa2EHhVSxDdu79gLl45EHPiursVToJ2V0l7a9.5xQgMdV4AOKRsWBVMzfO vdl__hFIMNjT8flaq7GyPgNFsbVtdtpVtGgrNPR1PVvdFKp_dw65XXzjpACuyrXFdiK4S6kEWiIg eMGp7EVzL8Dl7gAhvrv0d4YcfrswwpeAul8Pmhuu6P08sUfHOTvZI0JIiQtKS15inMc6yNqcXCbJ PXz49n7V6B5oE8Y.L4h8d2LOaIXnBKmYeRNjSAavzyXmoafNADZZTORiSvHXqEp8E_vr53YheILh 8ZWihs5qlud00XqwugL_apDBsQaiiedm_xgUr0xzyuNvm3PmIbVHKUFZj9KVzROe_SYfY92bQ2P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54I00uA6gAuxlGk dt3wjn4_QcPd6Wb.Rxn_ZEQPfY9Ui3BNVvbdY6e7rCRLe.z2HSBwwEZcGk8I9xJKiznkuwBWiZCl s5KwByd80_2ylhM6np8dSza3fI1SUB8IZa__18i9RPO.M8T_EadaFRKJ49uqzELBp_6JODEgE48j HQ6SL9zbXPgz1UUgqC5.dVBW9eNXmKGyKQNzQwGAHHfQEjFSfKJQOiwSECXWcsCB5UoVBImuzFsu U3j3sjmi8WxLAmrYFA2zcpTdjO8US5keXSRZZRbW5.Z_St6Fjid1CzfvLEmBsOvNvcOTSpUMLAMS e3Wzt0RxaUtkcyZm1gZxWyRhs_Qmh6Z3B8STDLkE9vT0ickUwzgckL.o69JWBAzlelT1MFE84W8_ X1Hns1jD1u4JDlUGpvdU1CwEd7KWHygs9IXBBOx0na6j3tVHSKnXXO11RZhrvJrQKVJ6bEdZnBTq XMWkr39Z0VG7BzUE5J695J6AuZXE3ufcbgGsXn27v1oMcE3CfUh5CmWq3XL13Myg6MwS.Xy8CoyQ GVuy6wdQNSlRKsixfCTt1ga5r3GcaVfKPGueu1P6nQuPxLm4ffaTasttDAOi31d72I5oSU.yWMcB CkaWYqfpYzNAUtz6h6m6j4N2zZ9TTwVvC8CpMGPUOuX1AYQKwQ5dSNKBaC.XdgA-- X-Sonic-MF: X-Sonic-ID: 9d4e417a-ce75-4040-8f17-f84b0396c001 Received: from sonic.gate.mail.ne1.yahoo.com by sonic301.consmr.mail.ir2.yahoo.com with HTTP; Wed, 4 Oct 2023 14:55:24 +0000 Received: by hermes--production-ir2-788446cff9-gdl6j (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID 0f0c042cfed5555303a228471cc84325; Wed, 04 Oct 2023 14:55:19 +0000 (UTC) Content-Type: multipart/alternative; boundary="------------7HdQGLLkm9vSyGrh8FsrjJw6" Message-ID: <13eb1e62-9c00-4878-bb82-ef003eca884c@yahoo.com> Date: Wed, 4 Oct 2023 16:55:19 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Strategies for converting UTC data to local windows for arbitrary resolutions and timezones To: pgsql-general@lists.postgresql.org References: <2582288.1696428710@sss.pgh.pa.us> From: Marian Wendt In-Reply-To: <2582288.1696428710@sss.pgh.pa.us> X-Mailer: WebService/1.1.21797 mail.backend.jedi.jws.acl:role.jedi.acl.token.atz.jws.hermes.yahoo Content-Length: 8095 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------7HdQGLLkm9vSyGrh8FsrjJw6 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Am 04.10.2023 um 16:11 schrieb Tom Lane: > Steve Crawford writes: >> On Tue, Oct 3, 2023 at 7:33 PM Lincoln Swaine-Moore >> wrote: >>> 5) Ideally, the solution would not involve messing with the >>> server/connection's value of timezone. (Though I would be interested if >>> there was a solution that relaxed this constraint and was relatively >>> safe/compatible with transactions and psycopg2.) >> Note that setting the time zone is a client/connection setting so if you >> set it within a transaction, it will stay set when the transaction >> concludes. But time manipulation is tricky and trying to DIY reinvent the >> wheel is painful and often buggy. Let PostgreSQL do the work for you. > Expanding on that philosophy: you should be able to set the timezone > locally within a function, so that it wouldn't be that hard to make a > wrapper for generate_series that emulates the 4-argument version added > in v16. > > Rather than messing with manually saving and restoring the prevailing > zone, I'd let the function SET infrastructure do it for me. Sadly, > that SET clause only takes literal constant arguments, so it'd go > roughly like this: > > create function generate_series(timestamptz, timestamptz, interval, text) > returns setof timestamptz > strict immutable language plpgsql as > $$ > begin > perform set_config('timezone', $4, true); > return query select generate_series($1, $2, $3); > end > $$ set timezone = 'UTC'; > > Setting the zone to UTC is a useless step, but that triggers > restoring the previous zone when the function exits; simpler > and probably faster than coding the save/restore explicitly. > > Side note: whether this is really "immutable" is a matter for > debate, since time zone definitions tend to change over time. > But we chose to mark the new 4-argument version that way, > so you might as well do so too. > > regards, tom lane > > As far as Lincoln describes it, the series is not the real problem here, but is just intended to be a simplified example of his actual data.The consideration that you can use the time zone using a function should apply here...The following SELECT should show as an example that the desired result can be achieved (TIMEZONE set to 'Etc/UTC'). Variations of date_bin for 15 minutes or 1 hour should work similarly... SELECT     sub.gs AS ts_in_utc     ,sub.gs AT TIME ZONE 'America/New_York' AS ts_in_local_tz     ,date_bin('1 days', sub.gs AT TIME ZONE 'America/New_York', '2023-01-01') FROM (SELECT generate_series('2023-11-03 00:00:00Z'::timestamptz, '2023-11-07 00:00:00Z'::timestamptz, '5 minutes'::interval) AS gs) AS sub WHERE     sub.gs >= '2023-11-05 00:00:00 America/New_York'::timestamptz AND sub.gs < '2023-11-06 00:00:00 America/New_York'::timestamptz -- regards, marian wendt --------------7HdQGLLkm9vSyGrh8FsrjJw6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit


Am 04.10.2023 um 16:11 schrieb Tom Lane:
Steve Crawford <scrawford@pinpointresearch.com> writes:
On Tue, Oct 3, 2023 at 7:33 PM Lincoln Swaine-Moore <lswainemoore@gmail.com>
wrote:
5) Ideally, the solution would not involve messing with the
server/connection's value of timezone. (Though I would be interested if
there was a solution that relaxed this constraint and was relatively
safe/compatible with transactions and psycopg2.)

      
Note that setting the time zone is a client/connection setting so if you
set it within a transaction, it will stay set when the transaction
concludes. But time manipulation is tricky and trying to DIY reinvent the
wheel is painful and often buggy. Let PostgreSQL do the work for you.
Expanding on that philosophy: you should be able to set the timezone
locally within a function, so that it wouldn't be that hard to make a
wrapper for generate_series that emulates the 4-argument version added
in v16.

Rather than messing with manually saving and restoring the prevailing
zone, I'd let the function SET infrastructure do it for me.  Sadly,
that SET clause only takes literal constant arguments, so it'd go
roughly like this:

create function generate_series(timestamptz, timestamptz, interval, text)
returns setof timestamptz
strict immutable language plpgsql as
$$
begin
  perform set_config('timezone', $4, true);
  return query select generate_series($1, $2, $3);
end
$$ set timezone = 'UTC';

Setting the zone to UTC is a useless step, but that triggers
restoring the previous zone when the function exits; simpler
and probably faster than coding the save/restore explicitly.

Side note: whether this is really "immutable" is a matter for
debate, since time zone definitions tend to change over time.
But we chose to mark the new 4-argument version that way,
so you might as well do so too.

			regards, tom lane



As far as Lincoln describes it, the series is not the real problem here, but is just intended to be a simplified example of his actual data. The consideration that you can use the time zone using a function should apply here... The following SELECT should show as an example that the desired result can be achieved (TIMEZONE set to 'Etc/UTC'). Variations of date_bin for 15 minutes or 1 hour should work similarly...

SELECT
    sub.gs AS ts_in_utc
    ,sub.gs AT TIME ZONE 'America/New_York' AS ts_in_local_tz
    ,date_bin('1 days', sub.gs AT TIME ZONE 'America/New_York', '2023-01-01')
FROM (SELECT generate_series('2023-11-03 00:00:00Z'::timestamptz, '2023-11-07 00:00:00Z'::timestamptz, '5 minutes'::interval) AS gs) AS sub
WHERE
    sub.gs >= '2023-11-05 00:00:00 America/New_York'::timestamptz AND sub.gs < '2023-11-06 00:00:00 America/New_York'::timestamptz

--
regards, marian wendt
--------------7HdQGLLkm9vSyGrh8FsrjJw6--