DurationCalculator - getSeconds vs getTotalSeconds

Douglas Mines
Kilo Contributor

Been asked to get the business duration of a ticket from opened until closed/now. Simple I say, we'll just have a quick durationCalculator, and throw a schedule/timezone, and the start/end dates at it.

var dc = new DurationCalculator();
dc.setSchedule('2c59445b4fdb9b8042800b318110c756');
var dur = dc.calcScheduleDuration(start, end);
var durSec = dc.getSeconds();
var durTotalSec = dc.getTotalSeconds();

if start/end are sufficiently far apart, dur and durSec cap out at MAX_INT_VAL/1000 (presumably behind the scenes the seconds value is being stored in millisecs), however durTotalSec seems to give a valid response. I'm just running this across multiple tickets in a non-prod to validate, but can't seem to find any good documentation on getTotalSeconds, other than totalSeconds being set during calcScheduleDuration and it being a standard getter.

Anyone able to shed any light?

 

1 ACCEPTED SOLUTION

ARG645
Tera Guru

Hi Douglas, 

After reading your question, I was playing around with DurationCalculator() script include. After examining the script include and after running some background scripts. Below is what i found the main difference between getSeconds and getTotalSeconds.

Summary:- 

getTotalSeconds: Doesn't care about the schedule, it will give the difference in seconds between startDate and EndDate without taking schedule into consideration 

getSeconds: Gives the Difference in Seconds, it will take the schedule into consideration(only if a schedule is set)

Example: -

Say I have a schedule that is 24/5 Weekdays only. 

var start = "2018-08-23 08:00:00"; // Tomorrow, Thursday
var end = "2018-08-28 08:00:00";// next week Tuesday
var dc = new DurationCalculator();
dc.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');// My 24/5 Schedule
var dur = dc.calcScheduleDuration(start, end);
var durSec = dc.getSeconds();// Skips weekdays as it involves the schedule into calculation
var durTotalSec = dc.getTotalSeconds();//Doesnt skip weekdays as it excludes the schedule in calculation

gs.print(durSec/86400);
gs.print(durTotalSec/86400);

Output:

*** Script: 3
*** Script: 5

 

Other things I found which may help you understand: 

getTotalSeconds is calculated from below logic

_totalSeconds: function(/* GlideDateTime */ startTime, /* GlideDateTime */ endTime) {
      return Math.max(0, (Math.floor(endTime.getNumericValue()/1000) - Math.floor(startTime.getNumericValue()/1000) ));
   },

Some Useful comments

 /**
    * Get the this.seconds property that was set by calcDuration/calcRelativeDuration
    * indicating the total number of seconds of work to be performed for the duration.
    *
    * (Note: this is the total work time, not the total time between start and end times
    * and may be used to determine percentages of the work time)
    */
   getSeconds: function() {
      return this.seconds;
   },
   
   /**
    * Get the this.totalSeconds property that was set by calcDuration/calcRelativeDuration
    * indicating the total number of seconds between the start and end times of the duration.
    */
   getTotalSeconds: function() {
      return this.totalSeconds;
   },

 

Please mark my my answer Correct/Helpful if applicable, so that it will help others in future. 

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Douglas,

Following link should help you:

https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=c_DurationCalculatorAPI

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

ARG645
Tera Guru

Hi Douglas, 

After reading your question, I was playing around with DurationCalculator() script include. After examining the script include and after running some background scripts. Below is what i found the main difference between getSeconds and getTotalSeconds.

Summary:- 

getTotalSeconds: Doesn't care about the schedule, it will give the difference in seconds between startDate and EndDate without taking schedule into consideration 

getSeconds: Gives the Difference in Seconds, it will take the schedule into consideration(only if a schedule is set)

Example: -

Say I have a schedule that is 24/5 Weekdays only. 

var start = "2018-08-23 08:00:00"; // Tomorrow, Thursday
var end = "2018-08-28 08:00:00";// next week Tuesday
var dc = new DurationCalculator();
dc.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');// My 24/5 Schedule
var dur = dc.calcScheduleDuration(start, end);
var durSec = dc.getSeconds();// Skips weekdays as it involves the schedule into calculation
var durTotalSec = dc.getTotalSeconds();//Doesnt skip weekdays as it excludes the schedule in calculation

gs.print(durSec/86400);
gs.print(durTotalSec/86400);

Output:

*** Script: 3
*** Script: 5

 

Other things I found which may help you understand: 

getTotalSeconds is calculated from below logic

_totalSeconds: function(/* GlideDateTime */ startTime, /* GlideDateTime */ endTime) {
      return Math.max(0, (Math.floor(endTime.getNumericValue()/1000) - Math.floor(startTime.getNumericValue()/1000) ));
   },

Some Useful comments

 /**
    * Get the this.seconds property that was set by calcDuration/calcRelativeDuration
    * indicating the total number of seconds of work to be performed for the duration.
    *
    * (Note: this is the total work time, not the total time between start and end times
    * and may be used to determine percentages of the work time)
    */
   getSeconds: function() {
      return this.seconds;
   },
   
   /**
    * Get the this.totalSeconds property that was set by calcDuration/calcRelativeDuration
    * indicating the total number of seconds between the start and end times of the duration.
    */
   getTotalSeconds: function() {
      return this.totalSeconds;
   },

 

Please mark my my answer Correct/Helpful if applicable, so that it will help others in future. 

Thanks Aman, that's good info - never thought to look at DucationCalculator Script Include.

Looks like the line:

this.seconds = this.schedule.duration(startTime, this.endDateTime, this.timezone).getNumericValue() / 1000;

is the source of my issue.

[EDIT: Removed script as it doesn't work]

I believe the duration needs to be split separate from the NumericValue and a getDays method called to return the correct value. 

Many thanks,

ARG645
Tera Guru
Douglas, Please close this thread if your question is answered.