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

I've a basic question regarding timestamps. In my program I need to enter a condition if a file is at least 20 minutes old. I' m currently using this:
if (abs ((stat("/tmp/file"))[9] - time) < 1200) { more code }
Is this a correct approach? Is it the best way when using perl? Thanks for any help...

Replies are listed 'Best First'.
Re: Timestamp conditional
by friedo (Prior) on Jul 27, 2006 at 17:33 UTC
    You may want to check out File::stat which provides a nicer interface to the stat function.
Re: Timestamp conditional
by derby (Abbot) on Jul 27, 2006 at 17:41 UTC

    This sounds like an XY Problem. The code you've given (besides the funky use of abs - I would just reverse the time and stat call), does not really check if the file is at least 20 minutes old - just that it was last modified at least 20 minutes ago. Given the general wonkiness of system clocks and finicky filesystems (nfs), this sounds like a recipe for a long night of sleepless debugging.

    In the past, I've seen appoaches like this for trying to determine if some file has finished processing (ala ftp'ing a file). The general consensus in that regard is to always process under one file name convention (.file or file.part) and when done, rename it to something your follow on process looks for (file or file.done).

    But then again ... maybe I'm just reading too much into the OP.

    -derby
Re: Timestamp conditional
by sgifford (Prior) on Jul 27, 2006 at 19:58 UTC
    That's basically right. The -M function may make this a little easier, since it does the subtraction for you. The units are in days, so you have to figure out what fraction of a day 20 minutes is. Here's an example:
    if (-M '/tmp/file' >= 20/60/24) { # ... }
    As others have mentioned, this will tell you whether the file has been unmodified for the last 20 minutes, which may or may not be exactly what you want.
Re: Timestamp conditional
by rodion (Chaplain) on Jul 27, 2006 at 20:01 UTC
    I usually use the simpler
    if ((-M '/tmp/file') > $ageThreshold) {
    to test the modify time. (or -A or -C for the access or change time on unix). See the documentation for file tests.

    Two things to note

    • The time is given in days, not hours or minutes.
    • The time is from the start of the script. That cuts overhead if you're checking more than one file, but it's a problem if you're waiting for the file to be old enough

    Ususally, neither of these is a problem.

Re: Timestamp conditional
by davorg (Chancellor) on Jul 28, 2006 at 07:59 UTC

    This is quick and dirty:

    # 1/72 is 20 minutes as a fraction of a day if (-M '/tmp/file' > (1/72)) { # do stuff }

    -M returns the age in days of the file, but relative to the time that your program started. Therefore this works if your program is pretty fast and you're not too fussed about the accuracy of the 20 minutes. If that's not true then you'll also need to throw the value of $^T into the equation.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg