home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / LANGUAGS / PASCAL / JDATE.PAS < prev    next >
Pascal/Delphi Source File  |  2000-06-30  |  4KB  |  107 lines

  1. program julian;
  2. var
  3. julian,year,month,day : integer;
  4.  
  5. procedure DtoJ(Day,Month,Year: integer;var Julian: integer);
  6.   { Convert from a date to a Julian number -- January 1, 1900 = -32767 }
  7.   { Note that much care is taken to avoid problems with inaccurate bit representations inherent in the binary fractions
  8.     of the real numbers used as temporary variables.  Thus the seemingly unnecessary use of small fractional offsets
  9.     and int() functions }
  10.   begin
  11.   if (Year=1900) and (Month<3)                   { Handle the first two months as a special case since the general }
  12.    then                                          {   algorithm used doesn't start until March 1, 1900 }
  13.     if Month=1
  14.      then
  15.       Julian := Day-$8000                        { Compiler won't accept -32768 as a valid integer, so use the hex form }
  16.      else
  17.       Julian := Day-32737
  18.    else
  19.     begin
  20.     if Month>2
  21.      then
  22.       Month := Month-3
  23.      else
  24.       begin
  25.       Month := Month+9;
  26.       Year := Year-1
  27.       end;
  28.     Year := Year-1900;
  29.     Julian := round(-32709.0+Day+int(0.125+int(1461.0*Year+0.5)/4.0))+((153*Month+2) div 5)
  30.     end
  31.   end;
  32.  
  33. procedure JtoD(Julian: integer;var Day,Month,Year: integer);
  34.   { Convert from a Julian date to a calendar date }
  35.   { Note that much care is taken to avoid problems with inaccurate bit representations inherent in the binary fractions
  36.     of the real numbers used as temporary variables.  Thus the seemingly unnecessary use of small fractional offsets
  37.     and int() functions }
  38.   var Temp: real;
  39.   begin
  40.   Temp := int(32767.5+Julian);                   { Convert 16 bit quantity into a real number }
  41.   if Temp<58.5
  42.    then
  43.     begin                                        { The first two months of the twentieth century are handled as a special }
  44.     Year := 1900;                                {   case of the general algorithm used which handles all of the rest }
  45.     if Temp<30.5
  46.      then
  47.       begin
  48.       Month := 1;
  49.       Day := round(Temp+1.0)
  50.       end
  51.      else
  52.       begin
  53.       Month := 2;
  54.       Day := round(Temp-30.0)
  55.       end
  56.     end
  57.    else
  58.     begin
  59.     Temp := int(4.0*(Temp-59.0)+3.5);
  60.     Year := trunc(Temp/1461.0+0.00034223);     { 0.00034223 is about one half of the reciprocal of 1461.0 }
  61.     Day := succ(round(Temp-Year*1461.0) div 4);
  62.     Month := (5*Day-3) div 153;
  63.     Day := succ((5*Day-3) mod 153 div 5);
  64.     Year := Year+1900;
  65.     if Month<10
  66.      then
  67.       Month := Month+3
  68.      else
  69.       begin
  70.       Month := Month-9;
  71.       Year := succ(Year)
  72.       end
  73.     end
  74.   end;
  75.  
  76. function DayOfWeek(Julian: integer): integer;
  77.   { Return an integer representing the day of week for the date }
  78.   { Sunday = 0, etc. }
  79.   var Temp: real;
  80.   begin
  81.   Temp := Julian+32767.0;                        { Convert into a real temporary variable }
  82.   DayOfWeek := round(frac((Temp+1.0)/7.0)*7.0)   { Essentially this is a real number version of Julian mod 7 with }
  83.   end;                                           { an offset to make Sunday = 0 }
  84.  
  85. procedure WriteDate(Julian: integer);
  86.   { Write the date out to the console in long form , e.g. "Monday, September 10, 1984" }
  87.   const Days: array[0..6] of string[9]=('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  88.         Months: array[1..12] of string[9] = ('January','February','March','April','May','June',
  89.                                              'July','August','September','October','November','December');
  90.   var Day,Month,Year: integer;
  91.   begin
  92.   JtoD(Julian,Day,Month,Year);                   { Convert into date form }
  93.   writeln(Days[DayOfWeek(Julian)]);
  94.   Write(Months[Month],' ',Day,', ',Year);
  95.   end;
  96.  
  97. begin
  98.   clrscr;
  99.   write('Day    ');   readln(Day);
  100.   write('Month  ');   readln(month);
  101.   write('Year   ');   readln(Year);
  102.   year := year + 1900;
  103.   DtoJ(Day,Month,Year,Julian);
  104.   writeln(julian);
  105.   writedate(julian);
  106.  
  107. end.