how do u convert a string to a date

Discussion in 'Perl Misc' started by colin_lyse, Jan 26, 2005.

  1. colin_lyse

    colin_lyse Guest

    using perl on Win2k box


    have a file with the following lines (total of 50)

    I. 12/16 22:33:17. <thread 1.5>: Ready to handle requests
    E. 01/11 18:29:12. <thread 1.1>: Ready to handle requests
    I. 01/12 11:29:12. <thread 1.2>: Ready to handle requests
    I. 01/12 13:29:12. <thread 1.3>: Ready to handle requests
    E. 01/26 19:29:12.<Main>: Error: [-10002] ODBC: [Microsoft] ODBC Driver
    I. 01/26 19:29:12. <thread 1.5>: Ready to handle requests

    what i need to do is for dates and times within the last hour fine any line
    with E.

    what i looking for is how to convert the timestamp in the line to an actual
    timestamp i can compare to the localtime. have seen lots of info on
    converting dates to string but not other way around
     
    colin_lyse, Jan 26, 2005
    #1
    1. Advertising

  2. (colin_lyse) wrote in
    news:41f8077a$0$94842$:

    > have a file with the following lines (total of 50)
    >
    > I. 12/16 22:33:17. <thread 1.5>: Ready to handle requests
    > E. 01/11 18:29:12. <thread 1.1>: Ready to handle requests
    > I. 01/12 11:29:12. <thread 1.2>: Ready to handle requests
    > I. 01/12 13:29:12. <thread 1.3>: Ready to handle requests
    > E. 01/26 19:29:12.<Main>: Error: [-10002] ODBC: [Microsoft] ODBC
    > Driver I. 01/26 19:29:12. <thread 1.5>: Ready to handle requests
    >
    > what i need to do is for dates and times within the last hour fine any
    > line with E.
    >
    > what i looking for is how to convert the timestamp in the line to an
    > actual timestamp i can compare to the localtime. have seen lots of
    > info on converting dates to string but not other way around


    Take a look at the Date::Calc module:

    #! /usr/bin/perl

    use strict;
    use warnings;

    use Date::Calc 'Mktime';

    my $this_year = 1900 + (localtime time)[5];

    while(my $line = <DATA>) {
    chomp $line;
    last unless $line;
    if($line =~ m{^E\. (\d+\d+)/(\d+\d+) (\d+\d+):(\d+\d+):(\d+\d+)}) {
    my($mon, $day, $hr, $min, $sec)
    = (0 + $1, 0 + $2, 0 + $3, 0 + $4, 0 + $5);
    my $parsed = localtime(
    Mktime($this_year, $mon, $day, $hr, $min, $sec)
    );
    print "$line =>\n\t$parsed\n\n";
    }
    }

    __DATA__
    I. 12/16 22:33:17. <thread 1.5>: Ready to handle requests
    E. 01/11 18:29:12. <thread 1.1>: Ready to handle requests
    I. 01/12 11:29:12. <thread 1.2>: Ready to handle requests
    I. 01/12 13:29:12. <thread 1.3>: Ready to handle requests
    E. 01/26 19:29:12.<Main>: Error: [-10002] ODBC: [Microsoft] ODBC Driver
    I. 01/26 19:29:12. <thread 1.5>: Ready to handle requests

    C:\Dload> q
    E. 01/11 18:29:12. <thread 1.1>: Ready to handle requests =>
    Tue Jan 11 18:29:12 2005

    E. 01/26 19:29:12.<Main>: Error: [-10002] ODBC: [Microsoft] ODBC Driver
    =>
    Wed Jan 26 19:29:12 2005

    Sinan
     
    A. Sinan Unur, Jan 26, 2005
    #2
    1. Advertising

  3. colin_lyse

    Tore Aursand Guest

    colin_lyse wrote:
    > have a file with the following lines (total of 50)
    >
    > I. 12/16 22:33:17. <thread 1.5>: Ready to handle requests
    > E. 01/11 18:29:12. <thread 1.1>: Ready to handle requests
    > I. 01/12 11:29:12. <thread 1.2>: Ready to handle requests
    > I. 01/12 13:29:12. <thread 1.3>: Ready to handle requests
    > E. 01/26 19:29:12.<Main>: Error: [-10002] ODBC: [Microsoft] ODBC Driver
    > I. 01/26 19:29:12. <thread 1.5>: Ready to handle requests
    >
    > what i need to do is for dates and times within the last hour fine any line
    > with E.


    I think the 'Time::Local' module will do the job on this one;


    #!/usr/bin/perl
    #
    use strict;
    use warnings;
    use Time::Local;

    use constant SECONDS_IN_HOUR => 60 * 60;

    my $now = time;
    my $year = (localtime)[5] + 1900;

    while ( <DATA> ) {
    if ( m,^E\. (\d+)/(\d+) (\d+):(\d+):(\d+), ) {
    my $time = timelocal( $5, $4, $3, $2, $1 - 1, $year );
    print if ( ($now - $time) < SECONDS_IN_HOUR );
    }
    }

    You might want to "calculate" $now and $year inside that 'while'
    statement if you're piping to the script, though.


    --
    Tore Aursand <>
    "To cease smoking is the easiset thing I ever did. I ought to know,
    I've done it a thousand times." (Mark Twain)
     
    Tore Aursand, Jan 26, 2005
    #3
  4. colin_lyse wrote:
    > using perl on Win2k box
    >
    >
    > have a file with the following lines (total of 50)
    >
    > I. 12/16 22:33:17. <thread 1.5>: Ready to handle requests
    > E. 01/11 18:29:12. <thread 1.1>: Ready to handle requests
    > I. 01/12 11:29:12. <thread 1.2>: Ready to handle requests
    > I. 01/12 13:29:12. <thread 1.3>: Ready to handle requests
    > E. 01/26 19:29:12.<Main>: Error: [-10002] ODBC: [Microsoft] ODBC Driver
    > I. 01/26 19:29:12. <thread 1.5>: Ready to handle requests
    >
    > what i need to do is for dates and times within the last hour fine any line
    > with E.
    >
    > what i looking for is how to convert the timestamp in the line to an actual
    > timestamp i can compare to the localtime. have seen lots of info on
    > converting dates to string but not other way around


    That is all you need to do:


    my @date = localtime time - 3600; # one hour ago
    my $today = sprintf '%02d/%02d', $date[ 4 ] + 1, $date[ 3 ];
    my $last_hour = sprintf '%02d:%02d:%02d', @date[ 2, 1, 0 ];
    my $curr_hour = sprintf '%02d:%02d:%02d', ( localtime )[ 2, 1, 0 ];


    while ( <FILE> ) {

    my ( $status, $date, $time ) = m|^(\w)\.\s+([\d/]+)\s+([\d:]+)|;

    print if $status eq 'E' and $date eq $today and $time gt $last_hour and
    $time lt $curr_hour;

    }



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jan 26, 2005
    #4
    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. h_ghanaty

    convert gregorian date to jalay date

    h_ghanaty, Nov 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    553
    Bret Mulvey [MS]
    Nov 9, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,370
    Michael Borgwardt
    May 30, 2004
  3. wgan
    Replies:
    7
    Views:
    616
    Roedy Green
    Jul 8, 2004
  4. Replies:
    8
    Views:
    820
    Boris
    May 1, 2007
  5. davyb
    Replies:
    7
    Views:
    226
    Gregory Brown
    Nov 18, 2005
Loading...

Share This Page