What is the XeTeX equivalent of \pdfcreationdate?
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.
Update July 2016
With the new texosquery
script, the code can be simplified:
\documentclass{standalone}
\usepackage{luatex85}
\ifdefined\pdfcreationdate\else
\usepackage{texosquery}
\TeXOSQueryNow{\pdfcreationdate}
\edef\pdfcreationdate{\detokenize\expandafter{\pdfcreationdate}}
\fi
\usepackage{datetime}
\newtimeformat{hhmmss}{\twodigit{\THEHOUR}\twodigit{\THEMINUTE}\twodigit{\THESECOND}}
\settimeformat{hhmmss}
\begin{document}
\currenttime
\end{document}
I added luatex85
in case LuaLaTeX is used. In this way, -shell-escape
is only needed for XeLaTeX. The \edef
with \detokenize
is necessary, because \TeXOSQueryNow
returns the starting D
with category code “letter” and the macros in datetime
assume category code “other” instead.
Note: the Java application, at the moment, seems only to run with Java 1.7.0_52 or later, but I was successful in recompiling it on 1.7.0_45. It would be nice if the application was made usable also with previous versions of Java.
you can use lualatex
or luatex
instead, no problem:
\documentclass{article}
\usepackage{luacode}
\begin{document}
Time: \luaexec{tex.print(os.date("\%X"))}
\end{document}
luatex
:
Time: \directlua{tex.print(os.date("\%X"))}
\bye
or without the colons: "\%H\%M\%S"
. All optional arguments:
%a abbreviated weekday name (e.g., Wed)
%A full weekday name (e.g., Wednesday)
%b abbreviated month name (e.g., Sep)
%B full month name (e.g., September)
%c date and time (e.g., 09/16/98 23:48:10)
%d day of the month (16) [01-31]
%H hour, using a 24-hour clock (23) [00-23]
%I hour, using a 12-hour clock (11) [01-12]
%M minute (48) [00-59]
%m month (09) [01-12]
%p either "am" or "pm" (pm)
%S second (10) [00-61]
%w weekday (3) [0-6 = Sunday-Saturday]
%x date (e.g., 09/16/98)
%X time (e.g., 23:48:10)
%Y full year (1998)
%y two-digit year (98) [00-99]
%% the character `%´