I have a table in PostgreSQL:
CREATE TABLE tableA
(
time_A time with timezone
);
How to get the difference in minutes between now()
and time_A
?
I have a table in PostgreSQL:
CREATE TABLE tableA
(
time_A time with timezone
);
How to get the difference in minutes between now()
and time_A
?
The type timetz
is broken by the design and its use is discouraged:
To factor in the time zone, use timestamptz
instead. But be aware that the actual time zone is not stored explicitly:
Details for timestamp / time zone handling in Postgres:
Assuming time
instead.
SELECT EXTRACT('epoch' FROM now()::time - time_a) / 60 AS minutes
FROM tablea;
now()
returns timestamptz
, which is adjusted for the current time zone automatically. You get the local time when casting to time
. See:
Subtracting two times yields an interval
(which can be negative, of course).
Apply EXTRACT('epoch' FROM ...)
to get the number of seconds in that interval. Divide by 60. Integer division truncates fractions.