9

Apex provides a TimeZone API. You can use this to query the detail of a time zone, named using its "SID" (a value like "Europe/London" or "America/New_York" for example). However, this API doesn't provide a means to query the SIDs for the supported time zones.

How can I determine the valid set of supported time zones by SID?

1 Answer 1

10

If you call TimeZone.getTimeZone with an invalid SID you get back the "GMT" time zone instance - this is one way to validate that your SID is supported (by checking that the SID for the object you get back is the same as the SID you provided). However, that doesn't help with getting the list of valid options.

Fortunately, the Salesforce schema includes a field on User, TimeZoneSidKey, that is a picklist containing all the valid SID values. This can be queried in Apex thus:

List<PicklistEntry> entries = Schema.SObjectType.User.fields.TimeZoneSidKey.picklistValues;

Each entry contains a single SID, which is the actual "value" for the entry.

3
  • 4
    FYI you can drop the Schema. in most cases, including this one.
    – Adrian Larson
    Commented Mar 18, 2019 at 16:29
  • 1
    User.TimeZoneSidKey.getDescribe().picklistValues also works
    – cropredy
    Commented Mar 18, 2019 at 16:58
  • Look at all that typing you guys could have saved me ;)
    – Phil W
    Commented Mar 18, 2019 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .