Apex DateTime.format with Unrecognized Timezone Identifier – Missing Validation Constraint
0 reputation · 31 May 2023, 21:35 UTC
When using DateTime.format(String format, String timezoneId) in Apex, the method accepts any string for the timezoneId argument. If the supplied identifier does not correspond to a known IANA time zone, the platform silently replaces it with the organization’s default time zone and returns a formatted string based on that fallback.
The goal is to guarantee that regional date conversions reflect the intended zone, especially during daylight‑saving transitions, without relying on undocumented behavior.
Because the fallback is not documented as an error condition, developers cannot depend on the method to signal a mistyped zone, which creates a validation gap and risks incorrect timestamps in integrations.
Should the platform throw an exception when an unrecognized timezoneId is provided? Should developers be required to validate timezoneId against a known list before calling format? Is there a configuration or setting that can enforce strict timezone validation in Apex?