Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert rfc3339 string to timestamp with timezone

Is there any way to properly convert a rfc3339 string in a character varying column to a timestamp with timezone?

Input: "2015-01-28T17:41:52Z"
Expected output: "2015-01-28 17:41:52+00"
Current output: "2015-01-28 17:41:52+01"

I tried the following

/* begin dummy data */
WITH messages as (
    select 123 as id, '2015-01-28T17:41:52Z'::text as received
)
/* end dummy data */
SELECT id, received, (to_timestamp(received, 'YYYY-MM-DDThh24:MI:SS')) as d
FROM messages

Adding TZ or tz is not possible for input data and OF is available in 9.4 but not in 9.3.

like image 639
Simon Warta Avatar asked Mar 17 '23 05:03

Simon Warta


1 Answers

RFC3339 is just a profile of ISO 8601.

PostregreSQL accepts any valid ISO 8601 input as date/timestamp/timestamp with time zone. Try casting, like:

SELECT '2015-01-28T17:41:52Z'::timestamptz

Note: however, your output (in your client) will always be in your current time zone.

like image 123
pozs Avatar answered Mar 20 '23 15:03

pozs