Friday, March 20, 2009

convert Julian date to normal date

This is little hacky to get normal date from day of year in perl (kind of Julian date )
My date was in YYDDDHH format and I converted this to MM/DD/YYYY HH:MM:SS format:
Where DDD is the day of he year.

Here is the Perl script i wrote for this :

use Time::Local 'timelocal_nocheck';
$file_date = "0907616";
#convert to MM/DD/YYYY : original is # YYDDDHH
if ($file_date =~ /[0-9]/ )
{
$year = substr($file_date, 0, 2 );$year =~ s/^$/0/;
$yday = substr( $file_date, 2, 3); $yday =~ s/^$/0/;
$hour = substr( $file_date, 5, 2);$hour =~ s/^$/0/;
$file_date = convert_yday_caldate($year, $yday , $hour);
$file_date =

substr( $file_date, 0, 2 ) . "/"
. substr( $file_date, 2, 2 ) . "/"
. substr( $file_date, 4, 4 ). " "
. substr( $file_date, 8, 2 ). ":"
. substr( $file_date, 10, 2 ). ":"
. substr( $file_date, 12, 2 ) ;
}

# Convert Julian date to normal claneder date
sub convert_yday_caldate
{
my ($year, $yday, $hour) = @_;

$year +=(1900 + 100);

my ($s, $min, $h, $d, $m, $y) = (localtime(timelocal_nocheck(0,0,$hour,$yday,0,$year)))[0,1,2,3,4,5];
$y +=1900; #(off set with 1900)
$m++; #(as month start from 0)
return sprintf("%02d%02d%4d%02d%02d%02d", $m, $d, $y, $h, $min, $s);
}



No comments:

Post a Comment