获取DST开始日期和结束日期的任何时区

问题描述:

我看到这个问题问了很多,但没有看到任何实际的答案(大多数解决方案都针对美国时区)。获取DST开始日期和结束日期的任何时区

给定一年和一个时区,SQL Server中是否有办法获取夏令时的开始和结束日期?

总之,我想看看是否有人想出了一个解决方案,下面的两个过程/函数:

datetime2 getDSTStartDate(@year int, @timeZone varchar) 
datetime2 getDSTEndDate(@year int, @timeZone varchar) 
+0

对于那些不做DST的地点,像亚利桑那州的整个州呢? – scsimon

我猜想,这是不是真的有可能得到确切的日子。有国家和地区的差异,有些地方根本不遵守DST。它在规定的日期和时间通常为,但通常在年内可以更改。那么你的日期时间计算并不重要。

https://www.timeanddate.com/time/dst/2017.html

请参阅页第二段:

注意,这个清单可能不会被最终确认 - 国家,地区和 状态有时会被宣布几天或 周调整在时间变化之前。

仅仅这句话就表明内部数据库函数可能很容易变得过时以至于毫无意义。

你可以做过去的日期,因为它们已经完成了,但是如下图所示,未来日期虽然现在有些设定,但可能会改变,并且不可能准确预测。

** Country Counts For Last 3 Years ** 
--------------------------------------------------------------- 
|       **2017**       | 
--------------------------------------------------------------- 
|    DST Observance     |Count|Example| 
--------------------------------------------------------------- 
|No DST at all         | 172 | China | 
|At least one location observes DST    | 77 | USA | 
|All locations observe DST some part of the year| 67 | Iran | 
|Many locations observe DST part of the year | 9 | USA | 
|At least one location observes DST all year | 1 |  | 
|All locations observe DST all year    | 1 | Chile | 
--------------------------------------------------------------- 

--------------------------------------------------------------- 
|       **2016**       | 
--------------------------------------------------------------- 
|    DST Observance     |Count|Example| 
--------------------------------------------------------------- 
|No DST at all         | 171 | China | 
|At least one location observes DST    | 77 | USA | 
|All locations observe DST some part of the year| 69 | Iran | 
|Many locations observe DST part of the year | 8 | USA | 
--------------------------------------------------------------- 

--------------------------------------------------------------- 
|       **2015**       | 
--------------------------------------------------------------- 
|    DST Observance     |Count|Example| 
--------------------------------------------------------------- 
|No DST at all         | 168 | China | 
|At least one location observes DST    | 80 | USA | 
|All locations observe DST some part of the year| 70 | Iran | 
|Many locations observe DST part of the year | 9 | USA | 
|At least one location observes DST all year | 1 |  | 
|All locations observe DST all year    | 1 | Chile | 
--------------------------------------------------------------- 

注:由于上述变化莫测的,因为你还不得不以编程方式确定您的用户做了他们的进入,这将是最好只存储日期时间在UTC或其他通用的格式和根据需要消耗它。 SQL可能不是最好的地方。

+0

为了增添乐趣,大图尾部的注释说:'*有些地点有多个DST时段:列出了第一个时段的开始和最后一个时段的结束。 – Shawn