Skip to content

Commit f96a42a

Browse files
committed
GUACAMOLE-1020: Move time conversion to shared function.
1 parent 4224b7b commit f96a42a

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

extensions/guacamole-auth-restrict/src/main/resources/controllers/timeRestrictionFieldController.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,27 @@ angular.module('guacRestrict').controller('timeRestrictionFieldController', ['$s
176176

177177
};
178178

179+
/**
180+
* Since new Time fields in HTML get a default year of 1970, we need to
181+
* merge the hours and minutes from the time field into the current Date,
182+
* primarily so that Daylight Savings Time offsets are correct.
183+
*
184+
* @param {Date} justTime
185+
* The Date object produced by an HTML field that contains the hours
186+
* and minutes we need.
187+
*
188+
* @returns {Date}
189+
* The Date object that merges the current calendar date with the
190+
* hours and minutes from the HTML field.
191+
*/
192+
const timeToCurrentDate = function timeToCurrentDate(justTime) {
193+
let dateAndTime = new Date();
194+
dateAndTime.setHours(justTime.getHours());
195+
dateAndTime.setMinutes(justTime.getMinute());
196+
197+
return dateAndTime;
198+
};
199+
179200
/**
180201
* Parse the restrictions in the field into a string that can be stored
181202
* in an underlying module.
@@ -214,21 +235,11 @@ angular.module('guacRestrict').controller('timeRestrictionFieldController', ['$s
214235
// When these fields first gets a value, the default year is 1970
215236
// In order to avoid issues with Daylight Savings Time, we have to
216237
// work around this.
217-
if (restrictions[i].startTime instanceof Date && restrictions[i].startTime.getFullYear() === 1970) {
218-
let startHour = restrictions[i].startTime.getHours();
219-
let startMin = restrictions[i].startTime.getMinutes();
220-
restrictions[i].startTime = new Date();
221-
restrictions[i].startTime.setHours(startHour);
222-
restrictions[i].startTime.setMinutes(startMin);
223-
}
238+
if (restrictions[i].startTime instanceof Date && restrictions[i].startTime.getFullYear() === 1970)
239+
restrictions[i].startTime = timeToCurrentDate(restrictions[i].startTime);
224240

225-
if (restrictions[i].endTime instanceof Date && restrictions[i].endTime.getFullYear() === 1970) {
226-
let endHour = restrictions[i].endTime.getHours();
227-
let endMin = restrictions[i].endTime.getMinutes();
228-
restrictions[i].endTime = new Date();
229-
restrictions[i].endTime.setHours(endHour);
230-
restrictions[i].endTime.setMinutes(endMin);
231-
}
241+
if (restrictions[i].endTime instanceof Date && restrictions[i].endTime.getFullYear() === 1970)
242+
restrictions[i].endTime = timeToCurrentDate(restrictions[i].endTime);
232243

233244
// Process the start day, factoring in wrapping for local time to
234245
// UTC adjustments.

0 commit comments

Comments
 (0)