Hello I need help understanding how business hours difference is calculated with the Apex class. The documentation is rather poor and doesn't explain the input parameters assumptions. I found one post on a forum saying local time is assumed but it doesn't seem to be the case. Could someone help me to understand where I make wrong assumptions?
I have an org with Business Hours set 8AM-6PM. The org's time zone is set to Europe/London and till last Saturday of October it's tuned to British Summer Time (GMT+1).
If I submit a case at 17:15 BST and I want to check the minute difference between 18:00 BST the following day I would expect to see 645 minutes. But the Business Hours class returns 600.
//Datetime submitTime = DateTime.now(); simulate submitting case at 17:15 BST
Datetime submitTime = DateTime.newInstance(2018,10,17,17,15,0);//returns GMT 16:15
//move GMT time to London's local time
Timezone londonTZ = Timezone.getTimeZone('Europe/London');
submitTime = submitTime.addSeconds(londonTZ.getOffset(submitTime)/1000);
//now GMT has moved to BST 16:15->17:15
System.debug('===================== FINAL submitTime='+submitTime);
//construct end of working day the next day
Time endOfBusinessTime = Time.newInstance(18, 0, 0, 0); //UTC is assumed
Datetime targetTime = Datetime.newInstance(date.newInstance(2018,10,18), endOfBusinessTime);
//returns GMT time 17:00
//move GMT time to London's local time
targetTime = targetTime.addSeconds(londonTZ.getOffset(targetTime)/1000);//17GMT->18BST
System.debug('===================== FINAL targetTime='+targetTime );
// business hours are 8:00 -16:00 and org is set in BST timezone
BusinessHours defaultBH = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
Long inputMillisecs = BusinessHours.diff(defaultBH.Id, submitTime, targetTime);
Long total_duration_in_seconds = (inputMillisecs/1000).intValue();
System.debug('===================== diff in working minutes ='+total_duration_in_seconds/60); //this prints 600 not 645