Last active
February 15, 2018 20:12
-
-
Save drydevelopment/ebc0bb3235956438efb976bb30446f69 to your computer and use it in GitHub Desktop.
PostgreSQL function to output an ISO 8601 formatted timestamp with time zone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Tested on PostgreSQL 9.6 */ | |
CREATE OR REPLACE FUNCTION iso_8601_timestamp(datetime TIMESTAMP WITH TIME ZONE) | |
RETURNS VARCHAR AS $$ | |
BEGIN | |
RETURN to_char(datetime::TIMESTAMPTZ AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"'); | |
END; | |
$$ | |
LANGUAGE PLPGSQL; | |
/* Example Usage */ | |
SELECT iso_8601_timestamp(now()); | |
-- 2018-02-15T20:07:12Z | |
SELECT iso_8601_timestamp('2018-02-15'); | |
-- 2018-02-15T05:00:00Z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment