Operating System - OpenVMS
1820270 Members
3475 Online
109622 Solutions
New Discussion юеВ

Re: Calculate no. of days

 
SOLVED
Go to solution
Kitti Thanapuasuwan
Occasional Advisor

Calculate no. of days

Hi,

Anyone has program to calculate no. of days between two date. I want to use it in DCL program.

Thanks
18 REPLIES 18
Jan van den Ende
Honored Contributor
Solution

Re: Calculate no. of days

Kitti,

upgrade to V7.3-2, and you will have F$DELTA_TIME


If you can not, a little DCL can do (we had it for years):

$ diff = 0
$ ldat=f$cvtim("","comparison","date")
$loop:
$ if f$cvtim("first_date+''diff'-","comparison","date") .lts. ldat
$ then
$ diff=diff+1
$ goto loop
$ endif

Sorry, indentation gets lost by forum makeup :-(

Proost.

Have one on me.

jpe
Don't rust yours pelled jacker to fine doll missed aches.
Hein van den Heuvel
Honored Contributor

Re: Calculate no. of days

Similar idea, but starting closer to the target:

$! DELTA_DAYS.COM original by J. FINKA Nov 1990
$! assume p1 <= p2 and delta < 9999
$ p2 = f$cvtime(p2,"absolute","date") !normalize
$ years = 'f$cvtime(p2,,"year") - 'f$cvtime(p1,,"year")
$ months = 'f$cvtime(p2,,"month") - 'f$cvtime(p1,,"month")
$ days = 'f$cvtime(p2,,"day") - 'f$cvtime(p1,,"day")
$ y = (36525 * years + 3044 * months) / 100 + days
$ i = 0
$ l: x = y + i
$ i = - i
$ if i .ge. 0 then i = i + 1
$ q = f$cvtime(p1 + " +" + f$string(x) + "-","absolute","date")
$ if q .nes. p2 then goto l
$ write sys$output "Delta days : ", x


$ @delta_days 01-jan-2001 01-jan-2005
Delta days : 1461

And one of many possible perl implemenations.
Calculate each date in seconds, subtract and divide by seconds in a day:

$ type delta_days.p
use Time::Local;
$_ = shift @ARGV;
if (/(\d{4})-(\d+)-(\d+)/) {
$d1 = timelocal(0,0,0,$3,$2-1,$1-1900);
} else {die "please provide date 1 as YYYY-MM-DD"};
$_ = shift @ARGV;
if (/(\d{4})-(\d+)-(\d+)/) {
$d2 = timelocal(0,0,0,$3,$2-1,$1-1900);
} else {die "please provide date 2 as YYYY-MM-DD"};
$days = ($d1 - $d2) / (24*60*60);
print "$days days";

$ perl delta_days.p 2005-1-1 2001-1-1
1461 days


Hein.
Jan van den Ende
Honored Contributor

Re: Calculate no. of days

@Hein:

Yes,

our _operational_ routine also had 1000 day steps with backtrack, then 100's, then 10's. but I did I Q & D re-create from memory...

And like any example given here:
A - use at own risk
B - use as starting point, take the idea & refine it.

cu in Nashua, I guess?

Proost.

Have one on me.

jpe
Don't rust yours pelled jacker to fine doll missed aches.
Antoniov.
Honored Contributor

Re: Calculate no. of days

Hi Kitti,
here there is a dcl proceure I converted from a my C program.
This procedure takes any date in dcl format (dd-mmm-yyyy) and converts it in julien date. Julien date is the number of days since a specific date. Within julien date you can add or subtract two dates.
Original routine works since 01-01-0000 but this dcl procedure works only since 1924 (read header for furthermore information).

Hope this help

Antonio Vigliotti
Antonio Maria Vigliotti
Robert Atkinson
Respected Contributor

Re: Calculate no. of days

Kitti - all of these are good solutions.

If you want a program to do this, ICALC (http://nucwww.chem.sunysb.edu/htbin/software_list.cgi?package=icalc) converts between Julian dates, so could be used to compare the two.

Robert.
Lachnitt_1
Frequent Advisor

Re: Calculate no. of days

Hi Kitti,

starting with VMS 8.2 you can use the lexical Function F$DELTA:

$ write sys$output F$DELTA("01-JAN-2001","01-JAN-2005")
1461 00:00:00.00

Bye Kuddel
Jan van den Ende
Honored Contributor

Re: Calculate no. of days

Kuddel,

like I already wrote in my first reply, F$DELTA is there since V7.3-2 (to be precise, in the DCL patch)

Proost.

Have one on me.

jpe
Don't rust yours pelled jacker to fine doll missed aches.
Wim Van den Wyngaert
Honored Contributor

Re: Calculate no. of days

Small remark for Antonio's solution :
1) the 2 dates must be in the same year to allow + - operations.
2) leap years are not supported (29 days in feb)

Wim
Wim
Wim Van den Wyngaert
Honored Contributor

Re: Calculate no. of days

To be clear : 2) isn't working because the leap year is added for all dates in a year, not only for days after 28-feb.

Wim
Wim
Antoniov.
Honored Contributor

Re: Calculate no. of days

Wim,
to be clear, dcl routine is convertion of C program. Original program is more complex because manage all dates since 01-01-0000 until today, calculating leap year and missed day in 1582 (Italy, France, Spain). C program is certified by a complex test unit. I removed complex calculation for missed days of 1582 and other minor feature so dcl routine is simple to write and easy to execute. I didn't remove leap year calculation therefore conversion works fine since 01-01-1925 for every country.

Antonio Vigliotti
Antonio Maria Vigliotti
Wim Van den Wyngaert
Honored Contributor

Re: Calculate no. of days

Antonio,

Just test it with the diff between 28-feb and 1-mar. It's always going to return 1, even for leap years.

Wim
Wim
Joseph Huber_1
Honored Contributor

Re: Calculate no. of days

Just for fun (and frustrated at VMS 7.3-1),
I made an executable f$delta_time for pre-7.3-2.

Look at it in f$delta_time.html

http://www.mpp.mpg.de/~huber
Wim Van den Wyngaert
Honored Contributor

Re: Calculate no. of days

Joseph Huber_1
Honored Contributor

Re: Calculate no. of days

Wim,

since it's different in every other forum:
to put such a link here in ITRC forum, is it enough to put the URL, not the
http://www.mpp.mpg.de/~huber
Wim Van den Wyngaert
Honored Contributor

Re: Calculate no. of days

Yes Joseph. Just paste what you have cut out of the address bar in IE.

Wim
Wim
Hein van den Heuvel
Honored Contributor

Re: Calculate no. of days

Antonio,

I'm sorry to also report that your DCL example is truly broken. You may want to verify that the C program was correct to begin with.
- As reported, it does not really do leapyears.
- It also 'lucks out' that 2000 actually was a leap year unlike 1900 and 2100
- It fails to accept input like 01-feb 01-apr because 'apr' is less than 'feb' in text form.
- It fails to accept 28-feb-2005 01-mar-2005 because 01 sorts befor 28 in text form.
- The days-so-far-in-this-year calculation uses a clumsy loop while you can just lookup the startdays of each month in the table instead of using the number of days in the table.

btw... I used a perl one-liner to make that new lis from your list:
$ perl -e "foreach (qw(0 31 28 31 30 31 30 31 31 30 31 30 31)) { $x +=$_; print "",$x"" }"
,0,31,59,90,120,151,181,212,243,273,304,334,365

The update code below seems to work.
Regards,
Hein.


$! Convertion date from DCL format to julien format
$! Author: Antonio Vigliotti antoniov@shs-av.it
$! Tweaks by Hein van den Heuvel
$! Julien date is the number of days since 1-1-0
$! Converted from a C routine
$! Original routine calculate change of calendar by pope Gregor XIII in
$! year 1582. Therefor this DCL routine works only since 1582 (in Italy)
$! or since 1824 (in Greek). For other countries read history!
$! Julien date can be use to compare two dates.
$begdat=p1
$enddat=p2
$if p2.nes."" then goto mloop1
$mloop:
$ inquire begdat "Beg.Date [dd-mmm-yyyy]"
$ if begdat.eqs."" then exit
$ inquire enddat "End Date [dd-mmm-yyyy]"
$ if enddat.eqs."" then exit
$mloop1:
$ if f$cvt(enddat).lts.f$cvt(begdat) then goto mloop
$ dt=begdat
$ gosub c2julien
$ begj=j
$ dt=enddat
$ gosub c2julien
$ endj=j
$ write sys$output enddat," - ",begdat," = ",endj-begj
$if p2.nes."" then exit
$ goto mloop
$c2julien:
$ y=f$cvtime(dt,,"year")
$ m=f$cvtime(dt,,"month")
$ days = "0,0,31,59,90,120,151,181,212,243,273,304,334"
$! Good untill 2100. Need 100/400 year rule after that
$ j=y*365 + (y+3)/4 + 'f$elem(m,",",days) + 'f$cvtim(dt,,"day")
$if m.gt.2 then j = j + 3 - f$cvtim("28-FEB-''y' +1-",,"MONTH") ! Leap?
$ return

--------------------


$ @tmp 28-feb-2004 01-mar-2004
01-MAR-2004 - 28-FEB-2004 = 2
$ @tmp 28-feb-2005 01-mar-2005
01-MAR-2005 - 28-FEB-2005 = 1





Antoniov.
Honored Contributor

Re: Calculate no. of days

Sorry for mistakes.
I worked too hurry :-(
Now, attachment is right routine. It works with leap year since 1582 in Italy, Spain and France. Since 1711 in UK, 1918 in Russia, 1925 in Greek.
I checked accurately original c routine so DCL is more complex.
I converted part of C test unit. Now you see test since 01-01-1900 until 31-12-2005.
Julien calculate yeap year every 4 years but not every century but every 400 years.
Before 15-10-1582 (in Italy, ...) yeap year was every 4 years. Pope Gregorio XIII changed calendar missed 10 days. England corrected calendar on 1710, Russia after revolution of 1917.
For furthermore information ask me.

Antonio Vigliotti
Antonio Maria Vigliotti
Antoniov.
Honored Contributor

Re: Calculate no. of days

Just for curiosity you can see original C routine. It is a complex project to calculate various date items (easter day lien date, holyday days and some other items).
Original C routine is not translated in english language so you can understand by C source code.
All routine are certificated by a very complex test unit (not attacched).
You can ask me for every information.

Thanks to Wim and Hein for their warning.

Antonio Vigliotti
Antonio Maria Vigliotti