The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
convert Julian date to calender date srikanthus2002 Shell Programming and Scripting 6 05-08-2007 03:27 AM
Find julian date for given corresponding date srikanthus2002 Shell Programming and Scripting 2 10-10-2006 06:33 PM
Julian Dates and the Cal command shan2on Shell Programming and Scripting 0 06-26-2006 02:32 PM
Calendar date to Julian and Back BCarlson Shell Programming and Scripting 4 05-14-2005 02:18 PM
Converting YYYYMMDD to Julian dfran1972 Shell Programming and Scripting 5 04-28-2005 07:34 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-17-2003
Registered User
 

Join Date: Nov 2001
Posts: 59
Julian Date

I have a shell script which gets passed a parameter which is a combination of Year and Julian Date <YYYYj>. So April 11th, julian date is 101. So if I wanted April 11th for 2003 I would get the following value 2003101. How would I convert that in unix to be 20030411? I am using the korn shell.
Reply With Quote
Forum Sponsor
  #2  
Old 04-17-2003
oombera's Avatar
Registered User
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
Well if you can alter the script that's passing the parameter, you could change the piece of code, such as date +%Y%j, so that it sends what you want it to send, such as date +%Y%m%d.

Otherwise...
Assuming the variable that stores that passed value is called varPassed, you can put this (ksh) code in a file called someScript, make it executable, and call it with newValue=`someScript $varPassed`:

Code:
varPassed=$1

month=1
flag=0

theYear=`echo $varPassed | awk '{print substr ($0, 1, 4)'}`
dayOfYear=`echo $varPassed | awk '{print substr ($0, 5, 3)'}`
dayOfYear=`expr $dayOfYear - 0`

TestLeapYear() {
  if [[ `expr $theYear % 4` != 0 && $dayOfYear -gt 28 ]] then
    month=`expr $month + 1`
    dayOfYear=`expr $dayOfYear - 28`
  else
    if [[ `expr $theYear % 4` = 0 && $dayOfYear -gt 29 ]] then
      month=`expr $month + 1`
      dayOfYear=`expr $dayOfYear - 29`
    else
      flag=1
    fi
  fi
}

Days30() {
  if [[ $dayOfYear -gt 30 ]] then
    month=`expr $month + 1`
    dayOfYear=`expr $dayOfYear - 30`
  else
    flag=1
  fi
}

Days31() {
  if [[ $dayOfYear -gt 31 ]] then
    month=`expr $month + 1`
    dayOfYear=`expr $dayOfYear - 31`
  else
    flag=1
  fi
}

Days31 #january
if [[ $flag = 0 ]] then
  TestLeapYear #february
fi
if [[ $flag = 0 ]] then
  Days31 #march
fi
if [[ $flag = 0 ]] then
  Days30 #april
fi
if [[ $flag = 0 ]] then
  Days31 #may
fi
if [[ $flag = 0 ]] then
  Days30 #june
fi
if [[ $flag = 0 ]] then
  Days31 #july
fi
if [[ $flag = 0 ]] then
  Days31 #august
fi
if [[ $flag = 0 ]] then
  Days30 #september
fi
if [[ $flag = 0 ]] then
  Days31 #october
fi
if [[ $flag = 0 ]] then
  Days30 #november
fi

if [[ $month -lt 10 ]] then
  month=0$month
fi
if [[ $dayOfYear -lt 10 ]] then
  dayOfYear=0$dayOfYear
fi

echo $theYear$month$dayOfYear

Last edited by oombera; 04-17-2003 at 11:52 AM.
Reply With Quote
  #3  
Old 04-17-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
What you're doing is more of a day-of-year thing rather than a Julian number thing. But a Julian number calculator can handle this stuff easily.

Get the Julian number for the day before Jan 1 of the year in question:
datecalc -j 2002 12 31
52639

Now calculate the JD of the date you want:
52740=52639+101

Lastly, convert that to a date:
datecalc -j 52740
2003 4 11

Of course, you will want to read the output of the last command into some variables, not just display them.

With ksh, this is really just a one-liner:
Code:
Y=2003
DOY=101
datecalc -j $(($(datecalc -j $((Y-1)) 12 31) + DOY)) | read year month day
You can get datecalc here.

I see oombera beat me to the punch here 8) Oh well, now you have two options..
Reply With Quote
  #4  
Old 04-17-2003
Registered User
 

Join Date: Nov 2001
Posts: 59
thanks

thanks for your help
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0