Skip to content
Advertisement

Comparing TIME WITH TIME ZONE returns unexpected result

Why does this query return false? Is it because of the 22:51:13.202248 +01:00 format?

Advertisement

Answer

… returns a value of time with time zone (timetz):

Then you compare it to time [without time zone]. Don’t do this. The time value is coerced to timetz in the process and a time offset is appended according to the current timezone setting. Meaning, your expression will evaluate differently with different settings. What’s more, DST rules are not applied properly. You want none of this! See:

db<>fiddle here

More generally, don’t use time with time zone (timetz) at all. The type is broken by design and officially discouraged in Postgres. See:

Use instead:

The right operand can be an untyped literal now, it will be coerced to time as it should.

BETWEEN is often the wrong tool for times and timestamps. See:

But it would seem that >= and <= are more appropriate for opening hours? Then BETWEEN fits the use case and makes it a bit simpler:

Related:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement