DurationCalculator calcScheduleDuration Mystery

stevejarman
Giga Guru

I'm having some problems using a DurationCalculator, which seems to be related to the schedule itself. Hoping someone has some thoughts on it.

I've included some snippets from the source code below, as well as screenshots of the schedule.

The summary version of the issue:

  • NOT USING the schedule, calcScheduleDuration calculates the duration between 2018-03-02 10:55:19 and 2018-03-12 15:22:40 as 10 04:27:21
  • USING the schedule, calcScheduleDuration calculates the duration between 2018-03-02 10:55:19 and 2018-03-12 15:22:40 as 3 00:00:00

I seem to consistently lose my remainder, and get a rounded number of days only... or something like that.

var dc = new DurationCalculator();

var bhSchedule = new GlideRecord("cmn_schedule");
bhSchedule.addQuery("name", "General Business Hours 7am - 7pm");
bhSchedule.query();

if (bhSchedule.next()) {
  dc.setSchedule(bhSchedule.getUniqueValue());
}

//
// A BUNCH OF OTHER STUFF HAPPENS IN HERE
//

gs.log("STEVEJ17 :: " + outages[x].start.toString() + " --> " + outages[x].end.toString());
// OUTPUT IS STEVEJ17 :: 2018-03-02 10:55:19 --> 2018-03-12 15:22:40

var focusDurationMs = dc.calcScheduleDuration(outages[x].start.toString(), outages[x].end.toString()) * 1000;
var focusDuration = new GlideDuration(focusDurationMs);

gs.log("STEVEJ17 :: " + focusDuration.getDurationValue());
// OUTPUT IS STEVEJ17 :: 3 00:00:00


// IF I COMMENT OUT THE SCHEDULE - I.E. THIS SECTION:
/*
var bhSchedule = new GlideRecord("cmn_schedule");
bhSchedule.addQuery("name", "General Business Hours 7am - 7pm");
bhSchedule.query();

if (bhSchedule.next()) {
  dc.setSchedule(bhSchedule.getUniqueValue());
}
*/

// THEN RESULTS LOOK CORRECT
// OUTPUT IS STEVEJ17 :: 2018-03-02 10:55:19 --> 2018-03-12 15:22:40
// OUTPUT IS STEVEJ17 :: 10 04:27:21

find_real_file.png

 

find_real_file.png

5 REPLIES 5

anguspalmer
Giga Guru

I'm getting expected results from the Duration Calculator in a new Kingston instance by adding a Timezone to the call to dc.setSchedule().

Here's my example ran as a background script. I'm using the ootb schedule; 8-5 weekdays 

var schedule = "8-5 weekdays"; // OOTB
var dc = new DurationCalculator();
var bhSchedule = new GlideRecord("cmn_schedule");
bhSchedule.addQuery("name", schedule);
bhSchedule.query();
if (bhSchedule.next()) {
  dc.setSchedule(bhSchedule.getUniqueValue(),"GMT");
} else {
  gs.log("Could not find schedule '" + schedule + "'");
}

var start = "2018-09-19 10:55:00";
var end = "2018-09-20 08:55:19";
var focusDurationMs = dc.calcScheduleDuration(start, end) * 1000;
var focusDuration = new GlideDuration(focusDurationMs);

gs.log(
    start +
    " :: " +
    end +
    " Duration = " +
    focusDuration.getDurationValue() +
    " (" +
    schedule +
    ")"
);
 // Expect 7hrs 19 secs