From eb174670fef2caff8dd6f2820d860f17e9d608b8 Mon Sep 17 00:00:00 2001
From: Tyler Lemburg <trlemburg@gmail.com>
Date: Wed, 10 Aug 2016 16:08:54 -0500
Subject: [PATCH] DST Fixes

---
 routes/resources.rb | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/routes/resources.rb b/routes/resources.rb
index 798bc7f..5e3b7da 100644
--- a/routes/resources.rb
+++ b/routes/resources.rb
@@ -227,14 +227,16 @@ post '/:service_space_url_name/resources/:resource_id/reserve/?' do
 		if params[:recurring_type] == 'weekly' || params[:recurring_type] == 'biweeekly'
 			inc = 7.days
 			inc = 14.days if params[:recurring_type] == 'biweeekly'
-			new_start = start_time
+			new_start = start_time.dup
 			while (new_start = new_start + inc) <= recurs_until_date + 1.day
-				starts << new_start
+				# reset in case we moved past DST change
+				new_start = (new_start + 1.hour).midnight.in_time_zone
+				starts << new_start + start_time.minutes_after_midnight.minutes
 			end
-		elsif %w(1st 2nd 3rd 4th).include?(params[:recurring_type])
+		elsif %w(first second third fourth).include?(params[:recurring_type])
 			day_of_week = start_time.wday
 
-			new_start = start_time
+			new_start = start_time.dup
 			while (new_start <= recurs_until_date + 1.day)
 				# calculate when this is next month
 				year = new_start.year
@@ -246,7 +248,16 @@ post '/:service_space_url_name/resources/:resource_id/reserve/?' do
 				end
 
 				# now add weeks
-				start_day += (params[:recurring_type][0].to_i - 1).weeks
+				weeks = {
+					"first" => 1,
+					"second" => 2,
+					"third" => 3,
+					"fourth" => 4
+				}
+
+				start_day += (weeks[params[:recurring_type]] - 1).weeks
+				# reset this to midnight in case of DST change
+				start_day = (start_day + 1.hour).midnight.in_time_zone
 				# and set the start time
 				start_day += start_time.hour.hours + start_time.min.minutes
 
@@ -257,13 +268,14 @@ post '/:service_space_url_name/resources/:resource_id/reserve/?' do
 			end
 		elsif params[:recurring_type] == 'last'
 			day_of_week = start_time.wday
-			new_start = start_time
+			new_start = start_time.dup
 			while (new_start <= recurs_until_date + 1.day)
 				# calculate when this is next month
 				year = new_start.year
 				month = new_start.month + 1
 				month = 1 and year += 1 if month == 13
 				start_day = Time.new(year, month, 1).midnight.in_time_zone
+
 				while (start_day.wday != day_of_week)
 					start_day = start_day + 1.day
 				end
@@ -272,6 +284,9 @@ post '/:service_space_url_name/resources/:resource_id/reserve/?' do
 				while (start_day + 1.week).month == month
 					start_day += 1.week
 				end
+
+				# reset this to midnight in case of DST change
+				start_day = (start_day + 1.hour).midnight.in_time_zone
 				# and set the start time
 				start_day += start_time.hour.hours + start_time.min.minutes
 
@@ -281,7 +296,7 @@ post '/:service_space_url_name/resources/:resource_id/reserve/?' do
 				new_start = start_day
 			end
 		elsif params[:recurring_type] == 'day'
-			new_start = start_time
+			new_start = start_time.dup
 			while (new_start <= recurs_until_date + 1.day)
 				# calculate when this is next month
 				year = new_start.year
-- 
GitLab