Date of file creation

Update: In the meantime I published the filemod package which can display and compare file modification dates. With this the modification date of the main file can be printed using \filemodprint{\jobname}.


pdfTeX provides the expandable primitive \pdffilemoddate which takes the file name in question as argument:

\pdffilemoddate{filename.tex}

or

\pdffilemoddate{\jobname.tex}

This works with all files, not just PDFs like the name might suggest. It expands to the file modification date. The format is like D:20110228133815Z, which then can be parsed using a macro. Note that all characters of this string have the catcode other not letter.

The following code defines a macro which reads every number of the returned string an finally passes it to a format macro which can be freely redefined. For help with formatting the date see this question or other questions tagged with datetime.

\newcommand*{\filedate}[1]{%
    \expandafter\filedateX\pdffilemoddate{#1}\relax
}
\def\filedateX#1#2#3#4#5#6#7#8{%
    \filedateXX{#3#4#5#6}{#7#8}
}
\def\filedateXX#1#2#3#4#5#6#7#8{%
    \filedateXXX{#1}{#2}{#3#4}{#5#6}{#7#8}%
}
\def\filedateXXX#1#2#3#4#5#6#7#8\relax{%
    \formatdate{#1}{#2}{#3}{#4}{#5}{#6#7}%
}

\newcommand*{\formatdate}[6]{%
   #1-#2-#3\ #4:#5:#6%
}

\filedate{\jobname.tex}

This will work on linux, with \write18 enabled; i.e., run using pdftex --shell-escape:

{\catcode`\%=12
\immediate\write18
 {/usr/bin/stat --format '\string\foo\space %yZ' \jobname.tex >\jobname.date}
 \def\foo#1 #2Z{\gdef\filedate{#1}}
 \input\jobname.date
}
The file is dated \filedate.
\bye

As I state in a comment above, you cannot get the file creation date, though. This gives you the file modification date instead.


For every document you write, the creation date is not going to change, by definition.

This means that it is perfectly acceptable to hard code the date in. For example,

\rhead{\textsf{28\textsuperscript{th} Febuary, 2011}}

or formatted however you want. This would keep the date the creation date, because it doesn't need to dynamically change every time you compile the document.

If I have misunderstood what you want to do, let me know.

Tags:

Datetime