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

Converting calendar date to julian date 1

Status
Not open for further replies.

sfitz454

Programmer
Mar 18, 2002
3
US
Is there a function or subroutine that I can use in a perl or Unix script to convert any calendar date to a julian date?
 
'+%j' returns the day of year [1-366]
see: man strftime

---- to convert to julian you probably need an own-eritted function. seed in c-perl-awk forum. there are a lot of date-threads ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
This works for Julian dates from 1/1/1900(start of last century) - Enter the date as DD MM CCYY - ie 16 10 1959
or as a julian date (eg 100 )to convert to calendar date.
Entering no args gives today's date

#!/usr/bin/ksh

integer jdate=0
integer d=0
integer m=0
integer y=0
integer argc=$#


case $argc in
0) break;;
1) jdate=$1
break;;
3) d=$1
m=$2
y=$3
break;;

*) echo "usage is $(basename $0) [jd | d m y ]"
exit 1 ;;
esac

awk -v argc="$argc" -v jdate="$jdate" -v d="$d" -v m="$m" -v y="$y" '

function isly(y)
{
x=0
if(y%400==0) x=x+1
if(y%100==0) x=x-1
if(y%4 ==0) x=x+1
return x
}

function daysin(m,y)
{
if(m==2) return 28+isly(y)

if(m==4 || m==6 || m==9 || m==11) return 30

return 31
}

function fDateToJulian( day, month, year )
{

ldaynumber=day

for(lmonth=1; lmonth<month; lmonth++)
{
ldaynumber+=daysin(lmonth,year)
}

for(lyear=1900;lyear<year; lyear++)
{
ldaynumber+= 365+isly(lyear)
}

return ldaynumber
}

function fJulianToDate(jdate)
{
d=1
m=1
y=1900

while( jdate-(365+isly(y)) > 0 )
{
jdate-=365+isly(y)
y=y+1
}

while( jdate-daysin(m,y) >0)
{
jdate-= daysin(m,y)
m=m+1
}

d=jdate
}

BEGIN {

if(argc==0)
{
&quot;date +\&quot;%d %m %Y\&quot;&quot; | getline datestr
$0=datestr
d=$1
m=$2
y=$3

juliandate= fDateToJulian( d,m,y)
printf(&quot;today is %02d/%02d/%04d -> %d\n&quot;,d,m,y, juliandate)

}
if(argc==1)
{
fJulianToDate(jdate)
printf(&quot;%d -> %02d/%02d/%04d\n&quot;,jdate,d,m,y)
}

if(argc==3)
{
juliandate= fDateToJulian( d,m,y)
printf(&quot;%02d/%02d/%04d -> %d\n&quot;,d,m,y, juliandate)
}

}'

;-)HTH ;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
i always thought julian date was day of the year....my bad sorry
 
Dickie Bird,

I executed the code with no args and generated the following messages.

awk: String 0

functio cannot be longer than 399 bytes.
The source line is 1.
The error context is
>>> <<<
awk: String 0

functio cannot be longer than 399 bytes.
The source line is 1.
awk: String 0

functio cannot be longer than 399 bytes.
The source line is 1.
awk: There is an illegal character in the input file.
The source line number is 1.


I have minimal awk programming experience so please advise.

Thanks,
sfitz454
 
do you have nawk installed ? try it ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top