Using GlideSchedule to create events in the future with DST

Nathan Sanders
Tera Contributor

I recently encountered an issue setting a fixed time for events in the future inside a script; when it comes to DST, the time would be incorrectly skewed by an hour. The UI performs as you would expect, you set the time in the future, and the TZ and DST offset gets correctly assigned to the value and saved as a UTC DateTime value. However, when using the GlideSchedule function to get a span of dates, the timezone is accounted for but NOT the DST offset. So I went around and around on a solution and found the following fix. I had initially gone down the road to migrating all the timezones to Country/City format as there is one KB that SN references that state DST are not accounted for in the old Time Zones, but then I found another kb that says they are linked. Well, the time zone change did not help, and it was 100% that on the script side, even though you provide a timezone with a DST, the system does not add this offset automatically to your values. I hope this helps others in the future!

 

Competing KB's:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0594661

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0960564 

 

Excerpt from my large Script Include, this is what takes the GlideDateTime object in and spits a DST-corrected GlideDtateTime object out:

    setDstOffset: function(time) {

        // Make sure we account for DST
        var dstOffset = (-Number(time.getDSTOffset() / 1000));
        time.addSeconds(dstOffset);
        return time;

 

Where I'm creating the timespan and calling the DST function:

   refreshTasksInSchedule: function(spansStart, spansEnd, schedule, template, newTask_ID) {
        if (!spansStart || !spansEnd)
            return;
        var timeMap = schedule.getTimeMap(spansStart, spansEnd);
        timeMap.buildMap(schedule.getTimeZone());

        // Skip the first element in the timeMap if it spans now 
        if (schedule.isInSchedule(new GlideDateTime()) && timeMap.hasNext() && !newTask_ID) {
            timeMap.next();
        }

        var timeSpan;
        while (timeMap.hasNext()) {
            count += 1;
            timeSpan = timeMap.next();

            var start = timeSpan.getStart().getGlideDateTime();
            var end = timeSpan.getEnd().getGlideDateTime();

			var today = new GlideDateTime(gs.endOfToday());
			if (start >= today){
				var newTaskID = this.refreshTask(template, start, end, newTask_ID);
				...
			}
        }

    },
    refreshTask: function(template, start, end, newTask_ID) {
        var timeZone = Packages.java.util.TimeZone.getTimeZone(template.u_time_zone);

        var startDate = new GlideDateTime(start);
        startDate.addSeconds(this.getTzOffsetSeconds(timeZone));

        // Setting DST offset for time
        startDate = this.setDstOffset(startDate);

        var endDate = new GlideDateTime(end);
        endDate.addSeconds(this.getTzOffsetSeconds(timeZone));

        // Setting DST offset for time
        endDate = this.setDstOffset(endDate);
        ...
}

 

0 REPLIES 0