Convert a UTC timezone in postgresql to EST (local time)

后端 未结 6 1028
野趣味
野趣味 2020-12-24 11:00

I am new to PostgreSQL and I was wondering if there is a direct way to just convert the timestamp values in a table to a different timezone using a function. In my case it i

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 11:50

    Here in London, we are currently 1 hour ahead of UTC. So - if I take your timezone without timestamp and say it is in UTC I will get it printed for my local timezone.

    richardh=> SELECT ((timestamp '2015-10-24 16:38:46') AT TIME ZONE 'UTC');
            timezone        
    ------------------------
     2015-10-24 17:38:46+01
    (1 row)
    

    But you want "EST" which seems to be somewhere in the Americas, judging by the value returned. You can wrap the expression in a little SQL function if you wanted to.

    richardh=> SELECT ((timestamp '2015-10-24 16:38:46') AT TIME ZONE 'UTC') AT TIME ZONE 'EST';
          timezone       
    ---------------------
     2015-10-24 11:38:46
    (1 row)
    

    Edit: how to do it in a query

    SELECT ((stored_timestamp AT TIME ZONE 'UTC') AT TIME ZONE 'EST') AS local_timestamp
    FROM my_table;
    

    You will probably want to get an introductory book on SQL if this sort of thing is causing you problems.

提交回复
热议问题