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.

7
  • 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
    Commented Feb 28, 2011 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. Commented Feb 28, 2011 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
    Commented Feb 28, 2011 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
    Commented Feb 28, 2011 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. Commented Mar 1, 2011 at 11:57

5 Answers 5

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.

5
  • 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. Commented Feb 28, 2011 at 15:03
  • Yes this is the sort of answer I was thinking of. Thanks to Martin as well.
    – lcv
    Commented Feb 28, 2011 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. Commented Mar 1, 2011 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. Commented Mar 1, 2011 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. Commented Mar 1, 2011 at 12:40
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}
2
  • I did not know about this. However, when I look for it in the pdftex documentation, I think you meant \pdffilemoddate, not \pdfcreationdate. Commented Feb 28, 2011 at 14:29
  • @Harald: Yes, you are right. I was under the impression that both are taken a file name as argument, which isn't the case. Thanks! Commented Feb 28, 2011 at 14:52
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.

1

There is 'getfiledate' package on CTAN and TeXLive.

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

1
  • 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
    Commented Mar 8, 2013 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.

2
  • See the bottom of Martin Scharrer's answer; he's the package author :-). Commented Mar 8, 2013 at 16:33
  • @HendrikVogt: Now at the top of my answer. Thanks for notifying me. Commented Mar 8, 2013 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .