Change the timezone of the latex document irrespective of the computer time zone using datetime2

datetime

I want to change the timezone format (say, I want to use always GMT-3 timezone). I am using the datetime2 package. Do I need to use the following command as mentioned in the manual?

\DTMdisplayzone{〈TZh〉}{〈TZm〉}

Best Answer

The datetime2 package has a library for performing calculations on times and dates which can be loaded with \usepackage[calc]{datetime2}. This library provides two functions to convert from and to Zulu time (UTC+0). With these functions you can do a three-step approach: first convert the local time to Zulu time, second convert the Zulu time to the timezone you want to show, and third actually show the result of step two in the requested timezone.

In the MWE below I defined two new commands, one for converting to arbitrary time zones with two arguments for the offset and the reverse offset, and one shortcut command with the arguments pre-filled for Central European Summer Time (which is a bit odd since it is not summer at the moment, but in any case it is UTC+2, so Finland, the Baltics, most of the Balkan countries, some Middle Eastern countries, Egypt, Libya).

MWE:

\documentclass[en-MT]{article}
\usepackage[calc,useregional]{datetime2}
\begin{document}
\DTMsetstyle{en-MT-numeric}

\newcommand{\DTMtznow}[2]{%
% store current time in object 'now'
\DTMsavenow{now}%
% convert current time to UTC+0
\DTMtozulu{now}{currzulu}%
% add requested timezone offset to zulu time
\DTMsaveaszulutime{currcest}{\DTMfetchyear{currzulu}}{\DTMfetchmonth{currzulu}}{\DTMfetchday{currzulu}}{\DTMfetchhour{currzulu}}{\DTMfetchminute{currzulu}}{\DTMfetchsecond{currzulu}}{#2}{00}%
% display zulu+offset in requested timezone (= reverse offset)
\DTMdisplay{\DTMfetchyear{currcest}}{\DTMfetchmonth{currcest}}{\DTMfetchday{currcest}}{}{\DTMfetchhour{currcest}}{\DTMfetchminute{currcest}}{\DTMfetchsecond{currcest}}{#1}{00}%
}
% shortcut command for central european summer time (UTC+2)
\newcommand{\DTMcestnow}{\DTMtznow{+02}{-02}}

in current timezone: \DTMnow

in CEST: \DTMcestnow
\end{document}

Result:

enter image description here

Related Question