1

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?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
user3532941
  • 17
  • 1
  • 4

1 Answers1

20

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:

Solution

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.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228