how to find the "yesterday" logfile name?

Discussion in 'Perl Misc' started by robertchen117@gmail.com, Mar 2, 2007.

  1. Guest

    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!
    , Mar 2, 2007
    #1
    1. Advertising

  2. Paul Lalli Guest

    On Mar 2, 4:04 am, "" <>
    wrote:
    > 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
    Paul Lalli, Mar 2, 2007
    #2
    1. Advertising

  3. Ted Zlatanov Guest

    On 2 Mar 2007 05:38:13 -0800 "Paul Lalli" <> wrote:

    PL> On Mar 2, 4:04 am, "" <>
    PL> wrote:
    >> 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...?


    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
    Ted Zlatanov, Mar 2, 2007
    #3
  4. gf Guest

    On Mar 2, 2:04 am, "" <>
    wrote:
    > 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.
    gf, Mar 2, 2007
    #4
  5. -berlin.de Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > On Mar 2, 4:04 am, "" <>
    > wrote:
    > > 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:


    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
    -berlin.de, Mar 3, 2007
    #5
  6. Paul Lalli Guest

    On Mar 3, 8:11 am, -berlin.de wrote:
    > Paul Lalli <> wrote in comp.lang.perl.misc:


    > > On Mar 2, 4:04 am, ""
    > > > today is 030207(03/02/07), I want to find the "yesterday" log,
    > > > how could I do in perl?


    > > 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.


    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
    Paul Lalli, Mar 3, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Muruga Raj

    Logfile name format in logging

    Muruga Raj, Dec 23, 2008, in forum: Ruby
    Replies:
    3
    Views:
    113
    Muruga Raj
    Dec 24, 2008
  2. Magnus
    Replies:
    6
    Views:
    113
    Tore Aursand
    Jan 21, 2004
  3. magoo

    Logfile name hack

    magoo, Jul 2, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    158
    Joe Smith
    Jul 3, 2004
  4. PerlFAQ Server

    FAQ 4.17 How do I find yesterday's date?

    PerlFAQ Server, Feb 7, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    124
    PerlFAQ Server
    Feb 7, 2011
  5. Cameron Simpson
    Replies:
    1
    Views:
    76
    Marko Rauhamaa
    Mar 23, 2014
Loading...

Share This Page