# Write date, time, and time zone

## 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!

-

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

-
will not work if you are minus UTC time i.e., plus sign needs to be -! –  Yiannis Lazarides Jan 10 '11 at 7:54
@Yiannis good point. thanks; I've updated the answer. –  Will Robertson Jan 10 '11 at 8:06
excellent solution! –  Yiannis Lazarides Jan 10 '11 at 8:10

There is now the filemod package which can return the file modification date of any file (which can be found by TeX). It is based on the \pdffilemoddate macro. It works with pdf(La)TeX and Lua(La)TeX, but not with Xe(La)TeX.

If you want the time when the main file was last changed use \jobname as filename in the macros below. If you really want the compile time you can use the macros with a trick:

\edef\pdfilemoddate#1{\pdfcreationdate}


and then keep the {<filename>} argument empty.

You can print, i.e. typeset the date, time and timezone using

\filemodprint{<filename>}


Only the date or time+timezone are printed with

\filemodprintdate{<filename>}
\filemodprinttime{<filename>}


They use the \thefilemoddate and \thefilemodtime to format the result. These macro get {<YYYY>}{<MM>}{<DD>} or {<HH>}{<mm>}{<SS>}{<TZ>} as arguments, respectively. You can redefine these to print the date or time in a custom format. There is also \thefilemod for \filemodprint which gets all seven arguments and by default calls the other two macros.

You can also create your own macro with seven arguments and use:

\filemodparse{\mymacro}{<filename>}


Then the timezone would be #7`.

-