home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0163_Dates for Delphi.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  6.2 KB  |  249 lines

  1.  
  2. unit JDates;
  3.  
  4. { A unit providing Julian day numbers and date manipulations.
  5.  
  6.   NOTE:
  7.    The range of Dates this unit will handle is 1/1/1900 to 1/1/2078
  8.  
  9.   Version 1.00 - 10/26/1987 - First general release
  10.  
  11.   Scott Bussinger
  12.   Professional Practice Systems
  13.   110 South 131st Street
  14.   Tacoma, WA  98444
  15.   (206)531-8944
  16.   Compuserve 72247,2671
  17.  
  18.   Version 1.01 - 10/09/1995 - Updated for use with Delphi v1.0
  19.                    Lets see some other code last this long without change
  20.  
  21.   Dennis Passmore
  22.   1929 Mango Tree Drive
  23.   Edgewater Fl, 32141
  24.  
  25.   Compuserve 71240,2464 }
  26.  
  27. interface
  28. uses
  29.   Sysutils;
  30.  
  31. const
  32.   BlankDate = $FFFF;                         { Constant for Not-a-real-Date }
  33.  
  34. type TDate = Word;
  35.      TDay = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
  36.      TDaySet = set of TDay;
  37.  
  38. procedure GetDate(var Year,Month,Day,Wday: Word);
  39.   { replacement for old WINDOS proc }
  40.  
  41. procedure GetTime(var Hour,Min,Sec,MSec: Word);
  42.   { replacement for old WINDOS proc }
  43.  
  44. function  CurrentJDate: Tdate;
  45.  
  46. function  ValidDate(Day,Month,Year: Word): boolean;
  47.   { Check if the day,month,year is a real date storable in a Date variable }
  48.  
  49.  
  50. procedure DMYtoDate(Day,Month,Year: Word;var Julian: TDate);
  51.   { Convert from day,month,year to a date }
  52.  
  53. procedure DateToDMY(Julian: TDate;var Day,Month,Year: Word);
  54.   { Convert from a date to day,month,year }
  55.  
  56. function BumpDate(Julian: TDate;Days,Months,Years: Integer): TDate;
  57.   { Add (or subtract) the number of days, months, and years to a date }
  58.  
  59. function DayOfWeek(Julian: TDate): TDay;
  60.   { Return the day of the week for the date }
  61.  
  62. function DayString(WeekDay: TDay): string;
  63.   { Return a string version of a day of the week }
  64.  
  65. function MonthString(Month: Word): string;
  66.   { Return a string version of a month }
  67.  
  68. function DateToStr(Julian: TDate): string;
  69.   { Convert a date to a sortable string }
  70.  
  71. function StrToDate(StrVar: string): TDate;
  72.   { Convert a sortable string form to a date }
  73.  
  74. implementation
  75.  
  76. procedure GetDate(var Year,Month,Day,Wday: Word);
  77. var
  78.   td: TDatetime;
  79. begin
  80.   td := Date;
  81.  
  82.   DeCodeDate(td,Year,Month,Day);
  83.   Wday := sysutils.DayofWeek(td);
  84. end;
  85.  
  86. procedure GetTime(var Hour,Min,Sec,MSec: Word);
  87. var
  88.   td: TDatetime;
  89. begin
  90.   td := Now;
  91.   DecodeTime(td,Hour,Min,Sec,MSec);
  92. end;
  93.  
  94. function  CurrentJdate: Tdate;
  95. var
  96.  y,m,d,w: word;
  97.  jd: TDate;
  98. begin
  99.   GetDate(y,m,d,w);
  100.   DMYtoDate(d,m,y,jd);
  101.   CurrentJDate:= jd;
  102. end;
  103.  
  104. function ValidDate(Day,Month,Year: Word): boolean;
  105.   { Check if the day,month,year is a real date storable in a Date variable }
  106. begin
  107.   if {(Day<1) or }(Year<1900) or (Year>2078) then
  108.     ValidDate := false
  109.   else
  110.     case Month of
  111.       1,3,5,7,8,10,12: ValidDate := Day <= 31;
  112.  
  113.       4,6,9,11: ValidDate := Day <= 30;
  114.       2: ValidDate := Day <= 28 + ord((Year mod 4)=0)*ord(Year<>1900)
  115.       else ValidDate := false
  116.     end
  117. end;
  118.  
  119. procedure DMYtoDate(Day,Month,Year: Word;var Julian: TDate);
  120.   { Convert from day,month,year to a date }
  121.   { Stored as number of days since January 1, 1900 }
  122.   { Note that no error checking takes place in this routine -- use ValidDate }
  123. begin
  124. if (Year=1900) and (Month<3) then
  125.   if Month = 1 then
  126.     Julian := pred(Day)
  127.   else
  128.     Julian := Day + 30
  129. else
  130.   begin
  131.     if Month > 2 then
  132.       dec(Month,3)
  133.     else
  134.       begin
  135.         inc(Month,9);
  136.         dec(Year)
  137.  
  138.       end;
  139.     dec(Year,1900);
  140.     Julian := (1461*longint(Year) div 4) + ((153*Month+2) div 5) + Day + 58;
  141.   end
  142. end;
  143.  
  144. procedure DateToDMY(Julian: TDate;var Day,Month,Year: Word);
  145.   { Convert from a date to day,month,year }
  146. var
  147.   LongTemp: longint;
  148.       Temp: Word;
  149. begin
  150.   if Julian <= 58 then
  151.     begin
  152.       Year := 1900;
  153.       if Julian <= 30 then
  154.         begin
  155.           Month := 1;
  156.           Day := succ(Julian)
  157.         end
  158.       else
  159.         begin
  160.           Month := 2;
  161.           Day := Julian - 30
  162.         end
  163.     end
  164.   else
  165.     begin
  166.       LongTemp := 4*longint(Julian) - 233;
  167.  
  168.       Year := LongTemp div 1461;
  169.       Temp := LongTemp mod 1461 div 4 * 5 + 2;
  170.       Month := Temp div 153;
  171.       Day := Temp mod 153 div 5 + 1;
  172.       inc(Year,1900);
  173.       if Month < 10 then
  174.         inc(Month,3)
  175.       else
  176.         begin
  177.           dec(Month,9);
  178.           inc(Year)
  179.         end
  180.     end
  181. end;
  182.  
  183. function BumpDate(Julian: TDate;Days,Months,Years: Integer): TDate;
  184.   { Add (or subtract) the number of days, months, and years to a date }
  185.   { Note that months and years are added first before days }
  186.   { Note further that there are no overflow/underflow checks }
  187. var Day: Word;
  188.     Month: Word;
  189.     Year: Word;
  190. begin
  191.   DateToDMY(Julian,Day,Month,Year);
  192.  
  193.   Month := Month + Months - 1;
  194.   Year := Year + Years + (Month div 12) - ord(Month<0);
  195.   Month := (Month + 12000) mod 12 + 1;
  196.   DMYtoDate(Day,Month,Year,Julian);
  197.   BumpDate := Julian + Days
  198. end;
  199.  
  200. function DayOfWeek(Julian: TDate): TDay;
  201.   { Return the day of the week for the date }
  202. begin
  203.   DayOfWeek := TDay(succ(Julian) mod 7)
  204. end;
  205.  
  206. function DayString(WeekDay: TDay): string;
  207.   { Return a string version of a day of the week }
  208. const DayStr: array[Sunday..Saturday] of string[9] =
  209.      ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  210.  
  211. begin
  212.   DayString := DayStr[WeekDay]
  213. end;
  214.  
  215. function MonthString(Month: Word): string;
  216.   { Return a string version of a month }
  217.   const MonthStr: array[1..12] of string[9] =
  218.      ('January','February','March','April','May','June','July','August',
  219.                                  'September','October','November','December');
  220. begin
  221.   MonthString := MonthStr[Month]
  222. end;
  223.  
  224. function DateToStr(Julian: TDate): string;
  225.   { Convert a date to a sortable string - NOT displayable }
  226. const tResult: record
  227.                 case integer of
  228.                   0: (Len: byte;  W: word);
  229.                   1: (Str: string[2])
  230.  
  231.                 end = (Str:'  ');
  232. begin
  233.   tResult.W := swap(Julian);
  234.   DateToStr := tResult.Str
  235. end;
  236.  
  237. function StrToDate(StrVar: string): TDate;
  238.   { Convert a sortable string form to a date }
  239. var Temp: record
  240.             Len: byte;
  241.               W: word
  242.           end absolute StrVar;
  243. begin
  244.   StrToDate := swap(Temp.W)
  245. end;
  246.  
  247. end.
  248.  
  249.