ewhitt has asked for the wisdom of the Perl Monks concerning the following question:

Could someone please recommend a clever way to embed a time & data stamp into a filename to compare? I can't rely on the file time and date stamp for my application. I have been using the following code, but don't know of an easy way to compare which is newer.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); printf "%s-%4d-%02d-%02d-%02d:%02d:%02d\n",$0,$year+1900,$mon+1,$mday, +$hour,$min,$sec; e.g. test.pl-2007-11-10-07:09:52 test.pl-2007-11-10-07:11:53 test.pl-2006-11-10-05:05:53
Does anyone know of an approach / module that may help? Thanks!

Replies are listed 'Best First'.
Re: Comparing time and date stamps
by Corion (Patriarch) on Nov 10, 2007 at 15:24 UTC

    See POSIX::strftime :

    use POSIX qw(strftime); print strftime '%Y-%m-%d-%H-%M-%S', localtime;

    Also be aware that not all filesystems support a ":" in the filename and many Unix utilities use the ":" as a delimiter in lists of directories which may lead to unexpected failures.

Re: Comparing time and date stamps
by FunkyMonk (Bishop) on Nov 10, 2007 at 15:35 UTC
    As long as you stick to the order most-significant to least-significant in your timestamp (as you are already doing), you can just use gt, Perl's normal string greater-than.

    print "$file1 is newer than $file2\n" if $file1 gt $file2;

    Update: Brainfart time. Replaced cmp with gt

Re: Comparing time and date stamps
by dwm042 (Priest) on Nov 10, 2007 at 16:42 UTC
    I'll suggest a couple possibilities:

    1. Embed the results of time() into the file name. It's an integer, so comparisons are simplified.

    2. Use the age function and let it decide how old your files are:

    # # the 'magic' of -x functions in Perl # -s gives size of $file in bytes, -M gives age in days. # my $size =-s $_; my $age =-M $_; $size /= 1048576.0; $total += $size;
Re: Comparing time and date stamps
by GrandFather (Saint) on Nov 10, 2007 at 22:51 UTC

    Your current time and date stamp are fine with a little manipulation. First you can drop all to punctuation the to leave just the digits. So 2007-11-10-07:09:52 becomes 20071110070952, not as nice to the eye, but OS safe, a little more compact, easy to parse and easy to compare.

    Tacking the stamp onto the end of the file extension is somewhat sub optimum. Better to tack it onto the file name part: test_20071110070952.pl where it doesn't hide the extension in the middle of a large file name.

    Having done all that you need to be able to isolate the stamp to make it easy to compare. A simple regex does that:

    my ($timestamp) = $filename =~ /_(\d{14})\b/;

    Then a simple cmp, gt, lt, ge, le, eq or ne between the two timestamps does the job.


    Perl is environmentally friendly - it saves trees
Re: Comparing time and date stamps
by Sixtease (Friar) on Nov 10, 2007 at 15:49 UTC
    This reminds me of me writing an archive script to make backups of older versions of my programs. My actions were driven by unawareness of CVS programs, like Subversion.