Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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 1 '11 at 11:57

5 Answers 5

up vote 5 down vote accepted

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.

share|improve this answer
    
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 1 '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 1 '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 1 '11 at 12:40

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}
share|improve this answer
    
I did not know about this. However, when I look for it in the pdftex documentation, I think you meant \pdffilemoddate, not \pdfcreationdate. –  Harald Hanche-Olsen Feb 28 '11 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! –  Martin Scharrer Feb 28 '11 at 14:52

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.

share|improve this answer

There is 'getfiledate' package on CTAN and TeXLive.

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

share|improve this answer
    
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 8 '13 at 15:48

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.

share|improve this answer
    
See the bottom of Martin Scharrer's answer; he's the package author :-). –  Hendrik Vogt Mar 8 '13 at 16:33
    
@HendrikVogt: Now at the top of my answer. Thanks for notifying me. –  Martin Scharrer Mar 8 '13 at 18:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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