我使用datetime在數據庫中將用戶保存的日期保存爲UTC_TIMESTAMP()
。
例如:CreatedDate datetime,
如何在MySQL中以正確的方式處理存儲的UTC日期轉換(夏令時)?
我知道bellow會返回正確的結果。
select convert_tz(utc_timestamp(),'+00:00',@@session.time_zone);
我懷疑是舊數據是需要檢查涉及在日光節約與否。之後做適當的計算這是我的主要疑問。是否需要。請解釋清楚嗎?
實施例:
select case when (month(utc_timestamp())<6 and month(CreatedDate)<6) or
(month(utc_timestamp())>6 and month(CreatedDate)>6))
then convert_tz(CreatedDate ,'+00:00',@@session.time_zone)
when (month(utc_timestamp())<6 and month(CreatedDate)>6) then
convert_tz(CreatedDate,'+00:00',@@session.time_zone)
-- require to decrease the @@session.time_zone with 1 hour
else convert_tz(CreatedDate,'+00:00',@@session.time_zone)
-- require to increase the @@session.time_zone with 1 hour
end as CreatedDate
from tbluser;
爲了報告目的,我使用@@session.time_zone
否則我會給CreatedDate(無需轉換)到前端顯影劑。但是我仍然懷疑是否還需要檢查夏令時並轉換爲舊數據?
上述查詢有錯誤,但我要求的邏輯。