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

This gives me path names and last modified times on my Unix server. How would I just fetch January 1,2000 and older files only?? Also I would rather just have the date print just as "Jul 22 2002" instead of "Mon Jul 22 14:55:04 2002" My output is:
HTML: /directory/path/dir/fil.html Mon Jul 22 14:55:04 200 +2 HTML: /directory/path/dir/df.html Mon Jul 22 15:04:58 2002 HTML: /directory/path/dir/fddd.html Thu Feb 28 13:44:54 2002 HTML: /directory/path/dir/fda.htm Thu Feb 28 13:42:49 200 +2
My script:
use strict; use File::Find; my $start = '/directory/path/dir'; find(\&process, $start); sub process { return unless /\.html?$/; print "HTML: $File::Find::name\t\t"; my($mtime) = (stat($_))[9]; print localtime($mtime) . "\n"; }

Replies are listed 'Best First'.
Re: Specific last modified times for html files
by joe++ (Friar) on Nov 06, 2002 at 16:23 UTC
    Hello,
    1. Compare $mtime to the epoch time for Jan 1st 2000 in your timezone
    2. use POSIX and see man strftime
    HTH,

    --
    Cheers, Joe

      Okay thanks now I have the dates looking good but how do I fetch dates older than 1/1/2000???? My new output:
      HTML: /directory/path/dir/fil.html 7/22/2002 HTML: /directory/path/dir/df.html 7/22/2002 HTML: /directory/path/dir/fddd.html 2/2/2000 HTML: /directory/path/dir/fda.htm 12/2/1999
      Here is my script so far:
      #!/usr/local/bin/perl #use strict; use File::Find; $start = '/directory/path/dir'; $ct = 0; find(\&process, $start); sub process { return unless /\.html?$/; $ct++; print "HTML: $File::Find::name\t\t"; $mtime = (stat($_))[9]; ($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yday, $isdst) = localtime($mtime); printf("%02d/%02d/%04d\n", $month+1, $day_of_month, $year+1900); } print "Tot = $ct\n";
        What about (untested...):
        sub process { my $mtime = (stat($_))[9]; # moved this line up return unless (/\.html?$/ && ($mtime < 946684800)); $ct++; print "HTML: $File::Find::name\t\t"; ($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yday, $isdst) = localtime($mtime); printf("%02d/%02d/%04d\n", $month+1, $day_of_month, $year+1900); } print "Tot = $ct\n";
        NOTE: 946684800 is the epoch time for Sat Jan 1 00:00:00 2000 GMT which I found thus:
        perl -MDate::Parse -e"print str2time('01 Jan 2000', 'GMT')"
        HTH!

        --
        Cheers, Joe

Re: Specific last modified times for html files
by meetraz (Hermit) on Nov 06, 2002 at 17:09 UTC
    When you print the localtime() function, the output is converted to a string scalar first, which is the in the standard format "Day Mon dd hh:mm::ss Year". To get the output you want, you should use the output as an array. See the documentation for the localtime() function for more info:

    use strict; use File::Find; my $start = '/directory/path/dir'; find(\&process, $start); sub process { return unless /\.html?$/; print "HTML: $File::Find::name\t\t"; my $mtime = (stat($_))[9]; my @timevars = localtime($mtime); my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov D +ec ); return unless ( (($timevars[5]+1900) < 2000) || ( #less than 2000 or (($timevars[5]+1900) == 2000) && #year is 2000 ($timevars[4] == 0) && # month is Jan ($timevars[3] == 1) ) # Day is 1 ); printf( "%s %02u %04u\n", $months[$timevars[4]], $timevars[3], $timevars[5]+1900, ); }
      Thanks, after looking over what you gave me it I now understand and got it to work. Sorry I am new to this and it took me a bit long to figure out what you did here. Thanks again!
Re: Specific last modified times for html files
by jdporter (Paladin) on Nov 06, 2002 at 17:23 UTC
    use Time::Local; use File::stat; my $cutoff = timelocal( 0, 0, 0, 1, 0, 2000 ); # 2000-01-01 sub process { my $mtime = stat($file)->mtime; $mtime <= $cutoff or return; local $, = " "; local $\ = "\n"; print( (split ' ', localtime($mtime))[1,2,4] ); }
      ah, yes... ++jdporter, I forgot about timelocal() !