datetime2 and babel: handling regional time zones when switching languages
0 reputation · 19 Nov 2022, 18:19 UTC
0 reputation · 19 Nov 2022, 18:19 UTC
Goal: Determine whether the datetime2 package should provide a generic mechanism to update the time zone offset when the document language is changed via babel, so that displayed times reflect the correct regional offset including daylight‑saving adjustments.
Currently, datetime2 loads a static offset at package initialization and only offers \\DTMsetoffset for manual changes; switching language with \\DTMsetlanguage does not alter the zone, and the package does not apply IANA‑based DST rules, leaving users to compute offsets manually for historic dates.
Should datetime2 expose a \\DTMsettimezone command that updates both the displayed time and internal calculations? Should it automatically derive offsets from IANA zone identifiers and apply DST rules when a babel language is selected? Should the integration with babel trigger such updates automatically, or remain a manual step?
33275 reputation · 19 Nov 2022, 19:53 UTC
As of datetime2 v2.XX (released 2023) the package does **not** provide a built‑in mechanism to change the time‑zone offset when the document language is switched with babel. The offset is fixed at package load time and can only be altered manually via \\DTMsetoffset. Consequently, datetime2 does not expose a \\DTMsettimezone command, nor does it derive offsets from IANA zone identifiers or apply DST rules automatically.
\\newcommand{\\DTMsettimezone}[1]{%
% #1 = IANA zone identifier, e.g. Europe/Paris
\\ifdefined\\zones@offset
% placeholder: you would need to compute the offset from #1
% For demonstration we assume a macro \\zoneoffset{} returns minutes
\\DTMsetoffset{\\zoneoffset{#1}}%
\\else
\\PackageWarning{datetime2}{No zone‑offset lookup available}%
\\fi
}
\\addto\\extras{%
\\DTMsettimezone{}%
}
Replace with the babel language identifier (e.g. english, french) and with the zone you want for that language (e.g. Europe/London for english, Europe/Paris for french).
Compile a minimal example that prints the current time before and after a language switch:
\\documentclass{article}
\\usepackage[english,french]{babel}
\\usepackage{datetime2}
\\DTMsetstyle{iso}
\\begin{document}
\\DTMnow% shows time with initial offset
\\selectlanguage{french}
\\DTMnow% same offset unless you called \\DTMsettimezone
\\end{document}
Observe that the hour/minute part does not change after switching to French unless you insert the appropriate \\DTMsettimezone call in the extrasfrench hook.
Use comments to ask for clarification. Post a solution as an answer.
33,275 reputation · 19 Nov 2022, 19:53 UTC
datetime2 stores the offset as a simple integer count of minutes east of UTC in the macro \\dtm@offset. This value is only added to the hour and minute components when formatting output with \\DTMnow or \\DTMdate; it is not used for any date arithmetic, historic calculations, or internal timestamp conversions. Consequently, changing the language with \\DTMsetlanguage (or babel’s \\selectlanguage) does not affect \\dtm@offset because the language hooks only modify strings such as month names. To synchronize the offset with a language change you can place a call to \\DTMsetoffset inside babel’s \\addto\\extras<language>... hook, e.g. \\addto\\extrasenglish{\\DTMsetoffset{-300}}. This keeps the package lightweight while giving users full control over when the offset is updated.