PostgreSQL:将MongoDB日期格式转换为timestamptz
我参考了psql并提出了这个查询 选择时间戳“2016年8月30日星期二04:07:13 GMT+0530 IST”,“下周一DD YYYY HH24:MI:SS” 这个日期时间字符串Tue Aug 30 2016 04:07:13 GMT+0530 IST是我从MongoDB printjsoncreatedAt获得的 上面的postresql似乎不能正确地用于所有偏移 我试过这个PostgreSQL:将MongoDB日期格式转换为timestamptz,sql,postgresql,timestamp-with-timezone,Sql,Postgresql,Timestamp With Timezone,我参考了psql并提出了这个查询 选择时间戳“2016年8月30日星期二04:07:13 GMT+0530 IST”,“下周一DD YYYY HH24:MI:SS” 这个日期时间字符串Tue Aug 30 2016 04:07:13 GMT+0530 IST是我从MongoDB printjsoncreatedAt获得的 上面的postresql似乎不能正确地用于所有偏移 我试过这个 select to_timestamp('Tue Aug 30 2016 04:07:13 GMT+0530 (
select to_timestamp('Tue Aug 30 2016 04:07:13 GMT+0530 (IST)', 'Dy MON DD YYYY HH24:MI:SS "GMT"OF "(IST)"');
但是我得到了这个错误``错误:to_date中不支持格式模式的TZ/TZ/`
如何从该字符串转换为psql timestamptz格式(2016年8月30日星期二04:07:13 GMT+0530 IST?它看起来像一个丑陋的轮子,需要为每个无法识别的偏移量进行这样的替换,但它可以工作。对于您的样本,请将“GMT+0530 IST”替换为“GMT+05:30”,然后将拾取该样本:
t=# select replace('Tue Aug 30 2016 04:07:13 GMT+0530 (IST)','GMT+0530 (IST)','GMT+05:30')::timestamptz;
replace
------------------------
2016-08-30 09:37:13+00
(1 row)
t=# select replace('Tue Aug 30 2016 14:07:13 GMT+0530 (IST)','GMT+0530 (IST)','GMT+05:30')::timestamptz;
replace
------------------------
2016-08-30 19:37:13+00
(1 row)
更新:根据您的时区结果,可能会令人困惑:
t=# set timezone TO 'GMT-5:30'; SET
t=# select replace('Tue Aug 30 2016 14:07:13 GMT+0530 (IST)','GMT+0530 (IST)','GMT+05:30')::timestamptz;
replace
---------------------------
2016-08-31 01:07:13+05:30
(1 row)
要检查其是否正确,请使用:
t=# select replace('Tue Aug 30 2016 14:07:13 GMT+0530 (IST)','GMT+0530 (IST)','GMT+05:30')::timestamptz at time zone 'UTC';
timezone
---------------------
2016-08-30 19:37:13
(1 row)
它是24小时制的吗?我想这个查询正在将我的24小时时钟转换为12小时!!谢谢,沃。当我尝试时,它没有正确转换。它是否也与系统时钟设置有关?我将用几个例子来证明我自己?。。在你看到确切的错误之前,很难说出来。用检查的方法更新了答案。另外,你可以为你的客户设置时区,以避免双重转换Messack@vitaly-t-他在这方面相当活跃