"date_part (" era ", now () in the time zone" UTC ")" is not at the same time as "now () in the time zone" UTC "in postgresql

I am writing a web interface for a database (PHP / Postgresql) in which I need to store different dates / times. The time should always be entered on the client side at local time and displayed in local time. For storage purposes, I save all dates / times as integers (UNIX timestamps) and normalize to UTC. One specific field has a limitation that the filled timestamp is not allowed in the future, so I tried this with a database constraint ...

CONSTRAINT not_future 
    CHECK (timestamp-300 <= date_part('epoch', now() at time zone 'UTC'))

. -300 - give a 5-minute delay in case of a bit of desynchronous time between the browser and server. The problem is that this limitation always fails when sending the current time. I tested and found the following.

In the PostgreSQL client:

SELECT now() - returns the correct local time

SELECT date_part('epoch', now()) - returns the unix timestamp in UTC (checked by supplying a value to the date function in PHP, correcting its compensation in my time zone)

SELECT date_part('epoch', now() at time zone 'UTC')- returns the unix timestamp at two offsets the time zone to the west, for example. I am in GMT + 2, I get the timestamp GMT-2.

, " " UTC " ", , "" unix, AFAIK UTC, ​​ "" UTC? , - / .

+3
1

"now() " UTC " , UTC, TIMESTAMP WITHOUT TIME ZONE.

, "date_part" TIMESTAMP WITHOUT TIME ZONE ( ) ( "EPOCH", 1970-01- 01 00:00:00 UTC).

Postgres TIMESTAMP WITH TIME ZONE, . , "UTC" , , .

- :

select ((now() at time zone 'UTC') at time zone '<your_time_zone>') at time zone 'UTC';
+4

Source: https://habr.com/ru/post/1740703/


All Articles