home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / DATES.ZIP / DATES.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1988-04-21  |  6.5 KB  |  240 lines

  1. unit Dates;
  2.  
  3. { A unit providing Julian day numbers and date manipulations.
  4.  
  5.   Version 1.03 -  4/21/1988 - Removed compiler directives (just uses defaults)
  6.           1.02 -  4/13/1988 - Changed Today from variable to a function
  7.                               Added Age function
  8.           1.01 - 11/25/1987 - Added Today variable
  9.                               Changed day, month and year types to words
  10.           1.00 - 10/26/1987 - First general release
  11.  
  12.   Scott Bussinger
  13.   Professional Practice Systems
  14.   110 South 131st Street
  15.   Tacoma, WA  98444
  16.   (206)531-8944
  17.   Compuserve 72247,2671 }
  18.  
  19.  
  20. interface
  21.  
  22. uses Dos;
  23.  
  24. const BlankDate = $FFFF;                         { Constant for Not-a-real-Date }
  25.  
  26. type Date = Word;
  27.      Day = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
  28.  
  29. function ValidDate(Day,Month,Year: word): boolean;
  30.   { Check if the day,month,year is a real date storable in a Date variable }
  31.  
  32. procedure DMYtoDate(Day,Month,Year: word;var Julian: Date);
  33.   { Convert from day,month,year to a date }
  34.  
  35. procedure DateToDMY(Julian: Date;var Day,Month,Year: word);
  36.   { Convert from a date to day,month,year }
  37.  
  38. function BumpDate(Julian: Date;Days,Months,Years: integer): Date;
  39.   { Add (or subtract) the number of days, months, and years to a date }
  40.  
  41. function CurrentAge(Birthdate: Date): word;
  42.   { Return the current age of a person in years from a given date of birth }
  43.  
  44. function DayOfWeek(Julian: Date): Day;
  45.   { Return the day of the week for the date }
  46.  
  47. function Today: Date;
  48.   { Return the current system date }
  49.  
  50. function DayString(WeekDay: Day): string;
  51.   { Return a string version of a day of the week }
  52.  
  53. function MonthString(Month: word): string;
  54.   { Return a string version of a month }
  55.  
  56. function DateToStr(Julian: Date): string;
  57.   { Convert a date to a sortable string }
  58.  
  59. function StrToDate(StrVar: string): Date;
  60.   { Convert a sortable string form to a date }
  61.  
  62.  
  63. implementation
  64.  
  65. function ValidDate(Day,Month,Year: word): boolean;
  66.   { Check if the day,month,year is a real date storable in a Date variable }
  67.   begin
  68.   if (Day<1) or (Year<1900) or (Year>2078)
  69.    then
  70.     ValidDate := false
  71.    else
  72.     case Month of
  73.       1,3,5,7,8,10,12: ValidDate := Day <= 31;
  74.       4,6,9,11: ValidDate := Day <= 30;
  75.       2: ValidDate := Day <= 28 + ord((Year mod 4)=0)*ord(Year<>1900)
  76.       else ValidDate := false
  77.       end
  78.   end;
  79.  
  80. procedure DMYtoDate(Day,Month,Year: word;var Julian: Date);
  81.   { Convert from day,month,year to a date }
  82.   { Stored as number of days since January 1, 1900 }
  83.   { Note that no error checking takes place in this routine -- use ValidDate }
  84.   begin
  85.   if (Year=1900) and (Month<3)
  86.    then
  87.     if Month = 1
  88.      then
  89.       Julian := pred(Day)
  90.      else
  91.       Julian := Day + 30
  92.    else
  93.     begin
  94.     if Month > 2
  95.      then
  96.       dec(Month,3)
  97.      else
  98.       begin
  99.       inc(Month,9);
  100.       dec(Year)
  101.       end;
  102.     dec(Year,1900);
  103.     Julian := (1461*longint(Year) div 4) + ((153*Month+2) div 5) + Day + 58
  104.     end
  105.   end;
  106.  
  107. procedure DateToDMY(Julian: Date;var Day,Month,Year: word);
  108.   { Convert from a date to day,month,year }
  109.   var LongTemp: longint;
  110.       Temp: integer;
  111.   begin
  112.   if Julian <= 58
  113.    then
  114.     begin
  115.     Year := 1900;
  116.     if Julian <= 30
  117.      then
  118.       begin
  119.       Month := 1;
  120.       Day := succ(Julian)
  121.       end
  122.      else
  123.       begin
  124.       Month := 2;
  125.       Day := Julian - 30
  126.       end
  127.     end
  128.    else
  129.     begin
  130.     LongTemp := 4*longint(Julian) - 233;
  131.     Year := LongTemp div 1461;
  132.     Temp := LongTemp mod 1461 div 4 * 5 + 2;
  133.     Month := Temp div 153;
  134.     Day := Temp mod 153 div 5 + 1;
  135.     inc(Year,1900);
  136.     if Month < 10
  137.      then
  138.       inc(Month,3)
  139.      else
  140.       begin
  141.       dec(Month,9);
  142.       inc(Year)
  143.       end
  144.     end
  145.   end;
  146.  
  147. function BumpDate(Julian: Date;Days,Months,Years: integer): Date;
  148.   { Add (or subtract) the number of days, months, and years to a date }
  149.   { Note that months and years are added first before days }
  150.   { Note further that there are no overflow/underflow checks }
  151.   var Day: word;
  152.       Month: word;
  153.       Year: word;
  154.   begin
  155.   DateToDMY(Julian,Day,Month,Year);
  156.   Month := Month + Months - 1;
  157.   Year := Year + Years + (Month div 12) - ord(Month<0);
  158.   Month := (Month + 12000) mod 12 + 1;
  159.   DMYtoDate(Day,Month,Year,Julian);
  160.   BumpDate := Julian + Days
  161.   end;
  162.  
  163. function CurrentAge(Birthdate: Date): word;
  164.   { Return the current age of a person in years from a given date of birth }
  165.   var BirthDay: word;
  166.       BirthMonth: word;
  167.       BirthYear: word;
  168.       Temp: word;
  169.       TodayDay: word;
  170.       TodayMonth: word;
  171.       TodayYear: word;
  172.   begin
  173.   DateToDMY(Birthdate,BirthDay,BirthMonth,BirthYear);
  174.   DateToDMY(Today,TodayDay,TodayMonth,TodayYear);
  175.   Temp := TodayYear - BirthYear;
  176.   if (TodayMonth<BirthMonth) or ((TodayMonth=BirthMonth) and (TodayDay<BirthDay)) then
  177.     dec(Temp);
  178.   CurrentAge := Temp
  179.   end;
  180.  
  181. function DayOfWeek(Julian: Date): Day;
  182.   { Return the day of the week for the date }
  183.   begin
  184.   DayOfWeek := Day(succ(Julian) mod 7)
  185.   end;
  186.  
  187. function Today: Date;
  188.   { Return the current system date }
  189.   var Day: word;
  190.       DontCare: word;
  191.       Month: word;
  192.       Temp: Date;
  193.       Year: word;
  194.   begin
  195.   GetDate(Year,Month,Day,DontCare);              { Get today's date from system }
  196.   DMYtoDate(Day,Month,Year,Temp);
  197.   Today := Temp
  198.   end;
  199.  
  200. function DayString(WeekDay: Day): string;
  201.   { Return a string version of a day of the week }
  202.   const DayStr: array[Sunday..Saturday] of string[9] =
  203.           ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  204.   begin
  205.   DayString := DayStr[WeekDay]
  206.   end;
  207.  
  208. function MonthString(Month: word): string;
  209.   { Return a string version of a month }
  210.   const MonthStr: array[1..12] of string[9] =
  211.           ('January','February','March','April','May','June','July','August','September','October','November','December');
  212.   begin
  213.   MonthString := MonthStr[Month]
  214.   end;
  215.  
  216. function DateToStr(Julian: Date): string;
  217.   { Convert a date to a sortable string }
  218.   const Result: record
  219.           case integer of
  220.             0: (Len: byte;
  221.                 W: word);
  222.             1: (Str: string[2])
  223.           end = (Str:'  ');
  224.   begin
  225.   Result.W := swap(Julian);
  226.   DateToStr := Result.Str
  227.   end;
  228.  
  229. function StrToDate(StrVar: string): Date;
  230.   { Convert a sortable string form to a date }
  231.   var Temp: record
  232.         Len: byte;
  233.         W: word
  234.         end absolute StrVar;
  235.   begin
  236.   StrToDate := swap(Temp.W)
  237.   end;
  238.  
  239. end.
  240.