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

Hi everybody,
I need to find a difference between file creation date and
today.If it's more then 45 days- this file should
be moved,if not- I'll continue to write to it.
Here what I'm using:
..... my $create_date = (stat(MLOG))[10]; $creat = scalar localtime $creat; # create_date=Thu Apr 14 16:26:14 2005 $today= localtime time; #today= same format ...
I need to find a diff between $create_date and $today.
I'm not allowed to install Date::Calc or anything wich is
not standard for Perl initial installation.
Thank you for your help

Replies are listed 'Best First'.
Re: Dates difference
by Corion (Patriarch) on May 23, 2005 at 20:35 UTC

    You don't need to do the calculation yourself, see perldoc -f -X (or perlfunc):

    if (-C $file > 45) { print "$file is too old.\n"; };
      Bear in mind that for a long running script this solution will fail. The file test compares to start of script run time.
Re: Dates difference
by dave_the_m (Monsignor) on May 23, 2005 at 22:27 UTC
    UNIX filesystems do not record the creation time of a file.

    (stat)[10] and -C are (on UNIX) the inode change time, which roughly equates to the last time the file's size or permissions changed.

    Dave.

Re: Dates difference
by jeffa (Bishop) on May 23, 2005 at 20:35 UTC

    I had to do that recently.

    use Time::Local; my $now = timelocal(localtime); my $mtime = (stat $file)[9]; my $delta = ($now - $mtime) / (60 * 60 * 24);
    FWIW ... i wrote some Date::Calc code and decided to use the above code instead.

    UPDATE: Corion to the rescue again. :) (That means i learned something too. Yes ... -C works fine, in fact, it works better than fine. I just wish i knew about that last week when i wasted 10 minutes writing that snippet. ;))

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thank you. -C $file works fine
Re: Dates difference
by mikeraz (Friar) on May 23, 2005 at 20:51 UTC

    Is that 45 days from this instant, from midnight last night or another point in the time that makes up a day?

    The time function returns the number of seconds since the epoch. Fortunately the stat function you invoke does the same. So all you need to do is subtract the create date from now and see if it's greater than 45 days of seconds. So,

    my $create_date = (stat(MLOG))10;
    my $now = time;
    
    # 45 days of seconds is 60*60*24*45 or 3888000
    
    if(($now - $create_date) > 3888000) { #do whatever }
    
    

    I'll leave the issue of needing to calculate the difference between last mid-night, noon, close of business or whatever erference point in the day as an exercise for the reader.