Skip to content

Commit

Permalink
GUACAMOLE-1020: Move time conversion to shared function.
Browse files Browse the repository at this point in the history
  • Loading branch information
necouchman committed Sep 8, 2024
1 parent fd53f1e commit 9d4142b
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,27 @@ angular.module('guacRestrict').controller('timeRestrictionFieldController', ['$s

};

/**
* Since new Time fields in HTML get a default year of 1970, we need to
* merge the hours and minutes from the time field into the current Date,
* primarily so that Daylight Savings Time offsets are correct.
*
* @param {Date} justTime
* The Date object produced by an HTML field that contains the hours
* and minutes we need.
*
* @returns {Date}
* The Date object that merges the current calendar date with the
* hours and minutes from the HTML field.
*/
const timeToCurrentDate = function timeToCurrentDate(justTime) {
let dateAndTime = new Date();
dateAndTime.setHours(justTime.getHours());
dateAndTime.setMinutes(justTime.getMinute());

return dateAndTime;
};

/**
* Parse the restrictions in the field into a string that can be stored
* in an underlying module.
Expand Down Expand Up @@ -214,21 +235,11 @@ angular.module('guacRestrict').controller('timeRestrictionFieldController', ['$s
// When these fields first gets a value, the default year is 1970
// In order to avoid issues with Daylight Savings Time, we have to
// work around this.
if (restrictions[i].startTime instanceof Date && restrictions[i].startTime.getFullYear() === 1970) {
let startHour = restrictions[i].startTime.getHours();
let startMin = restrictions[i].startTime.getMinutes();
restrictions[i].startTime = new Date();
restrictions[i].startTime.setHours(startHour);
restrictions[i].startTime.setMinutes(startMin);
}
if (restrictions[i].startTime instanceof Date && restrictions[i].startTime.getFullYear() === 1970)
restrictions[i].startTime = timeToCurrentDate(restrictions[i].startTime);

if (restrictions[i].endTime instanceof Date && restrictions[i].endTime.getFullYear() === 1970) {
let endHour = restrictions[i].endTime.getHours();
let endMin = restrictions[i].endTime.getMinutes();
restrictions[i].endTime = new Date();
restrictions[i].endTime.setHours(endHour);
restrictions[i].endTime.setMinutes(endMin);
}
if (restrictions[i].endTime instanceof Date && restrictions[i].endTime.getFullYear() === 1970)
restrictions[i].endTime = timeToCurrentDate(restrictions[i].endTime);

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

0 comments on commit 9d4142b

Please sign in to comment.