11

When I write my notes, (I actually use lyx but that does not matter) I like to add a header with the name of file and the date the file was compiled. This I do using fancyhdr package and

\pagestyle{fancy}
\lhead{\texttt{\jobname}}
\rhead{\textsf{\today}}

(to tell the truth in lyx I have to use \jobname.lyx, but, again, this does not matter).

The point is that it would be much more useful (at least for me) to be able to print the date when the file was first created. Is there a way in TeX/LaTeX to access the time information of a latex file? I can understand the answer might be OS dependent. I am actually using a linux distribution. I looked in many other forums/FAQ but could not find the answer so far.

Martin Scharrer
  • 262,582
lcv
  • 275
  • Welcome to tex.sx! Note that you don't have to (and shouldn't) sign with your name since it automatically appears in the lower right corner of your post – Seamus Feb 28 '11 at 12:35
  • 8
    On linux, there is no such thing as the date the file was first created. There are three timestamps on a file, known as mtime, atime and ctime. Here mtime is the time of last modification, atime is the time of last access, and ctime has a more technical definition that is irrelevant here. – Harald Hanche-Olsen Feb 28 '11 at 12:41
  • Oops! You are right Harald, I na"ively thought ctime was creation time. Your observation preatty much nullifys my question. But I did find all the answer useful, I am sorry I can not vote them up. – lcv Feb 28 '11 at 14:37
  • Googling tells me that there is a "birthtime" and the stat command line utility under linux can display that time. Can't verify that, for I don't have a linux box right here. – topskip Feb 28 '11 at 19:32
  • @Patrick: Google is hardly an authority here. Both man pages for stat (ch 1 for the command, ch 2 for the system call) confirm that there are only the three time stamps I mentioned above. – Harald Hanche-Olsen Mar 01 '11 at 11:57
  • @Harald: google is just a search engine for other web pages, and therefore never an authority. As I said, I couldn't verify. I am pretty sure that there are linux systems out there that support btime/birthtime, but probably not from a default distribution. So that doesn't help lcv and is offtopic here anyway, so there is no need to discuss this any further. – topskip Mar 01 '11 at 12:22
  • @Patrick: In principle, the ext4 filesystem supports creation time, but I don't think this is already implemented in the system calls. – Hendrik Vogt Mar 02 '11 at 09:20

5 Answers5

11

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 .

\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}
Werner
  • 603,163
Martin Scharrer
  • 262,582
7

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.

  • Martin's answer surely makes more sense than this one. But I leave my answer here in case someone could use the technique for something related. – Harald Hanche-Olsen Feb 28 '11 at 15:03
  • Yes this is the sort of answer I was thinking of. Thanks to Martin as well. – lcv Feb 28 '11 at 16:59
  • 1
    @Icv: My solution turned out to only return the file modification date, not the creation date as I thought at the beginning. So Harald's answer is the correct one. Note that you can write a % using \@percentchar which saves you the trouble of changing the catcode. – Martin Scharrer Mar 01 '11 at 11:26
  • @Martin: No, my solution too gives modification date. Linux doesn't keep track of any creation date, so there is no way to get it. – Harald Hanche-Olsen Mar 01 '11 at 11:59
  • Yes true, but under Windows such an approach should work and return the creation time. The difficulty might be to find a stat program under Windows. – Martin Scharrer Mar 01 '11 at 12:40
2

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.

Heather
  • 836
1

There is 'getfiledate' package on CTAN and TeXLive.

http://tug.ctan.org/tex-archive/macros/latex/contrib/getfiledate

Ahmed Musa
  • 11,742
  • how can I use this stuff to just get the date, without any extra boxes, newlines, etc? I tried \getfiledate[notime,datecolor=black,putprefix=false]{animalEstimation.tex} but it creates some nasty new lines (this is inside the \date{} tag) – Grzenio Mar 08 '13 at 15:48
0

If you are after the modification date, then the package filemod seems to do exactly what you need. To get the last modified date of the file you just include the package in the usual way:

\usepackage{filemod}

and the modification time of the current document is printed by:

\filemodprintdate{\jobname}

you can also print the modification time, and there are many options to format the output.

Grzenio
  • 9,465