Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing dates

Status
Not open for further replies.

Corwinsw

Programmer
Sep 28, 2005
117
BG
I have file with sth like that:

Mon Aug 1 08:07:21 -0700 2005
Tue Aug 2 09:11:32 -0700 2005

I need only the month, day, hour and year values to sort them later, which is absolutely another question/thread:)

So the first think which comes to my mind is to
split them into array and pass the needed values in another array, but it is too stupid I think. Then I tried this:

while (<IN>) {

($arr[$i][1], $arr[$i][2], $arr[$i][3], $arr[$i][4]) =
/^\w{3}\s(\w{3})\s(\d{2})\s(\d{2}\:\d{2}\:\d{2})\s[\-\s\d{4}]\s(\d{4})/;

}

but it's obviouslly wrong.

Do you have any ideas?

Corwin
 
I'd recommend the Parse_Date routine from Date::Calc:

#!/usr/bin/perl -w
use strict;
use Date::Calc qw(Parse_Date);

my $date = 'Mon Aug 1 08:07:21 -0700 2005';
my ($year,$month,$day) = Parse_Date($date);
print "$year-$month-$day\n";

Holger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top