简体   繁体   中英

mysql daylight saving - determine if true or false

im looking to determine if a date is in daylight saving. This is used to determine the number of hours between two dates as this will effect the cost - its for a care hire. I have tried to strip the code down as much as possible. I am struggling with the syntax a little (is_daylight function does not exist) but hopefully have managed to convey what im trying to do. Any help much appreciated.

SELECT 

  -- lost an hour on pickup date so add an hour to the date
  WHEN 
   is_daylight(pickup_date)=1 AND is_daylight(dropoff_date)=0
  THEN 
    unix_timestamp(dropoff_date) - unix_timestamp(pickup_date)+3600 / 86400 as num_hours

  -- not date time
  ELSE
    unix_timestamp(dropoff_date) - unix_timestamp(pickup_date) / 86400 as num_hours 

The manual states:

UNIX_TIMESTAMP() assumes that its argument is a datetime value in the current time zone.

So if your server is configured properly, ie using the correct time zone, then the unix time stamp should already incorporate any dst modifications which might be required. So first of all, make sure that there is anything to fix, and if there is, check whether time zone support is configured properly.

Note that the conversion is lossy, as immediately around a switchover, there might be a local time corresponding to two UTC times, one before and one after the switchover. For this reason, it might be better to store unix timestamps at all times, and only convert to local time for user output.

If you want to know whether DST is active at a given date, you could probably use CONVERT_TZ . Convert from local time to the fixed offset time that corresponds to local time without DST. If the result stays the same, DST is not active. If it changes by one hour, then DST is active.

Example:

SELECT CASE
 WHEN            '2013-07-24 15:00' =
      CONVERT_TZ('2013-07-24 15:00', 'EST', 'America/New_York')
 THEN 'no DST' ELSE 'DST' END AS isDST

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM