Detect if a date is in Daylight saving time in MySql

孤街醉人 提交于 2019-12-02 03:01:08

Timezones are not stored in DATETIME values. Interestingly, they are for TIMESTAMPs.

Given a stored date, you can posthumously figure out if DST was "on" at that time based on the local rules. Since you can't change the dates, I would guess that you can't add a column to store the timezone...

Make a stored procedure that contains the rules and converts a given date to GMT.

Note that only the people that live around Greenwich want their times displayed in GMT. :)

Good luck.

You're in UK. So, your TZ should be CET or CEST (Central Europe Summer Time). You can get the info out this way:

mysql> SELECT @@system_time_zone;
+--------------------+
| @@system_time_zone |
+--------------------+
| CEST               |
+--------------------+

I use this in many of my Stored Procedures. Note: I need both forms of TZ info, whether I need to compute offsets with or without DST being applied.

CASE @@system_time_zone
  WHEN 'CET'  THEN SET l_tz = 'Europe/Paris';   SET l_tzOff = '+1:00';
  WHEN 'CEST' THEN SET l_tz = 'Europe/Paris';   SET l_tzOff = '+1:00';
  WHEN 'SGT'  THEN SET l_tz = 'Asia/Singapore'; SET l_tzOff = '+8:00';
  ELSE             SET l_tz = 'Europe/Paris';   SET l_tzOff = '+1:00';
END CASE;

You can get some inspiration from this.

MartiniB

found an ugly complicated way, @DT is input Date&Time

Set @DT=20150329020304; # -> 0

Set @DT=20150329030304; # -> 1(-0.0511)

Set @DT=20150329040304; # -> 1(-1)

Select 0!=(UNIX_TIMESTAMP(@DT)-   UNIX_TIMESTAMP(Concat(Year(@DT),'0101',Time_Format(@DT,'%H%i%s')))-DateDiff(@DT,Concat(Year(@DT),'0101',Time_Format(@DT,'%H%i%s')))*86400)/3600 as DlsIsOn
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!