- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 08:34 AM
how to check which date is closest to my planned start date of change request: 2024-03-26 01:00:00
var ss = new GlideRecord('cmn_schedule_span'); ss.addQuery('schedule', 'sys ids of the maintenance schedules'); ss.orderBy('start_date_time'); ss.query(); // consider i have 2 maitenace schedules while (ss.next()) { var timeZone = "GMT"; var thisSchedule = new GlideSchedule(); thisSchedule.setTimeZone(timeZone); gs.log("addHoliday to Schedule " + ss.name); thisSchedule.addTimeSpan(ss); Start from Today var gd = new GlideDate(); var gdt = new GlideDateTime(); gdt.setNumericValue(gd.getNumericValue()); / GDT (GlideDateTime) // Start Datevar sd = new GlideDateTime(); sd.setValue(gdt.getDate() + " 00:00:00"); / End Date var ed = new GlideDateTime(); ed.setValue(gdt.getDate() + " 23:59:59"); ed.addDaysUTC(730); // Look 2 years ahead (this could be anything) var ssd = new GlideScheduleDateTime(sd); // Start Day ssd.setTimeZone(timeZone); var sed = new GlideScheduleDateTime(ed); // End Day sed.setTimeZone(timeZone); var scheduleMap = thisSchedule.getTimeMap(sd, ed, timeZone); var span = new GlideScheduleDateTimeSpan(ssd, sed); var thisMap = new GlideScheduleTimeMap(); thisMap.addInclude(span); thisMap.buildMap(timeZone); overlaps = scheduleMap.overlapsWith(thisMap, timeZone); overlaps.buildMap(timeZone); while (overlaps.hasNext()) { var overlapSpan = overlaps.next(); gs.log("overlapSpan Dates: " + overlapSpan.getStart() + " - " + overlapSpan.getEnd()); } }
output:
schedule 1: *** Script: overlapSpan Dates: 2024-03-27 01:00:00 - 2024-03-27 23:00:00 *** Script: overlapSpan Dates: 2024-04-03 01:00:00 - 2024-04-03 23:00:00 schedule 2: *** Script: overlapSpan Dates: 2024-03-21 07:00:00 - 2024-03-21 21:00:00 *** Script: overlapSpan Dates: 2024-04-21 07:00:00 - 2024-04-21 21:00:00
This is how i get the schedule dates of maintenace windows for a particular CI.
now the closest date to planned start date of change request form: 2024-03-28 01:00:00 from schedule 1 is 2024-04-03 01:00:00 - 2024-04-03 23:00:00
now the closest date to planned start date of change request: 2024-03-26 01:00:00 from schedule 2 is 2024-04-21 07:00:00 - 2024-04-21 21:00:00
among these two date the closest date to planned start date of change request is 2024-04-03 01:00:00 - 2024-04-03 23:00:00
so how to find the closest date via scripting and print both the date of schedule?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 01:27 PM
Hi,
Can you try the below changes to your script and see if this works for you?
var plannedStartDate = new GlideDateTime("2024-03-26 01:00:00");
var closestStartDate; // Variable to store the closest start date
var closestEndDate; // Variable to store the corresponding end date
var closestDifference = Infinity; // Variable to store the closest time difference, initialized to Infinity
while (ss.next()) {
// Your existing code to retrieve schedule spans
while (overlaps.hasNext()) {
var overlapSpan = overlaps.next();
var scheduleStartDate = new GlideDateTime(overlapSpan.getStart());
var scheduleEndDate = new GlideDateTime(overlapSpan.getEnd());
// Calculate the time difference between the planned start date and the schedule span's start date
var timeDifference = Math.abs(plannedStartDate.getNumericValue() - scheduleStartDate.getNumericValue());
// If the current schedule span is closer than the previous closest one, update the closest variables
if (timeDifference < closestDifference) {
closestStartDate = scheduleStartDate;
closestEndDate = scheduleEndDate;
closestDifference = timeDifference;
}
}
}
// Output the closest schedule span's start and end dates
gs.log("Closest schedule span to planned start date:");
gs.log("Start Date: " + closestStartDate.getDisplayValue());
gs.log("End Date: " + closestEndDate.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 01:11 PM
Hi, one option might be an array or object to hold a reference 'offset' value and a schedule/id.
Then in your while loop use GlideDateTime.subtract() to calculate how close your offset is to your objective, with Math.abs() allowing comparison of negative\positive values.
If the current overlap is closer to the target than the reference, then update the reference with the current schedule and offset. When the loop is complete your reference will contain the closest result.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 01:27 PM
Hi,
Can you try the below changes to your script and see if this works for you?
var plannedStartDate = new GlideDateTime("2024-03-26 01:00:00");
var closestStartDate; // Variable to store the closest start date
var closestEndDate; // Variable to store the corresponding end date
var closestDifference = Infinity; // Variable to store the closest time difference, initialized to Infinity
while (ss.next()) {
// Your existing code to retrieve schedule spans
while (overlaps.hasNext()) {
var overlapSpan = overlaps.next();
var scheduleStartDate = new GlideDateTime(overlapSpan.getStart());
var scheduleEndDate = new GlideDateTime(overlapSpan.getEnd());
// Calculate the time difference between the planned start date and the schedule span's start date
var timeDifference = Math.abs(plannedStartDate.getNumericValue() - scheduleStartDate.getNumericValue());
// If the current schedule span is closer than the previous closest one, update the closest variables
if (timeDifference < closestDifference) {
closestStartDate = scheduleStartDate;
closestEndDate = scheduleEndDate;
closestDifference = timeDifference;
}
}
}
// Output the closest schedule span's start and end dates
gs.log("Closest schedule span to planned start date:");
gs.log("Start Date: " + closestStartDate.getDisplayValue());
gs.log("End Date: " + closestEndDate.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 02:32 PM
From the below script i am trying to get the overlap dates of a schedule
It is providing me the display values of the schedule date fields, Display value not getting adjusted according to user timezone.
var ss = new GlideRecord('cmn_schedule_span'); ss.addQuery('schedule', 'sys ids of the maintenance schedules'); ss.orderBy('start_date_time'); ss.query(); // consider i have 2 maitenace schedules while (ss.next()) { var timeZone = gs.getSession().getTimeZoneName(); var thisSchedule = new GlideSchedule(); thisSchedule.setTimeZone(timeZone); gs.log("addHoliday to Schedule " + ss.name); thisSchedule.addTimeSpan(ss); Start from Today var gd = new GlideDate(); var gdt = new GlideDateTime(); gdt.setNumericValue(gd.getNumericValue()); / GDT (GlideDateTime) // Start Datevar sd = new GlideDateTime(); sd.setValue(gdt.getDate() + " 00:00:00");
/ End Date var ed = new GlideDateTime(); ed.setValue(gdt.getDate() + " 23:59:59"); ed.addDaysUTC(730); // Look 2 years ahead (this could be anything)
var ssd = new GlideScheduleDateTime(sd); // Start Day ssd.setTimeZone(timeZone);
var sed = new GlideScheduleDateTime(ed); // End Day sed.setTimeZone(timeZone);
var scheduleMap = thisSchedule.getTimeMap(sd, ed, timeZone); var span = new GlideScheduleDateTimeSpan(ssd, sed); var thisMap = new GlideScheduleTimeMap(); thisMap.addInclude(span); thisMap.buildMap(timeZone); overlaps = scheduleMap.overlapsWith(thisMap, timeZone); overlaps.buildMap(timeZone); while (overlaps.hasNext()) { var overlapSpan = overlaps.next(); gs.log("overlapSpan Dates: " + overlapSpan.getStart() + " - " + overlapSpan.getEnd()); } }
output:
schedule 1: *** Script: overlapSpan Dates: 2024-03-27 01:00:00 - 2024-03-27 23:00:00 *** Script: overlapSpan Dates: 2024-04-03 01:00:00 - 2024-04-03 23:00:00 schedule 2: *** Script: overlapSpan Dates: 2024-03-21 07:00:00 - 2024-03-21 21:00:00 *** Script: overlapSpan Dates: 2024-04-21 07:00:00 - 2024-04-21 21:00:00
EXPECTED OUTPUT:
GMT values of the schedule/Display value according to usertimezone
Reason for conversion
the above script not working based on user timezone. it is not displaying the overlap span according to user timezone
my system time zone is Europe/Dublin so if can at least get the GMT of Europe/Dublin , i can adjust the values to user timezone
please provide a solution.
thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 03:10 PM
Make sure to replace 'sys ids of the maintenance schedules' with the actual sys IDs of the maintenance schedules you want to query. Also, ensure that the timezone settings are correctly configured in your GlideDateTime objects
var ss = new GlideRecord('cmn_schedule_span');
ss.addQuery('schedule', 'sys ids of the maintenance schedules');
ss.orderBy('start_date_time');
ss.query(); // consider i have 2 maintenance schedules
while (ss.next()) {
var timeZone = gs.getSession().getTimeZoneName();
var thisSchedule = new GlideSchedule();
thisSchedule.setTimeZone(timeZone);
gs.log("addHoliday to Schedule " + ss.name);
thisSchedule.addTimeSpan(ss);
// Start from Today
var gd = new GlideDate();
var gdt = new GlideDateTime();
gdt.setNumericValue(gd.getNumericValue());
// Start Date
var sd = new GlideDateTime();
sd.setValue(gdt.getDate() + " 00:00:00");
// End Date
var ed = new GlideDateTime();
ed.setValue(gdt.getDate() + " 23:59:59");
ed.addDaysUTC(730); // Look 2 years ahead (this could be anything)
var ssd = new GlideScheduleDateTime(sd); // Start Day
ssd.setTimeZone(timeZone);
var sed = new GlideScheduleDateTime(ed); // End Day
sed.setTimeZone(timeZone);
var scheduleMap = thisSchedule.getTimeMap(sd, ed, timeZone);
var span = new GlideScheduleDateTimeSpan(ssd, sed);
var thisMap = new GlideScheduleTimeMap();
thisMap.addInclude(span);
thisMap.buildMap(timeZone);
overlaps = scheduleMap.overlapsWith(thisMap, timeZone);
overlaps.buildMap(timeZone);
while (overlaps.hasNext()) {
var overlapSpan = overlaps.next();
gs.log("overlapSpan Dates: " + overlapSpan.getStart() + " - " + overlapSpan.getEnd());
}
}