我的时区是太平洋时间。我想根据地区将其转换为当地时间。下面是示例
time region
2017-05-23 14:00:00 Central
2017-05-23 14:00:00 Eastern
2017-05-23 14:00:00 Mountain
我在寻找什么
time region time_local
2017-05-23 14:00:00 Central 2017-05-23 16:00:00
2017-05-23 14:00:00 Eastern 2017-05-23 17:00:00
2017-05-23 14:05:00 Mountain 2017-05-23 15:05:00
答案 0 :(得分:2)
您可以加入时区系统视图以调整时间戳:
select
time,
region,
time - tz1.utc_offset + tz2.utc_offset
from Example ex
JOIN pg_timezone_names tz1
on tz1.name = 'US/Pacific'
JOIN pg_timezone_names tz2
on tz2.name = 'US/' || ex.region
答案 1 :(得分:1)
假设您的“时间”列类型是正确的“带时区的时间戳”(也简称为“时间戳”):
create table times (time timestamp with time zone not null, region text not null);
set timezone='US/Pacific';
insert into times (time, region) values
('2017-05-23 14:00:00','US/Central'),
('2017-05-23 14:00:00','US/Eastern'),
('2017-05-23 14:00:00','US/Mountain');
select *, time at time zone region as time_local from times;
time | region | time_local
------------------------+-------------+---------------------
2017-05-23 14:00:00-07 | US/Central | 2017-05-23 16:00:00
2017-05-23 14:00:00-07 | US/Eastern | 2017-05-23 17:00:00
2017-05-23 14:00:00-07 | US/Mountain | 2017-05-23 15:00:00
如果您的时间栏是普通的时间戳,那么您需要先考虑对其进行更改,然后再疯狂。时间戳类型根本不表示时间戳,它表示某个地方将显示什么时钟,这取决于您身在何处,它在哪一天,在哪个国家/地区以及您的数据库客户端设置和当前环境以及之后的时间而不同。政客将来会改变时钟。不要走这条路。
答案 2 :(得分:0)
您可以使用以下转换:
SET TIMEZONE TO 'US/Central';
SELECT now()::timestamp;
current_time
2018-07-25T14:01:50.608042Z
SET TIMEZONE TO 'US/Eastern';
SELECT now()::timestamp;
current_time
2018-07-25T15:01:50.608042Z
SET TIMEZONE TO 'US/Mountain';
SELECT now()::timestamp;
current_time
2018-07-25T13:01:50.608042Z
或者使用:
SET TIMEZONE TO 'US/Central';
SELECT concat(current_date,' ',localtime) as current_time;
current_time
2018-07-25 14:10:57.962193
SET TIMEZONE TO 'US/Eastern';
SELECT concat(current_date,' ',localtime) as current_time;
current_time
2018-07-25 15:10:57.962193
SET TIMEZONE TO 'US/Mountain';
SELECT concat(current_date,' ',localtime) as current_time;
current_time
2018-07-25 13:10:57.962193