Salesforce developers can obtain time zone information easily from Apex code by utilizing the Local Time API.
The following sample apex code illustrates how to use the API method tz.LocalTimeV2.getTimezones() in different scenarios. This function is bulkified, and thus it is ideal to use in triggers, flows, and in other use cases where you need to process multiple records in one API call.
Pre-requisite
It is assumed that you have installed Local Time App version v2.20 or later. You can install the latest version of the App from this link.
Sample Code
public class LocalTimeAPITest { // sub class to store the timezone request information public with sharing class TimezoneRequest { public String id; // record id or some unique key public String country; public String state; public String city; public String postalcode; public String phone; public Boolean adjust_dst; // set it to true if data needs to be adjusted for DST public Boolean broad_match; // set it to true if result does not need to be very clear-cut or definitive } // sub class to store timezone response public with sharing class TimezoneResponse { public String id; // record id or some unique key public Decimal offset; public Decimal dst_offset; public String abbreviation; public String dst_abbreviation; public String tz_name; public String dst_tz_name; public String tz_iana; // timezone by iana org public String tz_sfdc; // salesforce timezone public String dst_setting; // Id of the DST_Setting__c record public String system_country; // country name in the database public String system_state; // state name in the database } // Get the timezones for a given request // if the request is ambiguous, all available timezones are returned // if the request is unambiguous, exact timezone is returned public static void testgetTimezones() { List<TimezoneRequest> tz_reqs = new List<TimezoneRequest>(); TimezoneRequest tz_req = new TimezoneRequest(); tz_req.id = '100'; tz_req.country = 'USA'; tz_req.state = 'TX'; tz_req.adjust_dst = true; tz_req.broad_match = true; tz_reqs.add(tz_req); // ambiguous and broad match is set to true, so we expect to get two records for this request as Texas observes two timezones tz_req = new TimezoneRequest(); tz_req.id = '200'; tz_req.country = 'USA'; tz_req.state = 'TX'; tz_req.adjust_dst = true; tz_reqs.add(tz_req); // ambiguous but broad match is false, so we expect to get one record with the widely used timezone in Texas tz_req = new TimezoneRequest(); tz_req.id = '300'; tz_req.country = 'USA'; tz_req.state = 'TX'; tz_req.postalcode = '79910'; tz_req.adjust_dst = true; tz_reqs.add(tz_req); // unambiguous, so we expect to get one record for this request // it does not matter if you set broad_match to true or false tz_req = new TimezoneRequest(); tz_req.id = '400'; tz_req.country = 'Germany'; tz_req.adjust_dst = false; tz_reqs.add(tz_req); // unambiguous, so we expect to get one record for this request // it does not matter if you set broad_match to true or false tz_req = new TimezoneRequest(); tz_req.id = '500'; tz_req.country = 'USA'; tz_req.phone = '+1 212 1234'; // New York tz_req.adjust_dst = false; tz_reqs.add(tz_req); // unambiguous, so we expect to get one timezone record based on phone area code as New York observes one timezone tz_req = new TimezoneRequest(); tz_req.id = '600'; tz_req.country = 'USA'; tz_req.state = 'CA'; tz_req.adjust_dst = true; tz_reqs.add(tz_req); // unambiguous, so we expect to get one timezone record as California observes one timezone String request = JSON.serialize(tz_reqs); String resp = tz.LocalTimeV2.getTimezones(request); // make the API call Map<String, List<TimezoneResponse>> map_tzones = (Map<String, List<TimezoneResponse>>) JSON.deserialize(resp, Map<String, List<TimezoneResponse>>.class); List<TimezoneResponse> tzones = map_tzones.get('100'); system.debug(tzones); // Expected response - two records /* (TimezoneResponse:[abbreviation=CDT, dst_abbreviation=null, dst_offset=null, dst_setting=a013N000002oBORQA2, dst_tz_name=null, id=100, offset=-5, system_country=USA, system_state=TX, tz_iana=America/Chicago, tz_name=Central Daylight Time, tz_sfdc=America/Chicago], TimezoneResponse:[abbreviation=MDT, dst_abbreviation=null, dst_offset=null, dst_setting=a013N000002oBORQA2, dst_tz_name=null, id=100, offset=-6, system_country=USA, system_state=TX, tz_iana=America/Denver, tz_name=Mountain Daylight Time, tz_sfdc=America/Denver]) */ tzones = map_tzones.get('200'); system.debug(tzones); // Expected response - 1 record /* (TimezoneResponse:[abbreviation=CDT, dst_abbreviation=null, dst_offset=null, dst_setting=a013N000002oBORQA2, dst_tz_name=null, id=200, offset=-5, system_country=USA, system_state=TX, tz_iana=America/Chicago, tz_name=Central Daylight Time, tz_sfdc=America/Chicago]) */ tzones = map_tzones.get('300'); system.debug(tzones); // Expected response - 1 record /* (TimezoneResponse:[abbreviation=MDT, dst_abbreviation=null, dst_offset=null, dst_setting=a013N000002oBORQA2, dst_tz_name=null, id=300, offset=-6, system_country=USA, system_state=TX, tz_iana=America/Denver, tz_name=Mountain Daylight Time, tz_sfdc=America/Denver]) */ tzones = map_tzones.get('400'); system.debug(tzones); // Expected response - 1 record /* (TimezoneResponse:[abbreviation=CET, dst_abbreviation=CEST, dst_offset=2, dst_setting=a013N000002oBMIQA2, dst_tz_name=Central European Summer Time, id=400, offset=1, system_country=GERMANY, system_state=null, tz_iana=Europe/Berlin, tz_name=Central European Time, tz_sfdc=Europe/Berlin]) */ tzones = map_tzones.get('500'); system.debug(tzones); // Expected response - 1 record /* (TimezoneResponse:[abbreviation=EST, dst_abbreviation=EDT, dst_offset=-4, dst_setting=a013N000002oBORQA2, dst_tz_name=Eastern Daylight Time, id=500, offset=-5, system_country=USA, system_state=null, tz_iana=America/New_York, tz_name=Eastern Standard Time, tz_sfdc=America/New_York]) */ tzones = map_tzones.get('600'); system.debug(tzones); // Expected response - 1 record /* (TimezoneResponse:[abbreviation=PDT, dst_abbreviation=null, dst_offset=null, dst_setting=a013N000002oBORQA2, dst_tz_name=null, id=600, offset=-7, system_country=USA, system_state=CA, tz_iana=America/Los_Angeles, tz_name=Pacific Daylight Time, tz_sfdc=America/Los_Angeles]) */ } } // class LocalTimeAPITest