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

Hi all, I'm writing a script that is supposed to keep the most recent seven days of a log file and discard the rest. The only problem I'm facing now is that I need it to handle end of the month conversions from say, March 31st to April 1st. Here's a snippet of the code I have: For note: CurDay is an incremental value that starts out at a value 7 less than today's date and builds up gradually until a match takes place.
foreach $file (@filenames) { open (IN,"$file") || die "Cannot open \"$file\" for input: $!. +\n"; @log_in = <IN>; close IN; print "Processing $file . . .\n"; init_vars(); foreach $ln (@log_in) { if ($alreadymatched) { $logfile[$count] = $ln; $count++; } else { do { if ($ln =~ /^$month:$curday/) { $alreadymatched = 1; $logfile[0] = $ln; } $curday++; } until ($alreadymatched); } } open (IN,">done.$file") || die "Cannot open \"$file\" for outp +ut: $!."; foreach $writelog (@logfile) { print IN $writelog; } close IN; }
The problem I'm facing is that tomorrow, when it tries to count from 7 days ago to tomorrow, although it will reach 1, it will not pattern match what I want it to in the file. I need to find a way to get CurDay to go from 25 26 27 28 29 30 31 1. The same will hold true for the next month as well, of course I'm told there's a Julian Date function or library out there but cannot seem to find it. I'm very much a novice at this and would appreciate any help that you all can give. If you can solve this w/o getting too obfuscated (such that a novice like myself can understand it) I'd appreciate that as well. Thanks! Xao

Replies are listed 'Best First'.
Re: End of Month Conversions For Date
by chromatic (Archbishop) on Apr 01, 2000 at 10:43 UTC
    CPAN has a lot of modules that would be worth using. Date::Calc comes to mind.

    You could also do a localtime manipulation as well.... I'd prefer that, myself.

Re: End of Month Conversions For Date
by little_mistress (Monk) on Apr 01, 2000 at 06:42 UTC
    OK i could help you but i dont know what your data looks like. can you post an example bit your your data?


    but other wise it may be the most simple to store the data as the result of the time() function. that way you have the number of seconds since the epoc.

    Perl then provides you with a simple way to convert that into plain text.

    so here is an example of what im talking about .....

    $timestandard = time() - 604800; # time() will give us the number of + seconds since the epoc # time() less 604800 seconds will give us the numbe +r of seconds since the epoc 7 days ago @data=&magic_fucntion_that_opens_the_file_and_gets_the_data(); foreach $d (@data) { push(@save, $d) if $d>$timestandard; } #so now all of the entries that are valid (ie. younger than one week o +ld) are in the array @save #now we print them to the file open(SEZME, ">my_data_file.txt"); $,="\n"; print SEZME @save; close SEZME; undef($,); #the rest of the script.............
    I dont know if that will help but ... i dont know how you are storing the time. If you have any control over that at all i would store it as seconds since the epoc ... it makes things so easy

    little_mistress@mainhall.com

Re: End of Month Conversions For Date
by httptech (Chaplain) on Apr 01, 2000 at 18:33 UTC
    Date::Manip can also do these calculations. It also has more flexibility in terms of returning the dates preformatted for you, which is why I use it instead of Date::Calc now.
RE: End of Month Conversions For Date
by vroom (His Eminence) on Apr 01, 2000 at 09:53 UTC
Re: End of Month Conversions For Date
by btrott (Parson) on Apr 03, 2000 at 05:56 UTC
    It sounds like you're doing log rotation, in which case you could probably make use of a simple fact: you want to keep around the last 7 days of log entries. So keep each day's log entries in separate files, and just keep the 7 most recent. Here's a really simple log rotation script (it's written for Apache log files, but there's nothing specific in it that you couldn't adapt to your purposes). Try it out--then you won't have to mess around with all that date arithmetic.

    If you must, though, my advice would be to convert the times into epoch seconds using Time::Local, then compare them thusly.

    use Time::Local; my $epoch_seconds = timelocal($sec, $min, $hour, $mday, $mon, $year);
Re: End of Month Conversions For Date
by xaokitten (Novice) on Apr 01, 2000 at 11:29 UTC
    epoch does sound like something I could do right now. I'll look into the Date::Calc module though. Oh, the data, btw, looks like so: 3:25:2000@0:0:1;Initializing LogWriter. 3:25:2000@0:0:1;NightlyEmailer;Mailer;Starting to process jobs.. 3:25:2000@0:0:2;NightlyEmailer;NightlyEmailer;*Starting mailer* 3:25:2000@0:0:2;Attempting to grab a database table lock for a mail jo +b. 3:25:2000@0:0:2;Lock acquired. 3:25:2000@0:0:35;Lock released. 3:25:2000@0:0:35;NightlyEmailer;NightlyEmailer;*Finishing mailer*