[Tex/LaTex] Write date, time, and time zone

datetime

Background

Looking to include the current date, time, and timezone within a PDF.

Problem

The datetime package does not provide a macro for the current timezone.

Question

How would you write a macro in LaTeX to create a date such as:

Sun Jan 09 20:17:18 EST 2011

Working

Thus far:

\usepackage{datetime}
\shortdayofweekname{\day}{\month}{\year}
\shortmonthname{} \twodigit{\day} \hhmmsstime{}
EST \number\year

The timezone (EST) eludes me.

Related

Thank you!

Best Answer

You can get the offset from GMT using the pdfTeX primitive \pdfcreationdate: (no doubt LuaTeX has something similar)

\documentclass{article}
\begin{document}
\def\grabtimezone #1#2#3#4#5#6#7#8#9{\grabtimezoneB}
\def\grabtimezoneB #1#2#3#4#5#6#7{\grabtimezoneC}
\def\grabtimezoneC #1#2'#3'{sign: #1 / hr: #2 / min: #3}
\expandafter \grabtimezone\pdfcreationdate
\end{document}

If you need to convert this into a timezone string, you'll need to define a dictionary of timezones to match up with the time offsets. Since I think this is a many-to-one mapping for many time zones, I'm not sure how that would work.

Once you work out exactly what you need for your application, I suggest writing to the datetime author to add such a feature to that package.

Update. To get a crude lookup table for getting string output for the timezone, you can try something like this:

\makeatletter
\@namedef{timezone+0930}{CST}
\@namedef{timezone+1000}{EST}
\@namedef{timezone+1030}{CST'}% daylight savings
\def\grabtimezone #1#2#3#4#5#6#7#8#9{\grabtimezoneB}
\def\grabtimezoneB #1#2#3#4#5#6#7{\grabtimezoneC}
\def\grabtimezoneC #1#2'#3'{%
  \@ifundefined{timezone#1#2#3}
    {No timezone for `#1#2#3'}
    {\@nameuse{timezone#1#2#3}}%
}
\def\timezone{\expandafter\grabtimezone\pdfcreationdate}
\makeatother

\timezone
Related Question