From 112b174e5fad5417a0083eca35c75e01751c4858 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 31 Aug 2017 17:32:04 +0200 Subject: [PATCH] [bug] invalid time for reccurent events after Daylight Saving Time change --- app/helpers/application_helper.rb | 12 ++++++++++++ app/models/event.rb | 3 +++ 2 files changed, 15 insertions(+) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ae40ef803..445acf540 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -80,6 +80,18 @@ module ApplicationHelper nil end + ## + # Apply a correction for a future DateTime due to change in Daylight Saving Time (DST) period + # @param datetime {DateTime} + # Inspired by https://stackoverflow.com/a/12065605 + ## + def dst_correction(datetime) + datetime = datetime.in_time_zone(Time.zone.tzinfo.name) + datetime = datetime - 1.hour if datetime.dst? && !Time.now.dst? + datetime = datetime + 1.hour if Time.now.dst? && !datetime.dst? + datetime + end + private ## inspired by gems/actionview-4.2.5/lib/action_view/helpers/translation_helper.rb diff --git a/app/models/event.rb b/app/models/event.rb index 4c9e28395..7d0678171 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -1,5 +1,6 @@ class Event < ActiveRecord::Base include NotifyWith::NotificationAttachedObject + include ApplicationHelper has_one :event_image, as: :viewable, dependent: :destroy accepts_nested_attributes_for :event_image, allow_destroy: true @@ -87,8 +88,10 @@ class Event < ActiveRecord::Base r.events.each do |date| days_diff = availability.end_at.day - availability.start_at.day start_at = DateTime.new(date.year, date.month, date.day, availability.start_at.hour, availability.start_at.min, availability.start_at.sec, availability.start_at.zone) + start_at = dst_correction(start_at) end_date = date + days_diff.days end_at = DateTime.new(end_date.year, end_date.month, end_date.day, availability.end_at.hour, availability.end_at.min, availability.end_at.sec, availability.end_at.zone) + end_at = dst_correction(end_at) if event_image ei = EventImage.new(attachment: event_image.attachment) end