# What is the XeTeX equivalent of \pdfcreationdate?

I need the time to 1-second resolution.

datetime uses \pdfcreationdate for this, but only under pdf(La)TeX.

As a result under XeLaTeX, this:

\usepackage{datetime}
...
\newtimeformat{hhmmss}{\twodigit{\THEHOUR}\twodigit{\THEMINUTE}\twodigit{\THESECOND}}
\settimeformat{hhmmss}
\currenttime


always produces 00 as the last two digits, eg:

113600

-
\pdfcreationdate is specific to pdfTeX (and LuaTeX); XeTeX has nothing of this kind, so it can use only the data provided by \time that contains only the minutes since midnight. –  egreg Jan 22 '12 at 11:45
@egreg Perhaps make your comment an answer. The only way I can think of doing this is using shell escape to get a timestamp from a file, but that is then system and shell escape status dependent. –  Joseph Wright Jan 22 '12 at 12:14
@egreg et al: Maybe I just move from XeTeX to LuaTeX? –  Brent.Longborough Jan 22 '12 at 12:29

There is no \pdfcreationdate command in XeTeX as in pdfTeX and LuaTeX. There is a technical reason, for this: the PDF file is produced by XeTeX in an asynchronous way, delivering the pages produced in xdv format to xdvipdfmx. In general, these files are kept in a temporary directory and the PDF is finally moved to the current directory at the end.

One could use the shell escape, checking the date of creation of the .log file, which is opened at the start of the job, but this would be system dependent.

## Emulate \pdfcreationdate in XeTeX

We can use LuaTeX also from XeTeX (or XeLaTeX)! Create the following file creationdate.lua:

os.remove("creationdate.timestamp")
io.output("creationdate.timestamp"):write(os.date("\\edef\\tempa\{\\string D:%Y%m%d%H%M%S\}\n\\def\\tempb\{%z\}"))\end{filecontents*}


Then put the following code before loading datetime:

\ifdefined\pdfcreationdate\else
\begingroup
\ifnum\shellescape=1
\immediate\write18{texlua creationdate.lua}
\input{creationdate.timestamp}
\def\tempc#1#2#3#4#5{#1#2#3'#4#5'}
\edef\tempb{\expandafter\tempc\tempb}
\edef\x{\endgroup\def\noexpand\pdfcreationdate{\tempa\tempb}}\x
\fi
\fi


The file creationdate.timestamp will be removed, if existent, and a new one created with the current time in it, in a format similar to that produced by \pdfcreationdate in pdfTeX. Then it will be read in; the time shown will be that of the execution of the script. However, any recent TeX distribution should have texlua (because it has luatex), so this should be as system independent as possible.

Actually, what's written in creationdate.timestamp is something like

\edef\tempa{\string D:20120122195802}
\def\tempb{+0100}


and then its contents is massaged a bit to get it into, essentially,

\def\pdfcreationdate{D:20120122195802+01'00'}


that's the format requested by datetime (the D must have category code 12, because such is the one produced by \pdfcreationdate in (pdf|Lua)TeX).

A complete example, to be run with --shell-escape, as always (otherwise \pdfcreationdate will not be defined and datetime will show seconds as "00"); the macros do nothing if \pdfcreationdate is already present (so using pdftex or luatex as engines).

\begin{filecontents*}{creationdate.lua}
os.remove("creationdate.timestamp")
io.output("creationdate.timestamp"):write(os.date("\\edef\\tempa\{\\string D:%Y%m%d%H%M%S\}\n\\def\\tempb\{%z\}"))
\end{filecontents*}
\documentclass{standalone}
\ifdefined\pdfcreationdate\else
\begingroup
\ifnum\shellescape=1
\immediate\write18{texlua creationdate.lua}
\input{creationdate.timestamp}
\def\tempc#1#2#3#4#5{#1#2#3'#4#5'}
\edef\tempb{\expandafter\tempc\tempb}
\edef\x{\endgroup\def\noexpand\pdfcreationdate{\tempa\tempb}}\x
\fi
\fi

\usepackage{datetime}
\newtimeformat{hhmmss}{\twodigit{\THEHOUR}\twodigit{\THEMINUTE}\twodigit{\THESECOND}}
\settimeformat{hhmmss}
\begin{document}
\currenttime
\end{document}


## An alternative way on Unix systems

Unfortunately, XeTeX doesn't sport the pdfTeX feature of \input|<command>, so an indirect way has to be taken, creating an auxiliary file to be read in.

• Create a shell script creationdate.sh
    #! /bin/sh
rm -f $1.timestamp date -r stat -f '%m' -t '%s'$1.log '+D:%Y%m%d%H%M%S+000000' > \$1.timestamp

• Give the file the executable bit
    chmod u+x creationdate.sh

• Put the following in the file before loading datetime

\usepackage{catchfile}
\ifdefined\pdfcreationdate\else
\ifnum\shellescape=1
\immediate\write18{./creationdate.sh \jobname}
\CatchFileDef\pdfcreationdate{\jobname.timestamp}{\catcode\D=12 }
\fi
\fi


Then your commands will give the required result. Of course one must check that stat and date accept the required command line options.

XeLaTeX must be run with the -shell-escape command line option, of course. If it isn't, then the behavior will be as without \pdfcreationdate available, that is, the seconds will always be "00". If the file is processed with pdfLaTeX or LuaLaTeX, this definition of \pdfcreationdate will be ignored and datetime will be happy nonetheless, as those engines already have \pdfcreationdate.

On Windows a suitable batch script can be devised.

-
Er, egreg, sorry to point this out, but datetime under LuaLaTeX works as is... But I've accepted your answer, as it does show the way for XeTeX. –  Brent.Longborough Jan 22 '12 at 16:25
@Brent.Longborough But this code is meant for XeLaTeX! I use luatex (better, texlua) only as an external command. –  egreg Jan 22 '12 at 16:28
Ah! Doh, sorry.. –  Brent.Longborough Jan 22 '12 at 19:27
@egreg: Yay! First LaTeX3, now Lua. I wish I could upvote it 10 times. :) –  Paulo Cereda Jan 23 '12 at 0:08

you can use lualatex or luatex instead, no problem:

\documentclass{article}
\begin{document}
Time: \directlua{tex.print(os.date("\%X"))}

\end{document}


luatex:

Time: \directlua{tex.print(os.date("\%X"))}
\bye


or without the colons: "\%H\%M\%S"`

-