function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JamsieJamsie 

Daylight Savings Time

Hi all,

 

I just discovered a problem where I was using some Apex to set a Task due date based upon a service level agreement.  I was using...

 

t.Due_Date_Time__c =  BusinessHours.add(bh.id, t.CreatedDate, SLA_hours * 60 * 60 * 1000L);

 

The BusinessHours class applies daylight savings time but the Task CreatedDate is expressed in GMT.  Therefore I need to convert CreatedDate to the local Datetime.  I could not find an API call to achieve this in one step so built a method in a utility class:

 

    public static Datetime getLocalDateTime(Datetime z)
    {    
        Datetime l = z.Date(); //local date at 00:00:00
        l = l.addHours(z.hour());
        l = l.addMinutes(z.minute());
        l = l.addSeconds(z.second());
        
        return l;
    }

 

Applying this to my original code gives:

 

t.Due_Date_Time__c =  BusinessHours.add(bh.id, Utility.getLocalDateTime(t.CreatedDate), SLA_hours * 60 * 60 * 1000L);

 

This fixed my bug .

 

I hope you find it useful.