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

Julian Date to Calender date conversion. Please help!

Status
Not open for further replies.

charanya

Programmer
Sep 30, 2011
1
0
0
US
I am new to Perl, I am trying to convert Julian date to calendar date (day of the year). For example, 26711 corresponds to 24-Sep-2011. I tried two approach to this, none being successful.

1) I saw this code online, but this code returns wrong date. This code does not return 24-Sep-2011, instead returns 26-Sep-2011 (Which is wrong!)

date_from_yday()
{
perl -e '
use Time::Local;

$yr = $ARGV[0] - 1900;
$yday = $ARGV[1];
$mytime = timelocal(1,0,0,1,1,$yr);
$mytime += ( 86400 * $yday );
($sec,$min,$hour,$day,$mon,$yr,$wday,$yday,$whocares) =
localtime($mytime);
$yr += 1900;
printf("%02d/%02d/%d\n", $day, $mon, $yr);

' $2 $1
}

date_from_yday 267 2011

-----------

2) I tried connecting to oracle db and fetch the date.

$RETVAL=`"select to_date(nvl(SUBSTR(126711 , 2 , 5) , '00179'), 'DDDYY') from dual" | $ORACLE_HOME/bin/sqlplus -L -S $CONNECT_STRING`;

This gives me error saying "select to_date(nvl(SUBSTR(126711 , 2 , 5) , '00179'), 'DDDYY') from dual : not found". I know the connection string is correct because that is used by other queries in the script.

Any help is appreciated. If there is a new approach to it, i can try that too. Please help ASAP.

Thanks

Charanya
 
The following will get you part of the way there. You'll still have to do the parsing and potentially install the DateTime module on your system:

Code:
use DateTime;

use strict;
use warnings;

my $dt = DateTime->new(
	year	=> 2011,
	month	=> 1,
	day 	=> 1,
);

$dt->add(days => 266);


print $dt->strftime("%b %d, %Y");

# Prints Sep 24, 2011

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top