For the time zone you can:
SHOW timezone;
or the equivalent:
SELECT current_setting('TIMEZONE');
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:
SELECT age(current_timestamp AT TIME ZONE 'UTC', current_timestamp)`
... but IMO it's cleaner to extract the tz offset in seconds from the current_timestamp
and convert to an interval:
SELECT to_char(extract(timezone from current_timestamp) * INTERVAL '1' second, 'FMHH24:MM');
That'll match the desired result except that it doesn't produce a leading zero, so -05:00
is just -5:00
. Annoyingly it seems to be impossible to get to_char
to produce a leading zero for hours, leaving me with the following ugly manual formatting:
CREATE OR REPLACE FUNCTION oracle_style_tz() RETURNS text AS $$
SELECT to_char(extract(timezone_hour FROM current_timestamp),'FM00')||':'||
to_char(extract(timezone_minute FROM current_timestamp),'FM00');
$$ LANGUAGE 'SQL' STABLE;
Credit to Glenn for timezone_hour
and timezone_minute
instead of the hack I used earlier with extract(timezone from current_timestamp) * INTERVAL '1' second)
and a CTE.
If you don't need the leading zero you can instead use:
CREATE OR REPLACE FUNCTION oracle_style_tz() RETURNS text AS $$
SELECT to_char(extract(timezone from current_timestamp) * INTERVAL '1' second, 'FMHH24:MM');
$$ LANGUAGE 'SQL' STABLE;
See also: