how to find the "yesterday" logfile name?

R

robertchen117

see if my directory has many logfiles like this:

log022607.log
log022707.log
log022807.log
log030107.log

today is 030207(03/02/07), I want to find the "yesterday" log, how
could I do in perl?
When time is 04/01/07, to get the "yesterday' log, which is
log033107.log maybe need some special handle, also when process for
year end...?

thanks very much!
 
P

Paul Lalli

see if my directory has many logfiles like this:

log022607.log
log022707.log
log022807.log
log030107.log

today is 030207(03/02/07), I want to find the "yesterday" log, how
could I do in perl?
When time is 04/01/07, to get the "yesterday' log, which is
log033107.log maybe need some special handle, also when process for
year end...?

Either use one of the bazillion Date parsing/modifying modules
available on CPAN (for example, Date::Calc), or just calculate the
timestamp yourself, subtract 24*60*60, and get the new date:

#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
use POSIX qw/strftime/;
my $today = '040107';
my ($m, $d, $y) = $today =~ /(\d\d)(\d\d)(\d\d)/;
my $timestamp = timelocal(0,0,12,$d,$m-1,$y);
my $yesterday = $timestamp - 24*60*60;
my $newdate = strftime("%m%d%y", localtime($yesterday));
print "$newdate\n";
__END__


Paul Lalli
 
T

Ted Zlatanov

PL> Either use one of the bazillion Date parsing/modifying modules
PL> available on CPAN (for example, Date::Calc), or just calculate the
PL> timestamp yourself, subtract 24*60*60, and get the new date:

PL> #!/usr/bin/perl
PL> use strict;
PL> use warnings;
PL> use Time::Local;
PL> use POSIX qw/strftime/;
PL> my $today = '040107';
PL> my ($m, $d, $y) = $today =~ /(\d\d)(\d\d)(\d\d)/;
PL> my $timestamp = timelocal(0,0,12,$d,$m-1,$y);
PL> my $yesterday = $timestamp - 24*60*60;
PL> my $newdate = strftime("%m%d%y", localtime($yesterday));
PL> print "$newdate\n";
PL> __END__

To find the next-to-last log, you don't necessarily need to do date
arithmetic. On the downside, this approach will be wrong when a log
is missing:

ls -t $dir | tail -2 | head -1

Another approach is to find the time period you want ('yesterday' is
usually defined as the period from midnight 1 day ago to midnight
tonight, excluding 00:00 tonight). If any file creation times fall
within that time period (assuming the logs are produced during
"yesterday") you have a hit.

It all comes down to what you are looking for: the file *stamped*
yesterday in the file name (Paul's solution), or the file *produced*
yesterday (my second suggestion), or the next-to-last file (my first
suggestion).

My personal favorite solution to this and most other timestamp issues
is to use Unix timestamps, which don't have the annoying problem of
date arithmetic (and they are timezone-neutral, which is something you
should consider). You then just need to pipe timestamps through a
simple filter like this (I call it t.pl and alias it to `t'):

#!/usr/bin/perl -p

use POSIX;

s/(\D?)(\d{10})(\D?)/$1 . strftime('%Y-%m-%d_%H:%M:%S', localtime($2)) . $3/e;

This will rewrite Unix timestamps as normal dates in your local
timezone. So if you stamp your files like this, just do 'ls|t'

Ted
 
G

gf

see if my directory has many logfiles like this:

log022607.log
log022707.log
log022807.log
log030107.log

today is 030207(03/02/07), I want to find the "yesterday" log, how
could I do in perl?
When time is 04/01/07, to get the "yesterday' log, which is
log033107.log maybe need some special handle, also when process for
year end...?

thanks very much!

Other ways to attack the same problems...

Precompute what the file's name should be, then use the "-M filename"
operator to get the number of days since the file was modified based
on the start date of the script. If your script is always running
this will give you bad results. If it's daily then quits (like a
scheduled job) then it'll be fine.

Again, precompute the file's name, then use "stat(filename)" to get
the file's particulars. You'll get its OS timestamp and can calculate
the delta in seconds from the current time to determine if the file
really has aged one day's worth of seconds.

If you are on Unix and don't know the file's name, then use the find
command ...

my $file_is = `find /path/to/files -m 1 -type t`;

and you'll get the file's name back if one exists that is one day old.
 
A

anno4000

Paul Lalli said:
Either use one of the bazillion Date parsing/modifying modules
available on CPAN (for example, Date::Calc), or just calculate the
timestamp yourself, subtract 24*60*60, and get the new date:

Usual objection: In places with DST there are two days per year
whose duration is different from 24*60*60 seconds. Better stick
to one of the time modules.

Anno
 
P

Paul Lalli

Usual objection: In places with DST there are two days per year
whose duration is different from 24*60*60 seconds.

Yes, which is why I set the dates to noon. If he's only subtracting
one date, there's no issue. If he were doing it in some sort of loop
where the hour offset would compound, then obviously it would be a
problem.
Better stick to one of the time modules.

<shrug> I consider that overkill for this particular situation.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top