public inbox for [email protected]  
help / color / mirror / Atom feed
Unexpected date conversion results
2+ messages / 2 participants
[nested] [flat]

* Unexpected date conversion results
@ 2025-11-22 00:09  Steve Crawford <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Steve Crawford @ 2025-11-22 00:09 UTC (permalink / raw)
  To: pgsql-general

Either there is a bug in my understanding or one in PostgreSQL. I expect a
date value to follow the current time zone setting and be interpreted as
midnight at the start of the given date. In many cases it does. Shown below
are the postgresql.conf settings and the psql client settings showing the
time zone to be America/Los_Angeles:

postgresql.conf:
log_timezone = 'America/Los_Angeles'
timezone = 'America/Los_Angeles'

Client time zone setting:

steve=> show timezone;
      TimeZone
---------------------
 America/Los_Angeles


Here is the value returned by current_timestamp and current_date:

steve=> select current_timestamp;
       current_timestamp
-------------------------------
 2025-11-21 14:48:06.948845-08


steve=> select current_date;
 current_date
--------------
 2025-11-21


Casting the current_date to a timestamp with time zone returns the expected
value (midnight November 21 Pacific Standard Time)

steve=> select current_date::timestamptz;
      current_date
------------------------
 2025-11-21 00:00:00-08


The output of to_char shows the same expected value:

steve=> select to_char(current_date, 'YYYY-MM-DD HH24:MI:SSTZH');
        to_char
------------------------
 2025-11-21 00:00:00-08


However, extracting the epoch from current_date returns 4pm the prior day
(i.e. 2025-11-21 00:00:00-00), in other words midnight 2025-11-21 UTC which
seems to be inconsistent behavior:

steve=> select to_timestamp(extract(epoch from current_date));
      to_timestamp
------------------------
 2025-11-20 16:00:00-08


steve=> select to_timestamp(extract(epoch from current_date))::date;
 to_timestamp
--------------
 2025-11-20


steve=> select to_timestamp(extract(epoch from '2025-11-21'::date))::date;
 to_timestamp
--------------
 2025-11-20


There was a time, like version 9-dot-something, when the above queries
performed as expected returning midnight in the current time zone but I
haven't been able to find a change document indicating this as an expected
change.

-Steve


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Unexpected date conversion results
@ 2025-11-22 00:51  Tom Lane <[email protected]>
  parent: Steve Crawford <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2025-11-22 00:51 UTC (permalink / raw)
  To: Steve Crawford <[email protected]>; +Cc: pgsql-general

Steve Crawford <[email protected]> writes:
> However, extracting the epoch from current_date returns 4pm the prior day
> (i.e. 2025-11-21 00:00:00-00), in other words midnight 2025-11-21 UTC which
> seems to be inconsistent behavior:

> steve=> select to_timestamp(extract(epoch from current_date));
>       to_timestamp
> ------------------------
>  2025-11-20 16:00:00-08

The reason this is misbehaving is that there are two versions of
extract(), one for timestamp-with-timezone input and one for
timestamp-without-timezone input.  The latter applies no
timezone correction, so it won't give true Unix-epoch results
unless you are in UTC zone to start with.

By default, a date will be promoted to timestamp-without-timezone not
timestamp-with-timezone, so the above doesn't give what you want.
It'd work better with a cast to force the right interpretation:

regression=# set timezone = 'America/Los_Angeles';
SET
regression=# select to_timestamp(extract(epoch from current_date));
      to_timestamp      
------------------------
 2025-11-20 16:00:00-08
(1 row)

regression=# select to_timestamp(extract(epoch from current_date::timestamptz)); 
      to_timestamp      
------------------------
 2025-11-21 00:00:00-08
(1 row)

> There was a time, like version 9-dot-something, when the above queries
> performed as expected returning midnight in the current time zone but I
> haven't been able to find a change document indicating this as an expected
> change.

A bit of experimenting says the current behavior dates to 9.2.
I've not checked the release notes to see if it was documented,
but in any case it's stood for long enough now that I doubt
we'd change it.

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-11-22 00:51 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-11-22 00:09 Unexpected date conversion results Steve Crawford <[email protected]>
2025-11-22 00:51 ` Tom Lane <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox