2

We have a custom Datetime field on a custom object, Appointment__r.Start_Date__c.

At 6pm, the day before the appointment, we must send every customer with an appointment an SMS which reminds them of the appointment, including the correct time.

We are using a batch process and scheduler to collect the relevant appointments and Twilio for Salesforce to send the messages.

Currently I am using appointment.Start_Date__c.addHours(GERMAN_TIME_OFFSET).format('HH:mm'), where GERMAN_TIME_OFFSET = 2.

... but unfortunately summer will end, so having a hardcoded 2 will lead to incorrect results.

While I could compare Datetime.now.addDays(1) against some specific date to check whether tomorrow will be in daylights savings, every year that date will change since it the clock always changes on the last Sundays of whatever month... This doesn't seem so trivial to calculate, and (to be honest) I'm hoping there is an easier/better way.

Any suggestions/solutions?

3
  • 2
    Have you looked into how TimeZone works? Sep 10, 2020 at 16:13
  • 1
    You can use the TimeZone.getOffset which will appropriately return a DST-aware offset for the given timezone for a specific date and time (the office can then be applied directly against a UTC (Salesforce "GMT") value).
    – Phil W
    Sep 10, 2020 at 16:29
  • I forgot about the TimeZone class.... I'll play with it tomorrow, thanks. :-) Sep 10, 2020 at 16:40

1 Answer 1

4

Simply use Salesforce's TimeZone class, and specifically the TimeZone.getOffset method.

You can probably do something like:

TimeZone germanyTZ = TimeZone.getTimeZone('Europe/Berlin');

System.debug(germanyTZ.getID());

appointment = appointment.addSeconds(germanyTZ.getOffset(appointment) / 1000);

System.debug(appointment);

Note that when looking to get a TimeZone instance via TimeZone.getTimeZone, it is always worth checking to see if it comes back with the ID "GMT" which it will do if the ID you gave is not valid.

2
  • I forgot about the TimeZone class.... I'll play with it tomorrow, thanks. :-) Sep 10, 2020 at 16:40
  • Exactly what I needed, thanks! Sep 11, 2020 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.