The script behaves inconsistently, producing correct results only for system timezone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2024 11:55 AM
I've developed a script to check if a planned start and end time coincide with a blackout schedule. The script retrieves the start and end dates of a change request from a GlideRecord and compares them against a schedule defined in another GlideRecord.
// Script to check blackout schedule
// Get user timezone - just to show the difference in outputs
var userTimeZone = gs.getSession().getTimeZone();
gs.info('User Timezone: ' + userTimeZone);
// Retrieve change request dates
var cor = new GlideRecord('x_novrp_it_it_change');
cor.addQuery('sys_id', '96fa52c98786461049aa0d030cbb359c');
cor.query();
if (cor.next()) {
var startdateofchgci = cor.start_date;
var enddataeofchgci = cor.end_date;
gs.info('Start Date: ' + startdateofchgci);
gs.info('End Date: ' + enddataeofchgci);
}
// Retrieve blackout schedule
var scheduleEntryGRsoft = new GlideRecord('cmn_schedule_span');
scheduleEntryGRsoft.addQuery('schedule', 'b257e7a11b398e5884e2caa16b4bcb62');
scheduleEntryGRsoft.query();
if (scheduleEntryGRsoft.next()) {
var recurrencerule = scheduleEntryGRsoft.schedule;
var schedule = new GlideSchedule(recurrencerule);
schedule.setTimeZone('Europe/Dublin');
// Convert dates to GlideDateTime objects
var gdt_start = new GlideDateTime(startdateofchgci);
var gdt_end = new GlideDateTime(enddataeofchgci);
// Calculate duration
var dur = schedule.duration(gdt_start, gdt_end);
var schedule_time = dur.getNumericValue() / 1000;
// Check if dates are within schedule
if (schedule.isInSchedule(gdt_start) || schedule.isInSchedule(gdt_end) || schedule_time > 0) {
gs.info('Found in blackout schedule');
} else {
gs.info('Not found in blackout schedule');
}
}
1. Checking Planned Start and End Against Blackout Schedule
- User's Timezone: Europe/Dublin
- Script Outcome: Working fine
- Output
*** Script: userTimezonesun.util.calendar.ZoneInfo[id="Europe/Dublin",offset=0,dstSavings=3600000,useDaylight=true,transitions=227,lastRule=java.util.SimpleTimeZone[id=Europe/Dublin,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
*** Script: startdateofchgci 2024-06-01 15:00:00
*** Script: enddataeofchgci 2024-06-01 16:00:00
*** Script: FOUND IN SCHEDULES:
2. Checking Planned Start and End Against Blackout Schedule
- User's Timezone: IST
- Script Outcome: Not working as expected
- Output:
*** Script: userTimezonesun.util.calendar.ZoneInfo[id="IST",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
*** Script: startdateofchgci 2024-06-01 15:00:00
*** Script: enddataeofchgci 2024-06-01 16:00:00
*** Script: NOT FOUND IN SCHEDULES:
3. Checking Planned Start and End Against Blackout Schedule
- User's Timezone: Europe/Dublin
- Script Outcome: Working fine
- Output
*** Script: userTimezonesun.util.calendar.ZoneInfo[id="IST",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
*** Script: startdateofchgci 2024-06-01 14:59:00
*** Script: enddataeofchgci 2024-06-01 16:00:00
*** Script: FOUND IN SCHEDULES:
Outcome:
- In scenario 1, when the user is in the Europe/Dublin timezone, the script correctly identifies that the planned start and end times coincide with the blackout schedule.
- In scenario 2, when the user is in the IST timezone, the script does not produce the expected outcome, indicating that the planned times do not coincide with the blackout schedule, despite being the same as in scenario 1.
- However, in scenario 3, when the start time is adjusted by one minute earlier in the IST timezone, the script correctly identifies that the planned times fall within the blackout schedule.
Issue: The script works as expected when the user is in the Europe/Dublin timezone. However, when the user is in the IST timezone, it doesn't always produce the correct result.
Questions:
- Why does the script produce different outcomes for users in different timezones, even though the change request dates remain the same?
- Why does adjusting the start date by just one minute in the IST timezone make the script work correctly?
Expected Outcome: The script should accurately determine whether the change request dates fall within the blackout schedule, regardless of the user's timezone.
Actual Outcome: The script behaves inconsistently, producing correct results only for system timezone or with minor adjustments to the input dates for other timezones.
I'd appreciate any insights or suggestions on how to address this issue. Thank you!