I am working with dates relative to the database server.
In Oracle, to get the full timezone offset you would use something like the following:
select tz_offset(SESSIONTIMEZONE) from dual;
or
select tz_offset(DBTIMEZONE) from dual;
These would return, in my case (importantly it includes the sign)
-04:00
I was wondering what functionality exists in Postgres to get a result of the exact same format as the Oracle version above? I need to know the timezone offset of the server inclusive of the sign indicating it being behind or ahead of GMT/UTC.
For the time zone you can:
or the equivalent:
but this can be in any format accepted by the server, so it may return
UTC,08:00,Australia/Victoria, or similar.Frustratingly, there appears to be no built-in function to report the time offset from UTC the client is using in hours and minutes, which seems kind of insane to me. You can get the offset by comparing the current time in UTC to the current time locally:
… but IMO it’s cleaner to extract the tz offset in seconds from the
current_timestampand convert to an interval:That’ll match the desired result except that it doesn’t produce a leading zero, so
-05:00is just-5:00. Annoyingly it seems to be impossible to getto_charto produce a leading zero for hours, leaving me with the following ugly manual formatting:Credit to Glenn for
timezone_hourandtimezone_minuteinstead of the hack I used earlier withextract(timezone from current_timestamp) * INTERVAL '1' second)and a CTE.If you don’t need the leading zero you can instead use:
See also: