#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    London, England
    Posts
    251
    Rep Power
    15
    I want to be able to read the timestamp on a file from Perl.

    I'm writing a CGI script to run from a SSI that indicates whether a webcam is on or not. So I'm going to do this by checking the timestamp of the last uploaded file and seeing whether it's recent enough.

    This sounds simple enough but I have read and read and can't find the answer.

    Any help greatly appreciated.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Charleston, SC, USA
    Posts
    10
    Rep Power
    0
    try 'stat'...... like

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    @details = stat aTextFile;
    $modifyTime = $details[9];
    [/code]


    'hope this helps
  4. #3
  5. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>try 'stat'...... like

    This would be the easiest and cleanest. If you want to break the date down to a few pieces, try this instead -> http://www.devshed.com/Talk/Forums/F...ML/000254.html
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    London, England
    Posts
    251
    Rep Power
    15
    Thanks for all your suggestions.

    In the end I used -M to determine the relative time in days since the file was last modified. I did this because as I only want to know whether the file is more than a certain amount old it's easier to work with a value that's already relative rather than make a calculation based on the current time.

    Here's my code that I embed from a server side include:

    #!/usr/bin/perl

    # Checks whether a file has been recently updated
    # used to determine whether a webcam is "on"

    $FILENAME = 'livepic.jpg'; # name of file you want to check
    $REFRESH = 300; # camera refresh in seconds (eg. 5 minutes)

    $ON_TEXT = "<p>The camera is <b>on</b>.";
    $OFF_TEXT = "<p>The camera is <b>off</b>.";

    $SECOND = 1 / 24 / 60 / 60; # calculate 1 second as a fraction of 1 day

    print "Content-type: text/htmlnn";

    if ( (-M $FILENAME) > $REFRESH * $SECOND ) {
    print "$OFF_TEXT";
    } else {
    print "$ON_TEXT";
    }

    exit;

IMN logo majestic logo threadwatch logo seochat tools logo