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

I'm trying to do a date/time comparison between a timestamp of a file against the current time.

I've used the command $lockfile_epoch = (stat($file))[9]; to get the timestamp of the files in seconds since epoch.

And used the command $current_epoch = time(); to get the current system time.

Used those commands and brought back the right time in seconds when I ran it as a perl script on the cywin installation on my local machine but when I tried to run it in a CGI on a unix box, the time stamps are not close even when I have touch the file ust before executing the CGI. There seems to be an offset of 77000-78000 seconds.

Thanks for any help or info

Edit by tye

Replies are listed 'Best First'.
Re: Unix date/time variance
by pepik_knize (Scribe) on May 22, 2002 at 05:33 UTC
    Hmm, very interesting. Which unix platform is giving you problems? It worked fine on linux and solaris for me. If I had to guess, I'd say that maybe your hardware clock is not in synch with your system time. Another option is to use the  -M file test. It returns the value you're looking for in days. Multiplying that by 24, 60, and then 60 again brings it back to seconds. See below, though, because the two methods don't agree. Well, see if this helps anyway:
    #!/usr/bin/perl -w use strict; system ("ls"); print "file? "; chomp (my $file = <>); my $lockfile_epoch = (stat($file))[9]; my $current_epoch = time(); my $sttime = $current_epoch - $lockfile_epoch; # using stat print "$sttime\n"; my $fttime = (-M $file)*24*3600; # using file test print "$fttime\n";

    Pepik

    Of all the causes that conspire to blind
    Man's erring judgment, and misguide the mind,
    What the weak head with strongest bias rules,
    Is pride, the never-failing vice of fools.
    -- Pope.
    (Humble to be American.)