Only function worked for me because you need to set timezone.
To have default value timezone with zone:
create table somedata (
release_date timestamptz DEFAULT NOW()
)
Create function:
CREATE OR REPLACE FUNCTION date_display_tz(param_dt timestamp with time zone)
RETURNS text AS
$$
DECLARE var_result varchar;
BEGIN
PERFORM set_config('timezone', 'UTC', true);
var_result := to_char(param_dt , 'YYYY-MM-DD"T"HH24:MI:SS:MS"Z"');
RETURN var_result;
END;
$$ language plpgsql VOLATILE;
And output:
# SELECT
# localtimestamp, current_timestamp,
# to_char(localtimestamp, 'YYYY-MM-DD"T"HH24:MI:SS:MS"Z"'),
# to_char(current_timestamp, 'YYYY-MM-DD"T"HH24:MI:SS:MS"Z"'),
# date_display_tz(localtimestamp), date_display_tz(current_timestamp);
timestamp | now | to_char | to_char | date_display_tz | date_display_tz
----------------------------+-------------------------------+--------------------------+--------------------------+--------------------------+--------------------------
2017-04-27 23:48:03.802764 | 2017-04-27 21:48:03.802764+00 | 2017-04-27T23:48:03:802Z | 2017-04-27T23:48:03:802Z | 2017-04-27T21:48:03:802Z | 2017-04-27T21:48:03:802Z
(1 row)
Look at this also:
If you want the server to return time zone information respective of
another time zone, I believe you'll need to use SET TIME ZONE.
Otherwise, the server automatically (converts the timestamp) and
returns the time zone of the server.
test=# select (current_timestamp at time zone 'UTC') at time zone 'UTC';
timezone
-------------------------------
2005-04-22 16:26:57.209082+09
(1 row)
test=# set time zone 'UTC';
SET
test=# select (current_timestamp at time zone 'UTC') at time zone 'UTC';
timezone
-------------------------------
2005-04-22 07:27:55.841596+00
(1 row)
test=# select (current_timestamp at time zone 'UTC');
timezone
----------------------------
2005-04-22 07:28:48.888154
(1 row)
test=# select (current_timestamp at time zone 'UTC')::timestamptz;
timezone
-------------------------------
2005-04-22 07:38:19.979511+00
(1 row)