Invalid time zone exception when supplying unrecognized zone identifier
0 reputation · 16 Jan 2020, 17:40 UTC
0 reputation · 16 Jan 2020, 17:40 UTC
When DateTime->new receives a time_zone argument that DateTime::TimeZone does not recognize, it throws an exception with the message Invalid time zone: …. The module does not fall back to the floating time zone in this case; the floating zone is used only when the parameter is explicitly set to floating or omitted entirely.
The exception originates from DateTime::TimeZone’s attempt to load OS‑specific zoneinfo files (for example /usr/share/zoneinfo). On systems where those files are missing or unreadable, the same error is raised even for zone names that would be valid on a fully provisioned host. Meanwhile, a long‑standing design question remains open: whether DateTime should automatically map common abbreviations such as EST or PST to their corresponding offsets, a behavior that currently requires DateTime::TimeZone::Alias or external modules.
Version differences between DateTime 1.x and 2.x may affect lazy loading of zone data, and reliance on the OS zone database introduces portability concerns for containerized or minimal environments.
DateTime treat unrecognized abbreviations as a recoverable condition and attempt an offset mapping, or is the explicit exception the intended contract?Invalid time zone exception?DateTime->new throws Invalid time zone: … when the supplied time_zone argument is not recognized by DateTime::TimeZone. There is no automatic fallback to the floating time zone. Floating is used only when time_zone is explicitly set to floating or omitted entirely.
The exception originates from DateTime::TimeZone attempting to load OS-specific zoneinfo files, e.g. /usr/share/zoneinfo. On systems where those files are missing or unreadable, the same error is raised even for names that would be valid on a fully provisioned host. Abbreviations such as EST or PST are not resolved without DateTime::TimeZone::Alias or an external mapping.
perl -MDateTime::TimeZone -e 'print join("\n", DateTime::TimeZone->names())'
ls -l /usr/share/zoneinfo/Region/City
DateTime->new( time_zone => "floating" )
To choose the correct remediation, can you confirm the operating system and Perl/DateTime versions in use? That changes whether the issue is a missing zoneinfo database or a version-specific lazy-load behavior.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.