首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Netezza利用夏令时将UTC/GMT转换为中环

Netezza利用夏令时将UTC/GMT转换为中环
EN

Stack Overflow用户
提问于 2021-03-30 08:42:09
回答 2查看 797关注 0票数 0

我正在一个Netezza数据库中工作,该数据库将时间存储为GMT (至少我们的数据工程师告诉我了)。我需要能够将其转换为中央标准时间(CST),但需要考虑夏时制。我发现我需要这样的东西:

代码语言:javascript
运行
AI代码解释
复制
SELECT CURRENT_TIMESTAMP, CURRENT_TIMESTAMP AT TIME ZONE 'CST' AT TIME ZONE 'GMT'

然而,当我运行这个选择(记住,今天是2021年3月30日- CST应该只有5个小时不同的GTM),我得到一个6小时的差异.我查找了一个引用,以查看Netezza中有哪些时区可用,我看到了一个"CDT“,它是5个小时,它适用于5小时的差异,但这意味着在我的查询中,我需要在DST每次切换时更改它,或者执行某种精细的case语句,以了解根据一年的日期/时间使用哪个时区。

是否有一种简单的自动化方法可以将GTM时间转换为计算夏时制的中央标准时?非常感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-30 10:49:15

这个问题可以用两种方式之一来解释。在这两种情况下,解决方案都是根据时间戳是否在3月2日上午2点到11月1日星期日凌晨2点之间确定要转换的时区(对于美国中央时区)。

表中的时间戳,需要根据当前时间(在运行查询时)转换为to (在运行查询时)

  • --这意味着,如果在2月运行相同的查询,结果将与现在运行的
  • 不同,这也将根据设置了
  • 系统的时区而有所不同。

例如

代码语言:javascript
运行
AI代码解释
复制
select 
  t as original, 
  -- extract year from current date and 2nd Sunday of March
  -- use last_day to make sure we account for March 1 being a Sunday
  (next_day(next_day(
           last_day((date_part('years', current_date) || '-02-01'):: date), 
        'sun'), 
      'sun')|| ' 02:00:00'):: timestamp as dstart, 
  -- extract year from current date and 1st Sunday of Nov
  -- use last_day to make sure we account for Nov 1 being a Sunday
  (next_day(last_day(
        (date_part('years', current_date) || '-10-01')::date), 
      'sun')|| ' 02:00:00'):: timestamp as dend, 
  case when current_timestamp between dstart 
  and dend then 'CDT' else 'CST' end as tz, 
  t at time zone tz as converted 
from 
  tdata;

将产生

代码语言:javascript
运行
AI代码解释
复制
      ORIGINAL       |       DSTART        |        DEND         | TZ  |       CONVERTED
---------------------+---------------------+---------------------+-----+------------------------
 2021-01-01 17:00:00 | 2021-03-14 02:00:00 | 2021-11-07 02:00:00 | CDT | 2021-01-01 12:00:00-05
 2021-04-01 17:00:00 | 2021-03-14 02:00:00 | 2021-11-07 02:00:00 | CDT | 2021-04-01 12:00:00-05
 2020-04-01 17:00:00 | 2021-03-14 02:00:00 | 2021-11-07 02:00:00 | CDT | 2020-04-01 12:00:00-05
 2020-12-01 17:00:00 | 2021-03-14 02:00:00 | 2021-11-07 02:00:00 | CDT | 2020-12-01 12:00:00-05
(4 rows)

  1. 表中的时间戳需要转换为CSTCDT,具体取决于在时间戳中定义的各个year中夏时制的开始/结束时间。这里是更多的deterministic

代码语言:javascript
运行
AI代码解释
复制
select 
  t as original, 
  -- extract year from this timestamp and 2nd Sunday of March
  -- use last_day to make sure we account for March 1 being a Sunday
  (next_day(next_day(
           last_day((date_part('years', t) || '-02-01'):: date), 'sun'), 
      'sun')|| ' 02:00:00'):: timestamp as dstart, 
  -- extract year from this timestamp and 1st Sunday of Nov
  -- use last_day to make sure we account for Nov 1 being a Sunday
  (next_day(last_day((date_part('years', t) || '-10-01')::date), 
      'sun')|| ' 02:00:00'):: timestamp as dend, 
  case when current_timestamp between dstart 
  and dend then 'CDT' else 'CST' end as tz, 
  t at time zone tz as converted 
from 
  tdata;

这将产生(tdata是一个示例表w/ 4时间戳)

代码语言:javascript
运行
AI代码解释
复制
      ORIGINAL       |       DSTART        |        DEND         | TZ  |       CONVERTED
---------------------+---------------------+---------------------+-----+------------------------
 2021-01-01 17:00:00 | 2021-03-14 02:00:00 | 2021-11-07 02:00:00 | CST | 2021-01-01 11:00:00-06
 2021-04-01 17:00:00 | 2021-03-14 02:00:00 | 2021-11-07 02:00:00 | CDT | 2021-04-01 12:00:00-05
 2020-04-01 17:00:00 | 2020-03-08 02:00:00 | 2020-11-01 02:00:00 | CDT | 2020-04-01 12:00:00-05
 2020-12-01 17:00:00 | 2020-03-08 02:00:00 | 2020-11-01 02:00:00 | CST | 2020-12-01 11:00:00-06
(4 rows)
票数 1
EN

Stack Overflow用户

发布于 2021-04-07 12:05:06

代码语言:javascript
运行
AI代码解释
复制
system.admin(admin)=> select '2021-04-07 11:00:00' as gmt, timezone('2021-04-07 11:00:00' , 'GMT', 'America/New_York') as eastern, timezone('2021-04-07 11:00:00', 'GMT', 'America/Chicago') as central, timezone('2021-04-07 11:00:00', 'GMT', 'America/Los_Angeles') as pacific;
         gmt         |       eastern       |       central       |       pacific
---------------------+---------------------+---------------------+---------------------
 2021-04-07 11:00:00 | 2021-04-07 07:00:00 | 2021-04-07 06:00:00 | 2021-04-07 04:00:00
(1 row)

system.admin(admin)=> select '2021-03-07 11:00:00' as gmt, timezone('2021-03-07 11:00:00' , 'GMT', 'America/New_York') as eastern, timezone('2021-03-07 11:00:00', 'GMT', 'America/Chicago') as central, timezone('2021-03-07 11:00:00', 'GMT', 'America/Los_Angeles') as pacific;
         gmt         |       eastern       |       central       |       pacific
---------------------+---------------------+---------------------+---------------------
 2021-03-07 11:00:00 | 2021-03-07 06:00:00 | 2021-03-07 05:00:00 | 2021-03-07 03:00:00
(1 row)

而不是CDT和CST,如果我们使用‘美国/芝加哥’,如上面所示,它负责日光节约。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66875021

复制
相关文章

相似问题

领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文