From:	SMTP%"marshall@lmsc.lockheed.com"  5-AUG-1994 13:18:41.86
To:	EVERHART
CC:	
Subj:	Re: DCL for obtaining days between dates?

From: marshall@skynet.ssd.lmsc.lockheed.com (Bob Marshall)
X-Newsgroups: comp.os.vms
Subject: Re: DCL for obtaining days between dates?
Message-Id: <1994Aug4.124342.365@skynet.ssd.lmsc.lockheed.com>
Date: 4 Aug 94 12:43:42 -0800
Reply-To: marshall@lmsc.lockheed.com
Distribution: world
Organization: Lockheed Missiles & Space Company
Lines: 90
To: Info-VAX@CRVAX.SRI.COM
X-Gateway-Source-Info: USENET

In article <31p20a$jid@gap.cco.caltech.edu>, carl@SOL1.GPS.CALTECH.EDU (Carl J Lydick) writes:
> In article <31o587$om0@paperboy.ids.net>, green@ids.net writes:
> =Say - does anyone have any DCL command procedures that will return
> =the number of days between two dates?
> 
> Well, I didn't until you asked, but it only took about 20 minutes to cook up
> the following.  It's not terribly elegant, and doesn't work with dates before
> 17-NOV-1858 or after 31-DEC-9999.  Takes about 11 seconds for worst case (dates
> 17-NOV-1858 and 31-DEC-9999) on a 3800.
>
> [...Carl's very clever implementation of a binomial search in DCL deleted...]

Here's one that I wrote that is much more direct. It converts the dates into
Julian dates, which are easily subtracted. (The drawback is that the Julian
formulas I use here are only good through the year 2100, so if you need dates
beyond that, use Carl's routine :-)).

$ DELTADAYS=99999
$ IF P1 .EQS. "" THEN READ/PROMPT="First  date : " SYS$COMMAND P1
$ IF P2 .EQS. "" THEN READ/PROMPT="Second date : " SYS$COMMAND P2
$!
$! Parse out year, month, day
$!
$ YEAR1 = F$CVTIME(P1,,"YEAR")
$ MONTH1 = F$CVTIME(P1,,"MONTH")
$ DAY1 = F$CVTIME(P1,,"DAY")
$ YEAR2 = F$CVTIME(P2,,"YEAR")
$ MONTH2 = F$CVTIME(P2,,"MONTH")
$ DAY2 = F$CVTIME(P2,,"DAY")
$!
$! Verify that the first date is in the usable range (3/1/1900 - 2/28/2100)
$!
$ IF YEAR1.EQ.1900 .AND. MONTH1.LE.2 THEN GOTO RANGE_ERR
$ IF YEAR1.LT.1900 THEN GOTO RANGE_ERR
$ IF YEAR1.EQ.2100 .AND. MONTH1.GT.2 THEN GOTO RANGE_ERR
$ IF YEAR1.GT.2100 THEN GOTO RANGE_ERR
$!
$! Verify that the second date is in the usable range (3/1/1900 - 2/28/2100)
$!
$ IF YEAR2.EQ.1900 .AND. MONTH2.LE.2 THEN GOTO RANGE_ERR
$ IF YEAR2.LT.1900 THEN GOTO RANGE_ERR
$ IF YEAR2.EQ.2100 .AND. MONTH2.GT.2 THEN GOTO RANGE_ERR
$ IF YEAR2.GT.2100 THEN GOTO RANGE_ERR
$!
$! The Julian day for the date ID-MP-YP is given by :
$!
$! J = INT(365.25*YP) + INT(30.6*MP) + ID + 1,720,982
$!
$! Since DCL does not allow for real variables or real constants, the real
$! constants are multipled by factors of ten, since we want to discard
$! fractional portions anyway.
$!
$! Calculate the Julian day of the first date (minus 1,720,982)
$!
$ YP = YEAR1
$ IF MONTH1.LE.2 THEN YP = YEAR1-1
$ MP = MONTH1+1
$ IF MONTH1.LE.2 THEN MP = MONTH1+13
$!
$ JULIAN1 = (36525*YP)/100 + (306*MP)/10 + DAY1
$!
$! Calculate the Julian day of the date (minus 1,720,982)
$!
$ YP = YEAR2
$ IF MONTH2.LE.2 THEN YP = YEAR2-1
$ MP = MONTH2+1
$ IF MONTH2.LE.2 THEN MP = MONTH2+13
$!
$ JULIAN2 = (36525*YP)/100 + (306*MP)/10 + DAY2
$!
$ DELTADAYS == JULIAN2 - JULIAN1
$!
$ EXIT
$!
$ RANGE_ERR:
$!
$ WRITE SYS$OUTPUT ""
$ WRITE SYS$OUTPUT "*** Error...One of the dates is out of the allowable range"
$ WRITE SYS$OUTPUT "            Both dates must be between 1-MAR-1900 and 28-FEB-2100"
$ WRITE SYS$OUTPUT ""
$ EXIT

-- 
=============================================================================
Bob Marshall                      \\   Disclaimer :
Lockheed Missiles & Space Co.      \\    I'm just a soul whose intentions
Sunnyvale, CA                       \\     are good, Oh, Lord, please      
marshall@lmsc.lockheed.com           \\      don't let me be misunderstood 
"I tell the truth 'cept when I lie"   \\       -Eric Burdon
=============================================================================

