datetime - 转换时区时注意夏令时

标签 datetime timezone amazon-redshift dst date-conversion

我有一个 Redshift 数据表,其中所有时间值都存储在 CST 中,我根据邮政编码(位置)将时间值转换为相应的时区。

当我这样做时,我知道所有时间值都是标准时间,因此我的函数用法是

CASE WHEN **** convert_timezone('CST', 'EST', time_column)  
     WHEN **** convert_timezone('CST', 'MST', time_column)
....
END

一旦我们进入夏令时,这可能不适用。 我该如何处理这个问题,以免在 2018 年 3 月及以后再次修改 SQL 查询?

最佳答案

不要使用时区缩写。有点模棱两可,只能指代时区的一个方面。相反,使用完整的 IANA time zone identifier ,例如美国中部时间的 America/Chicago

这在 the Redshift docs 中有很好的解释:

Using a Time Zone Name

If you specify a time zone using a time zone name, CONVERT_TIMEZONE automatically adjusts for Daylight Saving Time (DST), or any other local seasonal protocol, such as Summer Time, Standard Time, or Winter Time, that is in force for that time zone during the date and time specified by 'timestamp'. For example, 'Europe/London' represents UTC in the winter and UTC+1 in the summer.

就您问题的“...基于邮政编码”部分而言,了解并非每个邮政编码都是基于地区的。还有技术任务、海外 APO/FPO 地址、美国领土和其他边缘案例。此外,一些邮政编码可能跨越多个时区。

如果可能,更好的方法是:

  • 获取纬度/经度坐标的近似值 - 根据您的源数据使用各种技术。例如,地理编码 API 可以获取街道地址并给出纬度/经度。
  • 然后使用其中一种技术确定该位置的时区标识符 listed here .

关于datetime - 转换时区时注意夏令时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47664819/

相关文章:

带有没有小时的日期的 php DateTime

cocoa - NSDatePicker 正在更改系统时区的时间。我想停止更改日期选择器的日期值

java - GMT ID 如何在 Java TimeZone 中工作?

sql - 向 Redshift 表添加 NOT NULL 列

amazon-web-services - 使用 Redshift Spectrum 的步骤是什么?

python日期间隔交集

python - 使用 Pandas 查找包含周末的日期范围

php - 通过 > 和 < 运算符比较日期

amazon-web-services - 如何在 AWS Redshift 集群中查找数据库列表

c# - 计算日期之间的分钟差