home *** CD-ROM | disk | FTP | other *** search
- program DateConversion; {convert text dates to julian values and back}
-
- {
- This file is a rather literal translation of algorithims originally
- coded in BASIC, to demonstrate conversion of dates between Julian
- (sequential) and Gregorian (MM/DD/YY) notation.
-
- For the sake of clarity, I have made all variables (except three used
- internally in the CONVERT_GREGORIAN routine) global. Only one routine
- (CONVERT_JULIAN) makes use of a variable (WEEKDAY) whose value is set by
- a separate function (TEXTDATE), to reference the array constant DAYS.
- All other routines operate independently of one another.
-
- Thanks to John O'Boyle and Mike Todd on The Source IBMSIG for providing
- me with the original BASIC program. As with their code, this program is
- donated to the public domain.
-
- Ben Bacon - Source ID BDX678
- }
-
- type
- mthval = array [1..12] of integer;
- wkdays = array [1..7] of string[9];
- dstr = string[10];
- element = string[4];
-
- const
- Mths : mthval = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
- Days : wkdays = ('Monday', 'Tuesday', 'Wednesday', 'Thursday',
- 'Friday', 'Saturday', 'Sunday');
-
- var
- i, j : integer;
- DateStr : dstr;
- InptJDate : real;
- InptChar : char;
- continue : boolean;
-
- Weekday, DayNum, MthNum, YearNum, DaysLeft : real;
- LeapYr : integer;
- MthStr, DayStr : string[2];
- YearStr : string[4];
-
- procedure clr_eos;
- begin
- for i := 7 to 25 do
- begin
- gotoxy (1, i);
- clreol
- end;
- gotoxy (1, 7)
- end;
-
- procedure do_wait;
- var
- dummy : char;
- begin
- writeln ('Press any key to continue');
- read (KBD, dummy)
- end;
-
- function JulianDate (M, D, Y : integer) : real; {input MM/DD/YY}
- begin
- JulianDate := int ((Y * 365.25) + D + mths[M] + (M * 0.01) - 0.03)
- end;
-
- function TextDate (JDate : real) : dstr;
- begin
- LeapYr := 0;
- Weekday := JDate - 1 - int ((JDate - 1) / 7) * 7 + 1;
- YearNum := int (JDate / 365.25);
- if (trunc (YearNum) mod 4) = 0 then
- LeapYr := 1;
- DaysLeft := JDate - int (YearNum * 365.25 - 0.25);
- for i := 1 to 12 do
- begin
- if (i < 3) and (mths[i] < DaysLeft) then
- MthNum := i;
- if (i >= 3) and ((mths[i] + LeapYr) < DaysLeft) then
- MthNum := i
- end;
- if MthNum < 3 then
- DayNum := DaysLeft - mths[trunc (MthNum)]
- else
- DayNum := DaysLeft - mths[trunc (MthNum)] - LeapYr;
- str (MthNum:2:0, MthStr);
- if MthNum <= 9 then
- MthStr[1] := '0';
- str (DayNum:2:0, DayStr);
- if DayNum <= 9 then
- DayStr[1] := '0';
- str ((YearNum + 1900):4:0, YearStr);
- TextDate := MthStr + '/' + DayStr + '/' + YearStr
- end;
-
- procedure convert_gregorian;
- var
- MoVal, DyVal, YrVal : integer;
-
- begin
- writeln;
- repeat
- continue := true;
- write ('Enter 4-digit Year: ');
- readln (YrVal);
- if (YrVal < -999) or (YrVal > 9999) then
- begin
- continue := false;
- writeln ('ERROR - Valid years are -999 through 9999');
- end;
- until continue;
- repeat
- continue := true;
- write ('Enter # Of Month: ');
- readln (MoVal);
- if not (MoVal in [1..12]) then
- begin
- continue := false;
- writeln ('ERROR - Valid months are 1 through 12 (Jan - Dec)');
- end;
- until continue;
- repeat
- continue := true;
- write ('Enter Day: ');
- readln (DyVal);
- if (MoVal in [4, 6, 9, 11]) and not (DyVal in [1..30]) then
- begin
- continue := false;
- writeln ('ERROR - Valid days for this month are 1 through 30');
- end;
- if (MoVal in [1, 3, 5, 7, 8, 10, 12]) and not (DyVal in [1..31]) then
- begin
- continue := false;
- writeln ('ERROR - Valid days for this month are 1 through 31');
- end;
- if (MoVal = 2) and not (DyVal in [1..29]) then
- begin
- continue := false;
- writeln ('ERROR - Valid days for this month are 1 through 28');
- writeln (' (or 1 through 29 in a leap year)');
- end;
- if (MoVal = 2) and (DyVal = 29) and not ((YrVal mod 4) = 0) then
- begin
- continue := false;
- writeln ('ERROR - Not a leap year, day value incorrect');
- end;
- until continue;
- YrVal := YrVal - 1900;
- writeln;
- writeln ('Julian Date = ', JulianDate (MoVal, DyVal, YrVal):7:0);
- writeln
- end;
-
- procedure convert_julian;
- var
- InptJDate : real;
- begin
- writeln;
- write ('Enter A Julian Date: ');
- readln (InptJDate);
- writeln;
- if (InptJDate >= -1058859.0) and (InptJDate <= 2958524.0) then
- begin
- writeln ('Gregorian Date = ', TextDate (InptJDate):12);
- writeln ('Day Of Week = ', days[trunc (WeekDay)]:10);
- writeln
- end
- else
- begin
- writeln ('ERROR - Julian dates are integer numbers between');
- writeln ('-1,058,859 (1/1/-999) and 2,958,524 (12/31/9999).');
- writeln;
- writeln ('Press any key to continue');
- repeat until keypressed
- end;
- end;
-
-
- begin {DateConversion}
- clrscr;
- gotoxy (1, 1);
- writeln ('Date Conversion Routines in TURBO PASCAL');
- writeln;
- writeln ('Type "J" to convert a Julian date to a Gregorian date,');
- writeln ('"G" to convert a Gregorian date to a Julian date, or');
- writeln ('<ESC> to exit the program.');
- writeln;
- repeat
- clr_eos;
- read (KBD, InptChar);
- InptChar := upcase (InptChar);
- if not (InptChar in ['G', 'J', #27]) then
- write (^G)
- else
- case InptChar of
- 'J' : begin
- convert_julian;
- do_wait
- end;
- 'G' : begin
- convert_gregorian;
- do_wait
- end;
- end;
- until InptChar = #27;
- end.