2017-09-15 164 views
1

我看到這個問題問了很多,但沒有看到任何實際的答案(大多數解決方案都針對美國時區)。獲取DST開始日期和結束日期的任何時區

給定一年和一個時區,SQL Server中是否有辦法獲取夏令時的開始和結束日期?

總之,我想看看是否有人想出了一個解決方案,下面的兩個過程/函數:

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

對於那些不做DST的地點,像亞利桑那州的整個州呢? – scsimon

回答

2

我猜想,這是不是真的有可能得到確切的日子。有國家和地區的差異,有些地方根本不遵守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

相關問題