# UNIX timestamp (= seconds since 1/1/1970) in document?

I would like to include a so-called UNIX timestamp in my document. All technicalities aside, it's basically the number of seconds since 1/1/1970 0:00 UTC aka "the epoch". (At the time of me writing this, it's 1385812869.)

If have not found a way to include that number in my document. Unless I have overlooked something, the obvious suspects (datetime, isodate, scrtime) seem not to provide a ready-made solution.

• What is the purpose of this number? What we can do with it? – Sigur Nov 30 '13 at 13:44
• @Sigur: It's the UNIX way of keeping time, allowing you to easily and unambiguously specify a point in time, down to the second, without having to worry about DST, time zones, etc. etc. But surely this is beyond the scope of this forum? – Ingmar Nov 30 '13 at 13:49
• Thanks. I'm reading about it but I can not imagine how to use it. But now, I have some ideas. – Sigur Nov 30 '13 at 13:51
• @Ingmar: you wish! It has leap seconds. – Kos Nov 30 '13 at 15:18
• I said "all technicalities aside", didn't I? Also, we're talking about 25 or so leap seconds since 1972. I can live with that. – Ingmar Nov 30 '13 at 21:12

pdflatex \\def\\epoch{date +%s}\\input ep.tex


the exact form of quoting on the command line depends on your shell (this is bash)

where ep.tex is

\documentclass{article}

\begin{document}

now = \epoch.

\end{document}


If you want to calculate it portably in classic TeX (actually I used etex, arithmetic but that could be easily removed) you can do this (which actually is exactly 1 day out in my system, so you could either fix my arithmetic or just add a day at the start:-)

\documentclass{article}

\begin{document}

now1 = \epoch.
\newcount\tepoch

\count0=\numexpr\year-1970\relax
\tepoch=\numexpr\count0*365*24*60*60\relax

\count2=\count0
\divide\count2 4

%extra day for each leap year (avoid complication of mod 100 and mod 400, so we are good until 2100

% extra whatever for each whole month

\ifnum\numexpr4*\count2\relax=\count0
\else
\fi

% seconds from previous days in this month

% seconds from today

now2 = \the\tepoch.

diff = \the\numexpr(\epoch-\tepoch)/(60*60)\relax\ hours

\end{document}

• Yes, a system command seems like an obvious answer. Probably won't work across environments, though, will it? Will have to test this with Windows. – Ingmar Nov 30 '13 at 12:10
• @Ingmar well I ran that on windows (using cygwin) but yes using a system call does of course introduce system dependencies. – David Carlisle Nov 30 '13 at 12:12
• Its an ingenious solution to a problem that should never have existed in the first place. [James May] – Pouya Nov 30 '13 at 12:22
• A platform independent solution might exist via lualatex. I'm no expert of luatex but I'm sure there should be some lua functions for date/time conversions. – Pouya Nov 30 '13 at 12:25
• @Pouya You could calculate it in TeX (from \year \month \day and \time and a macro holding information about any leap seconds, it's just easier not to bother:-) – David Carlisle Nov 30 '13 at 12:36

This works with pdflatex and lualatex, with --shell-escape; of course it's system dependent and will work only on Unix systems.

\documentclass{article}
\usepackage{catchfile}
\makeatletter
\CatchFileEdef{\epoch}{|"date -j +\@percentchar s"}{\endlinechar=\m@ne}
\CatchFileEdef{\timestamp}{|"date -j"}{\endlinechar=\m@ne}
\makeatother
\begin{document}
\epoch

\timestamp

\pdfcreationdate % this is available also without shell escape
\end{document}


Apart from interesting answer by David and his suggestion about calculating it manually, here is my solution based on luatex. Please note that this is just a guideline and the code is neither clean nor complete by any means. There are lots of rooms for improvements!

\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
function epoch (format,time)
if format == 1 then
fmt = "%c"
elseif format == 2 then
fmt = "%A"
elseif format == 3 then
fmt = "%B"
elseif format == 4 then
fmt = "%X"
else
fmt = "%x"
end
tex.sprint(os.date(fmt, time))
end
\end{luacode*}

\newcommand\epoch[2]{\directlua{epoch(#1,#2)}}

\begin{document}
This is a test. We now check the time stamp for 1385817087
\begin{itemize}
\item \%c \epoch{1}{1385817087}
\item \%A \epoch{2}{1385817087}
\item \%B \epoch{3}{1385817087}
\item \%X \epoch{4}{1385817087}
\end{itemize}
\end{document}


You have to compile this by lualatex myfile.tex. Other options for using lua function os.date can be found here.

To supplement that part of David Carlisle's answer which uses the POSIX date utility, here is a way to get the epoch on Windows.

On Windows the "epoch time" concept not only implemented in Unix-like environments and their utilities, but also in the standard C library functions implemented in the Microsoft Visual C Library.

That is to say, Microsoft's <time.h> material is based on a Unix-like time_t whose units are seconds, and which measures since the Epoch. This is not required by ISO C, but it helps port code which assumes that representation.

So you can make yourself some trimmed down epoch.exe with Visual C that prints the time.

I just compiled this code with VS2008 into a Win32 console application.

#include <stdio.h>
#include <time.h>

int main(void)
{
printf("%ld\n", (long) time(0));
return 0;
}


The statically-linked (no dependency on a msvcrt.dll) executable (156 kB) is here.

• Did you not notice which stackexchange this is? Your answer isn't exactly TeX-relevant. – Random832 Nov 30 '13 at 20:09
• @Random832 Some other answers give a solution based on running the POSIX date utility and interpolating the result into the TeX. OP was concerned that it won't work on Windows, which doesn't have the date utility, unless some form of Unix emulation layer is installed. This is a possible way to address that issue without installing a whole environment. I clarified this in the answer now. – Kaz Nov 30 '13 at 20:46