转换时区时注意夏令时

Taking care of daylight saving time when converting timezone

我有一个 Redshift 数据 table,其中所有时间值都存储在 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 地址、美国领土和其他边缘情况。此外,一些邮政编码可能跨越多个时区。

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

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