HARVEY@SLACVM.SLAC.STANFORD.EDU (05/24/91)
I need a program/algorithm that will give me the number of days between dates. I have seen it on calculators before. Any language appreciated. Thanks in advance.
bayram@sun54.cs.nps.navy.mil (" Suleyman Bayramoglu ") (05/24/91)
Here is an Ada function that I wrote a couple of years ago. It uses CALENDAR
package in IntegrAda Compiler. I hope it would be useful to you.
Regards...
Suleyman Bayramoglu
bayram@taurus.cs.nps.navy.mil
______________________________________________________________________________
--
--
-- Title : function CONVERT_TO_DAYS
-- Author : Suleyman Bayramoglu
-- Date : 4 Jun 1989
-- Revised : 15 Jun 1989 16:00
-- Compiler : IntegrAda Version 4.0
-- System : IBM PC, DOS Version 2.xx or later
-- Description :
-- Calculates the number of days between the input date and Jan 1, 1901
-- taking the leap years into account.
-- ---------------------------------------------------------------------------
function CONVERT_TO_DAYS (DATE : CALENDAR.TIME) return FLOAT is
YEAR_DAYS : FLOAT; -- # of days just for years
MONTH_DAYS : INTEGER; -- # of days for months
LEAP_YEARS : INTEGER; -- # of leap years since 1901
DAYS : FLOAT; -- total # of days since Jan 1, 1901
START_YEAR : constant CALENDAR.YEAR_NUMBER := 1901;
YEAR : CALENDAR.YEAR_NUMBER; -- year in CALENDAR,range 1901..2099
MONTH : CALENDAR.MONTH_NUMBER; -- month in CALENDAR, range 1..12
DAY : CALENDAR.DAY_NUMBER; -- day in CALENDAR, range 1..31
SECONDS : CALENDAR.DAY_DURATION; -- defined in CALENDAR, just used
-- procedure call.
begin
-- extract YEAR, MONTH and DAY from DATE
CALENDAR.SPLIT(DATE, YEAR, MONTH, DAY, SECONDS);
-- calculate # of leap years betwwen 1901 and DATE
LEAP_YEARS := (YEAR - START_YEAR) / 4;
YEAR_DAYS := FLOAT(YEAR - START_YEAR) * 365.0 + FLOAT(LEAP_YEARS);
case MONTH is
when 1 => MONTH_DAYS := 0;
when 2 => MONTH_DAYS := 31;
when 3 => MONTH_DAYS := 59;
when 4 => MONTH_DAYS := 90; -- calculate # of days for the
when 5 => MONTH_DAYS := 120; -- month from the beginning of
when 6 => MONTH_DAYS := 151; -- the year of input DATE
when 7 => MONTH_DAYS := 181;
when 8 => MONTH_DAYS := 212;
when 9 => MONTH_DAYS := 243;
when 10 => MONTH_DAYS := 273;
when 11 => MONTH_DAYS := 304;
when 12 => MONTH_DAYS := 334;
when others => null;
end case; -- MONTH
-- a leap year is exactly divisible by 4 but not 100, or is exactly
-- divisible by 400
if (YEAR mod 4 = 0 and YEAR mod 100 /= 0) or YEAR mod 400 = 0 then
if MONTH >= 3 then
MONTH_DAYS := MONTH_DAYS + 1; -- add 1 if a leap year
end if;
end if;
DAYS := YEAR_DAYS + FLOAT(MONTH_DAYS) + FLOAT(DAY);
return DAYS;
end CONVERT_TO_DAYS;
--
Suleyman