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 1ruGXU-0042Su-Oq for pgsql-general@arkaria.postgresql.org; Tue, 09 Apr 2024 18:49:49 +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 1ruGXT-009MG1-Kw for pgsql-general@arkaria.postgresql.org; Tue, 09 Apr 2024 18:49:47 +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 1ruGXT-009MFC-65 for pgsql-general@lists.postgresql.org; Tue, 09 Apr 2024 18:49:47 +0000 Received: from mail-qv1-xf35.google.com ([2607:f8b0:4864:20::f35]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1ruGXQ-0023UZ-Dn for pgsql-general@lists.postgresql.org; Tue, 09 Apr 2024 18:49:45 +0000 Received: by mail-qv1-xf35.google.com with SMTP id 6a1803df08f44-6964b1c529cso44678806d6.0 for ; Tue, 09 Apr 2024 11:49:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712688583; x=1713293383; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=qIQgizB6TfCk+q7uXVZlEPdi1tclvosstXaukgwlGQg=; b=MlLSbEjZe0HImVAgyPXbqa4Z2TRqNaLgpSUpPaAWu0LukD8w3gPJ4EcfmKO/pfCf79 I4hWPwMPP5KtYo0BDe1VNFlf3tN9BbJj8tpQKkNcIJbF+d5jVzmoiFfz9FfZlutgoEoC x8WYWCfh9pxlDE1MKFgTwiU+XjmzHMGM7iloboP5mow88YWdKz8nBG1gTCX5DPpmgtvT X328g49WvaLiRzTl9yNAI8Z12H2HXRzFcS78TbH2NFxQ/9YH+gs2KDbrgR/qjtleVylv mn+DEsBZRLRQUWIWREbO64KERqZ+aa8KVFbHS0kI0ePRAueXTUcDpYXnjDoXYqQfkvWq CQdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712688583; x=1713293383; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qIQgizB6TfCk+q7uXVZlEPdi1tclvosstXaukgwlGQg=; b=R+rQZbwDNyKq4jgBBR7tjiIrvK9hDkmcsGSMZxFpbSDbeeNSAntufKowHNhIlMl8zD imiUiZvsos8X7he7rzRwlu27N1TpxqUVJfndtXjL8bQVb5u6OudSbMd6qNY/Uw2R8DTJ /4EIV1+Xiu9mIzGV5oAYji79xBXj4kezrWfcfQ7/fD4Zki6/J8PdBE6PjAhl28lBdSIa PgUjYUavuGySvuh0vtW3E2J68o7Ojvc6Yl9qZKupmoI7BXnxu873DFAprZ9RSimiKhdA 3/pkMl/aP5PykdlO1dhxBSyVc7JxIuJLm9ay6gA2Qy+2itDag1EkEnlUj8pg5InqQcOy ZOzA== X-Forwarded-Encrypted: i=1; AJvYcCUCU48XgovACW6FDNRzjqHlMomHcWs+AyTchM920yEBQxUas32lyLd5YMQNOzqd/0DZ553+0zc/DXMulLMjYS0QBMkUaKA/mTa4EY1CCjVum1N5 X-Gm-Message-State: AOJu0Ywi+auHY9w804s7kDsObFhqeoQnIUXWvy5QklNN6Iahup8dBQXA gucoyR3yZSgbVQMnolFhUxlR8mkVoi9ylCeF8FCswgdGmaw8REZKVfRv3rWjXvx8WMOt3HRVpJc 4UPNAQ6EkgIYL1E32IIL6JUiwlCQ= X-Google-Smtp-Source: AGHT+IH4PMA6xNIbDWlImGfdqRXlUWPTZln4mUSXUXVim/u23tGQqesrgZ8/eEvPb1a0bt23lWWQTq+LTBWE40DD8P4= X-Received: by 2002:a05:6214:ca3:b0:699:1e54:fcc7 with SMTP id s3-20020a0562140ca300b006991e54fcc7mr548791qvs.12.1712688583039; Tue, 09 Apr 2024 11:49:43 -0700 (PDT) MIME-Version: 1.0 References: <3756920.1712682208@sss.pgh.pa.us> In-Reply-To: From: yudhi s Date: Wed, 10 Apr 2024 00:19:29 +0530 Message-ID: Subject: Re: Issue with date/timezone conversion function To: Lok P Cc: Tom Lane , pgsql-general Content-Type: multipart/alternative; boundary="0000000000009a609a0615ae62e1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000009a609a0615ae62e1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Below should work... date_trunc('hour', timestamp_column *AT TIME ZONE '*America/New_York') + (((date_part('minute', timestamp_column *AT TIME ZONE '*America/New_York')::int / 15)::int) * interval '15 min') On Tue, Apr 9, 2024 at 11:54=E2=80=AFPM Lok P wrote: > > On Tue, Apr 9, 2024 at 10:33=E2=80=AFPM Tom Lane wrot= e: > >> Lok P writes: >> > These tables are INSERT only tables and the data in the create_timesta= mp >> > column is populated using the now() function from the application, whi= ch >> > means it will always be incremental, and the historical day transactio= n >> > count is going to be the same. However surprisingly the counts are >> changing >> > each day when the user fetches the result using the below query. So my >> > question was , if there is any issue with the way we are fetching the >> data >> > and it's making some date/time shift which is why the transaction coun= t >> > looks to be changing even for the past days data? >> >> Well, your cutoff time "CURRENT_TIMESTAMP - INTERVAL '10 day'" is >> constantly moving, so that'd account for shifts in what's perceived >> to belong to the oldest day. Maybe you want "CURRENT_DATE - 10" >> instead? >> >> > And also somehow this >> > conversion function "DATE_TRUNC('hour', create_timestamp AT TIME ZONE >> > 'EST')" is showing time in CST but not EST, why so? >> >> 'EST' is going to rotate to UTC-5, but that's probably not what >> you want in the summer. I'd suggest AT TIME ZONE 'America/New_York' >> or the like. See >> >> >> https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-= TIMEZONES >> >> regards, tom lane >> > > > Thank you so much. You are correct. The AT TIME ZONE 'America/New_York' > is giving correct EST time conversion. > > But I think regarding why it looks to be shifting i.e. the same time > duration appears to be holding a different count of transactions while th= e > base table is not getting updated/inserted/deleted for its historical > create_timestamps, I suspect the below conversion part. > > The task is to count each ~15minutes duration transaction and publish in > ordered fashion i.e. something as below, but the way it's been written > seems wrong. It's an existing script. It first gets the date component wi= th > truncated hour and then adds the time component to it to make it ~15minut= es > interval. Can it be written in some simple way? > > 9-apr-2024 14:00 12340 > 9-apr-2024 14:15 12312 > 9-apr-2024 14:30 12323 > 9-apr-2024 14:45 12304 > > *DATE_TRUNC('hour', create_timestamp AT TIME ZONE '*America/New_York'*) += * > *(EXTRACT(MINUTE FROM create_timestamp AT TIME ZONE '*America/New_York*') > / 15 * 15) * INTERVAL '15 minute'* > --0000000000009a609a0615ae62e1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Below should work...

date_trunc('ho= ur', timestamp_column=C2=A0AT TIME ZONE 'America/New_York= 9;) +=C2=A0(((date_part('minute', timestamp_column=C2=A0AT TIME = ZONE 'America/New_York')::int / 15)::int) * interval '15 mi= n')

On Tue, Apr 9, 2024 at 11:54=E2=80=AFPM Lok P <loknath.73@gmail.com> wrote:

On Tue, Apr 9, 2024 at 10:33=E2=80=AFPM Tom Lane <tgl@sss.pgh.pa.us> wrote:<= br>
Lok P <loknath.73@gmail.com> writes:
> These tables are INSERT only tables and the data in the create_timesta= mp
> column is populated using the now() function from the application, whi= ch
> means it will always be incremental, and the historical day transactio= n
> count is going to be the same. However surprisingly the counts are cha= nging
> each day when the user fetches the result using the below query. So my=
> question was , if there is any issue with the way we are fetching the = data
> and it's making some date/time shift which is why the transaction = count
> looks to be changing even for the past days data?

Well, your cutoff time "CURRENT_TIMESTAMP - INTERVAL '10 day'&= quot; is
constantly moving, so that'd account for shifts in what's perceived=
to belong to the oldest day.=C2=A0 Maybe you want "CURRENT_DATE - 10&q= uot;
instead?

> And also somehow this
> conversion function "DATE_TRUNC('hour', create_timestamp = AT TIME ZONE
> 'EST')" is showing time in CST but not EST, why so?

'EST' is going to rotate to UTC-5, but that's probably not what=
you want in the summer.=C2=A0 I'd suggest AT TIME ZONE 'America/New= _York'
or the like.=C2=A0 See

https://www.postgre= sql.org/docs/current/datatype-datetime.html#DATATYPE-TIMEZONES

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane


=C2=A0= Thank you so much. You are correct. The AT TIME ZONE 'America/New_York&= #39; is giving correct EST time conversion.

But I = think regarding why it looks to be shifting i.e. the same time duration app= ears to be holding a different count of transactions while the base table i= s not getting updated/inserted/deleted for its historical create_timestamps= , I suspect the below conversion part.

The task is= to count each ~15minutes duration transaction and publish in ordered fashi= on i.e. something as below, but the way it's been written seems wrong. = It's an existing script. It first gets the date component with truncate= d hour and then adds the time component=C2=A0to it to make it ~15minutes in= terval. Can it be written in=C2=A0some simple way?

9-apr-2024 14:00=C2=A0 =C2=A0 =C2=A012340
9-apr-2024 14:15=C2=A0= =C2=A0 =C2=A012312
9-apr-2024 14:30=C2=A0 =C2=A0 =C2=A012323
9-apr-2024 14:45=C2=A0 =C2=A0 =C2=A012304

<= i>DATE_TRUNC('hour', create_timestamp AT TIME ZONE 'America= /New_York') +
(EXTRACT(MINUTE FROM create_timestamp= AT TIME ZONE 'America/New_York') / 15 * 15) * INTERVAL '= ;15 minute'=C2=A0
--0000000000009a609a0615ae62e1--