Operating System - HP-UX
1833847 Members
2193 Online
110063 Solutions
New Discussion

Fast Julian Date Conversions

 
SOLVED
Go to solution
Mary Rice
Frequent Advisor

Fast Julian Date Conversions

Hello Experts,

I have used Clay's caljd.sh to do 30, 60, and 90 day date calculations but it is slow when I have to process thousands of these in one report. Does anyone (Clay maybe) have a faster set of routines which can be part of a shell script? I don't need anything really fancy just some basic 30 days from today in MM/DD/YYYY form.

TIA, Mary Rice
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Fast Julian Date Conversions

Hi Mary:

I'll be the first to admit that caljd.sh is something of a dog these days; it's become feature-rich and as a consequence expanded from less than 40 lines to nearly 1100 lines - most of it is usage, examples, and command-line processing. The good news is that I have 3 functions that are very fast and can be put in any Korn or POSIX shell script and uses the shell's internal arithmatic operators.

The first function is CAL_JDATE, feed it month, day, and year and it echoes a Julian Day.



If it ain't broke, I can fix that.
John Poff
Honored Contributor

Re: Fast Julian Date Conversions

Hi Mary,

Clay has a Perl version of his script. You might give it a try. Here is a link to the most recent thread where Clay posted it:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xab076049dbb6d611abdb0090277a778c,00.html

JP

P.S. Expert? Do you know the definition of an expert?

X-spurt:

X = an unknown quantity
spurt = a drip under pressure

:)

A. Clay Stephenson
Acclaimed Contributor

Re: Fast Julian Date Conversions

The next function is JDATE_CAL. It expects a Julian Date and echoes separated month, day, and year values.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Fast Julian Date Conversions

The 3rd function is WKDAY. It takes a Julian Day argument and echoes a value from 0 (Sunday) to 6 (Saturday).
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Fast Julian Date Conversions

Those should give you the basic operations but no skipping of holidays or automatic skipping of weekends.

Here is a simple example which takes today's date and converts it to a Julian Date and then does the reverse.

JD=$(CAL_JDATE $(date '+%m %d %Y'))
echo "JD = ${JD}"
CD=$(JDATE_CAL ${JD})
echo "CD = ${CD}"
typeset -i10 MO
typeset -i10 DA
typeset -i10 YR
echo "${CD}" | read MO DA YR
echo "Month = ${MO} Day = ${DA} YR = ${YR}"

W1=$(WKDAY ${JD})
echo "Weekday = ${W1}"

Of course to do your 30, 60, and 90 day stuff you would first get the Julian Day of a value and add 30, 60, and 90 days to it before calling JDATE_CAL() for each of the values.

You can also yank the equivalent functions out of caljd.pl but if you want pure shell then the above functions should do the trick and should amaze you with their speed.

Regards, Clay

If it ain't broke, I can fix that.
Mary Rice
Frequent Advisor

Re: Fast Julian Date Conversions

Thanks Clay,

As usual your answer was right on! These functions are much faster and work just as well as caljd.sh.

Thanks again,
Mary Rice