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

Dear Monks and Dudes...

I am trying to calculate the age of a log file. e.g.

The log file name will be in the format of 050927104522.log which is yymmddhhmmss.log

I need to check each log file and make sure that it is never 30 minutes old againts current date/time.

Explain:
Current datetime: 050927105522
List of log file: 050927104522.log,050927104222.log, 050927102022.log, 050927100000.log
Log file which is > 30 minutes old: 050927102022.log, 050927100000.log
(I need to remove those >30 minutes old)

I have a code (by pg) which gets current datetime and calculate the next 30 minutes

my $offset = 30; display(reverse((localtime(time()))[0 .. 5])); display(reverse((localtime(time() + 60 * $offset))[0 .. 5])); sub display { my @t = @_; printf("%d%02d%02d%02d%02d%02d\n", $t[0] + 1900, $t[1] + 1, @t +[2 .. 5]); }

How do i come about with this code where i can get the date from the log file name and add 30 minutes to it and compare againts the current date?

Cheers
Darren (Fellow Monk)

Replies are listed 'Best First'.
Re: Time/date calculation
by pg (Canon) on Sep 27, 2005 at 03:39 UTC

    You don't need any module. As your file names are well formatted - padded with zeroes and all the fields are in the right order, all you need is to compare file names as strings, in your case they follow the dictionary order.

    Instead of plus 30 minutes, subtract it, and form the file name on the boundary. Then just compare all the log file names with it as if they are all strings.

    my $offset = 30; display(reverse((localtime(time() - 60 * $offset))[0 .. 5])); sub display { my @t = @_; printf("%02d%02d%02d%02d%02d%02d\n", ($t[0] + 1900) % 100, $t[ +1] + 1, @t[2 .. 5]); }
Re: Time/date calculation
by monarch (Priest) on Sep 27, 2005 at 02:58 UTC
    Time::Local can convert your individual time fields (year, month, date, hour, etc) back into an epoch time which you can use to compare with the number returned by time().

    Of course you might want to use a regular expression to split your log filename into individual fields first..

    Update: pg's answer below is the best one I think; the great thing about time strings in yymmddhhmmss format is that they are easy to use for string sorting/comparisons..

Re: Time/date calculation
by graff (Chancellor) on Sep 27, 2005 at 03:26 UTC
    Time::Local, for sure:
    use strict; use Time::Local; $_ = my $file = "050924102022.log"; # a sample file name my @tl_data; unshift @tl_data, $1 while ( s/(\d{2})// ); print join " ", @tl_data, $/; # just to check $tl_data[-2]--; # need to subtract one from month number # (Time::Local can handle year number as-is) my $file_time = timelocal( @tl_data ); print "$file_time (sec. since epoch) = " . scalar localtime( $file_tim +e ) . $/; printf( "%s is %d minutes old\n", $file, int((time()-$file_time)/60));
Re: Time/date calculation
by davidrw (Prior) on Sep 27, 2005 at 03:39 UTC
    A Date::Calc solution (including a regex of your filename):
    my $filename = "050927104522.log"; use Date::Calc qw/Add_Delta_DHMS Today_and_Now Delta_YMDHMS/; my @thirtyMinAgo = Add_Delta_DHMS(Today_and_Now, 0,0,-30,0); # (Y,M,D +,H,M,S) if($filename =~ /(\d{12})\.log/ ){ my @fileDateTime = $1 =~ m/\d\d/g; # Break into the (Y,M,D,H,M,S) +format $fileDateTime[0] += 2000; # convert from YY to YYYY if( Delta_YMDHMS(@fileDateTime, @thirtyMinAgo) >= 0 ){ # fileDateTime is on or before thirtyMinAgo } }
Re: Time/date calculation
by tomazos (Deacon) on Sep 27, 2005 at 05:26 UTC
Re: Time/date calculation
by blazar (Canon) on Sep 27, 2005 at 10:16 UTC
    Not surprisingly there are many modules for date/time calculations. The two of them I've used myself with great advantage are Date::Calc and DateTime. In my experience they're somewhat complementary in functionality (even if there are, not unexpectedly, superimpositions, and by large) and definitely different in terms of UI: functional the former, OO-based the latter. HTH...
Re: Time/date calculation
by darrengan (Sexton) on Sep 28, 2005 at 02:06 UTC
    Hie Monks.....

    Thanks for all the advise and tips.... I am actually working on a huge production server (6 million subscribers) and the perl version is kinda old and not all modules are installed.

    That's why i would avoid using module whenever it is possible. I like the solution by pg (and also the rest). Anyway, i have solve my problem with bits and pieces..

    Cheers....