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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Conversion of Date stamp in a file 1

Status
Not open for further replies.

siswjh

MIS
Dec 6, 2001
63
US
What I have is some log files that are created by a scheduler we run our jobs through (OS Solaris 8). The date in the logfile is of this format Fri Jan 9. I would like to extract this out of the file and convert it to this format 2004-01-09 and put it in a new file. Is there a way to do this.
 
Yep. Date::Manip is a fantastic module for this kind of thing.

use Date::Manip;
$date=&ParseDate("Fri Jan 9");
$date =~ s/^(.{4})(.{2})(.{2})..:..:..$/$1-$2-$3/;
print $date;

To put it in a new file:

open (OUT, ">$file");
print OUT $date;
close (OUT);
 
this works if you do not have the module... (i'm not dissing it!)...

$date = "Fri Jan 29";

$date =~ s/[A-Z][a-z]{2} //; # get rid of day of week & following space
print "after removal of day of week: $date\n";

$date =~ s/ ([0-9])$/ 0$1/;
print "after addition of zero if necessary: $date\n";

$date =~ s/([A-Z][a-z]{2}) ([0-9]+)/2004-$1-$2/;

$date =~ s/Jan/01/;
$date =~ s/Feb/02/;
$date =~ s/Mar/03/;
$date =~ s/Apr/04/;
$date =~ s/May/05/;
$date =~ s/Jun/06/;
$date =~ s/Jul/07/;
$date =~ s/Aug/08/;
$date =~ s/Sep/09/;
$date =~ s/Oct/10/;
$date =~ s/Nov/11/;
$date =~ s/Dec/12/;

print "$date\n\n";


Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top