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

Everyone,
Ok, I have been working on this for a few days now and I am confused. I'm able to get the epoch time from stat and determine which file is newer. Yet, when I use localtime to try and find the DMYHMS format to the same problem, I get nothing but errors. Can someone please help!

To give you some background. I have a remote server on the otherside of the world, I need to check the date on a file on the remote server, if the date is older then the one on my local server, copy the new file from the local server to the remote server.

I want to do all this by comparing either the date modified or the date changed that is time stamped onto the file.

Now here is the code for the stat. Which gets the epoch time and divides by 60,60,60,24,7 to get the time in epoch seconds since 1970.

#!/usr/bin/perl -w use File::Copy; $filename1 = "c:/progra~1/imms"; #file on the client (win +95/98). $filename2 = "//bmc_1/imms_stock/imms_exe/imms.exe"; #file on serve +r. @file1 = stat($filename1); #print "$file1[10]\n"; @file2 = stat($filename2); #print "$file2[10]\n"; $days1 = ((((($file1[10] /60) /60) /60) /24) /7); #print "$days1\n +"; $days2 = ((((($file2[10] /60) /60) /60) /24) /7); #print "$days2\n +"; if ($days1 < $days2) { print "$filename1 is old\n"; copy ($filename2, $filename1) or die "copy failed: $!\n"; print "$filename2 updated!\n"; }else{ print "$filename2 is up to date!\n"; print "No files copied!\n"; }

What I need is to break the epoch seconds down by using the localtime function. I have been looking at both the non-module way and the module way of doing this. I feel that I need to know the non-module way first. Especially for this project.

Here is what I have so far.

$filename1 = "u:/curtisbl/documents/conditional.sql.txt"; ($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yda +y, $isdst) = localtime($filename1); printf ("Time: %02d-%02d-%04d\n", $month+1, $day, $year+1900);

Could someone help please?

Curtisb

"Confused Dated Person"

Replies are listed 'Best First'.
Re: stat and localtime
by davorg (Chancellor) on Dec 20, 2001 at 18:17 UTC

    You seem to be passing a filename to localtime which probably isn't what you wanted to do.

    Perhaps you could try:

    my ($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yday, $isdst) = localtime($file1[10]);

    or even

    my ($day_of_month, $month, $year) = (localtime($file1[10]))[3..5];
    --
    <http://www.dave.org.uk>

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

Re: stat and localtime
by grinder (Bishop) on Dec 20, 2001 at 19:33 UTC
    What's with the heavy mathematics? Why don't you just compare epoch times and be done with it.
    $filename1 = "c:/progra~1/imms"; #file on the client (win +95/98). $filename2 = "//bmc_1/imms_stock/imms_exe/imms.exe"; #file on serve +r. if ((stat($filename1))[10] < (stat($filename2))[10]) { print "$filename1 is old\n"; copy ($filename2, $filename1) or die "copy failed: $!\n"; print "$filename2 updated!\n"; }else{ print "$filename2 is up to date!\n"; print "No files copied!\n"; }

    Of course, that's a little obfuscated. I would tend to write that as

    sub file_age { (stat($_[0]))[10]; # return the epoch seconds since the file was cr +eated } $filename1 = "c:/progra~1/imms"; #file on the client (win +95/98). $filename2 = "//bmc_1/imms_stock/imms_exe/imms.exe"; #file on serve +r. if (file_age($filename1) < file_age($filename2)) { print "$filename1 is old\n"; copy ($filename2, $filename1) or die "copy failed: $!\n"; print "$filename2 updated!\n"; }else{ print "$filename2 is up to date!\n"; print "No files copied!\n"; }
    --
    g r i n d e r
    just another bofh

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';
Re: stat and localtime
by Juerd (Abbot) on Dec 20, 2001 at 18:18 UTC
    Use time(), which returns the number of seconds since the epoch (Jan 1, 1970, 0:00 GMT)
    Hope it helps.

    P.S. You don't need all those parens:
    $days1 = $file1[10] / 60 / 60 / 60 / 24 / 7;
    Hmm. I know there are 60 seconds in a minute, and 60 minutes in an hour, 24 hours in a day and seven days per week. What's the third 60 for?

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      Epoch's not always January 1st, 1980. On Macintosh it's January 1st, 1904...
      No, I don't care to explain why :)

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
Re: stat and localtime
by lordsuess (Scribe) on Dec 20, 2001 at 23:19 UTC
    Beware the month's count starting at 0, and the year 0 = 1900
    my ($seconds, $minutes, $hours, $day, $month, $year) = (localtime($fil +e))[0..5]; $month++; # starts counting with 0 $year += 1900; # years since 1900
Re: stat and localtime
by curtisb (Monk) on Dec 20, 2001 at 19:57 UTC

    Everyone,

    Thanks for your help. Here is what I have finished. Yes, I'm sure there is a way to improve on this. If anyone has any ideas let me know. But, this works.
    Thanks.

    #!/usr/bin/perl -w use File::Copy; $client = "<any drive mapping to a file, either locally or network>"; $server = "<any drive mapping to a file, either locally or network>"; + @client = stat($client); @server = stat($server); ($sec, $min, $hours, $mday, $month, $year) = (localtime($client[9]))[0 +..5]; printf ("Modify date and time on $client is: %02d-%02d-%04d %02d:%02d: +%02d\n", $month+1, $mday, $year+1900, $hours, $min, $sec); ($sec, $min, $hours, $mday, $month, $year) = (localtime($server[9]))[0 +..5]; printf ("Modify date and time on $server is: %02d-%02d-%04d %02d:%02d: +%02d\n\n", $month+1, $mday, $year+1900, $hours, $min, $sec); if ($client[9] < $server[9]) { print "$client is old!\n"; print "Copying new file!\n"; copy ($server, $client) or die "copy failed: $!\n"; print "$client updated!\n"; }else{ print "$client is up to date\n"; print "No files copied!\n"; }

    Thanks again for the help! Everyone out here is wonderful!

    Curtib

    has a small clue now.....