how do u convert a string to a date

C

colin_lyse

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
 
A

A. Sinan Unur

(e-mail address removed) (colin_lyse) wrote in
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
 
T

Tore Aursand

colin_lyse said:
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.
 
J

John W. Krahn

colin_lyse said:
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
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top