13

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
  • 3
    @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
  • 1
    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
12

enter image description here

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:-)

enter image description here

\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
\advance\tepoch\numexpr\count2*24*60*60\relax

% extra whatever for each whole month
\ifnum\month>1 \advance\tepoch\numexpr 31*24*60*60\relax \fi

\ifnum\numexpr4*\count2\relax=\count0
  \ifnum\month>2 \advance\tepoch\numexpr 29*24*60*60\relax \fi
\else
  \ifnum\month>2 \advance\tepoch\numexpr 28*24*60*60\relax \fi
\fi

\ifnum\month>3 \advance\tepoch\numexpr 31*24*60*60\relax \fi
\ifnum\month>4 \advance\tepoch\numexpr 30*24*60*60\relax \fi
\ifnum\month>5 \advance\tepoch\numexpr 31*24*60*60\relax \fi
\ifnum\month>6 \advance\tepoch\numexpr 30*24*60*60\relax \fi
\ifnum\month>7 \advance\tepoch\numexpr 31*24*60*60\relax \fi
\ifnum\month>8 \advance\tepoch\numexpr 31*24*60*60\relax \fi
\ifnum\month>9 \advance\tepoch\numexpr 30*24*60*60\relax \fi
\ifnum\month>10 \advance\tepoch\numexpr 31*24*60*60\relax \fi
\ifnum\month>11 \advance\tepoch\numexpr 30*24*60*60\relax \fi

% seconds from previous days in this month
\advance\tepoch\numexpr (\day-1)*24*60*60\relax

% seconds from today
\advance\tepoch\numexpr \time*60\relax


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
  • 4
    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
7

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}

enter image description here

6

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.


enter image description here


2

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
  • 1
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.