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.96) (envelope-from ) id 1vMbqe-00EZPC-0R for pgsql-general@arkaria.postgresql.org; Sat, 22 Nov 2025 00:51:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vMbqc-00ANvX-08 for pgsql-general@arkaria.postgresql.org; Sat, 22 Nov 2025 00:51:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vMbqb-00ANvP-2K for pgsql-general@lists.postgresql.org; Sat, 22 Nov 2025 00:51:30 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vMbqZ-000oVX-2I for pgsql-general@postgresql.org; Sat, 22 Nov 2025 00:51:29 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 5AM0pOd82554466; Fri, 21 Nov 2025 19:51:24 -0500 From: Tom Lane To: Steve Crawford cc: PG-General Mailing List Subject: Re: Unexpected date conversion results In-reply-to: References: Comments: In-reply-to Steve Crawford message dated "Fri, 21 Nov 2025 16:09:01 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2554464.1763772684.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 21 Nov 2025 19:51:24 -0500 Message-ID: <2554465.1763772684@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Steve Crawford writes: > However, extracting the epoch from current_date returns 4pm the prior da= y > (i.e. 2025-11-21 00:00:00-00), in other words midnight 2025-11-21 UTC wh= ich > seems to be inconsistent behavior: > steve=3D> 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=3D# set timezone =3D 'America/Los_Angeles'; SET regression=3D# select to_timestamp(extract(epoch from current_date)); to_timestamp = ------------------------ 2025-11-20 16:00:00-08 (1 row) regression=3D# select to_timestamp(extract(epoch from current_date::timest= amptz)); = 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 expect= ed > 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