home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d45 / ARDOCI.ZIP / GoodDate.pas < prev    next >
Pascal/Delphi Source File  |  2001-03-01  |  9KB  |  368 lines

  1. unit GoodDate;
  2.  
  3. interface
  4.  
  5. // Unix-like datetime(seconds from 1 Jan 1970) *
  6. function GetLocalUDateTime:int64;
  7. function GetLocalUDate:integer;
  8. function GetUDateFromUDateTime(UDateTime:int64):integer;
  9. function GetUTimeFromUDateTime(UDateTime:int64):integer;
  10. procedure UnMakeUDate(UDate:integer;var Year, Month, Day: Word);
  11. procedure UnMakeUTime(UTime:integer;var Hour, Min, Sec: Word);
  12. function MakeUDate(Year, Month, Day: Word):integer;
  13. function MakeUTime(Hour, Min, Sec: Word):integer;
  14. function UDateToSDate(UDate:integer):string;
  15. function UTimeToSTime(UTime:integer):string;
  16. function UDateToDateTime(UDate:integer):TDateTime;
  17. function UTimeToDateTime(UTime:integer):TDateTime;
  18. function DateTimeToUDate(DateTime:TDateTime):integer;
  19. function DateTimeToUTime(DateTime:TDateTime):integer;
  20.  
  21. function MakeGoodDate(Year, Month, Day: Word):integer;
  22. function MakeGoodTime(Hour, Min, Sec, MSec: Word):integer;
  23. function MakeGoodDateTime(Year, Month, Day, Hour, Min, Sec, MSec: Word):int64;
  24. procedure UnMakeGoodDate(ADate:integer;var Year, Month, Day: Word);
  25. procedure UnMakeGoodTime(ATime:integer;var Hour, Min, Sec, MSec: Word);
  26. procedure UnMakeGoodDateTime(ADateTime:int64;var Year, Month, Day: Word;var Hour, Min, Sec, MSec: Word);
  27.  
  28. function DateTimeToGoodDate(DDate:TDateTime):integer;
  29. function DateTimeToGoodTime(DDate:TDateTime):integer;
  30. function DateTimeToGoodDateTime(DDate:TDateTime):int64;
  31.  
  32. function GoodDateToDateTime(ADate:integer):TDateTime;
  33. function GoodTimeToDateTime(ADate:integer):TDateTime;
  34. function GoodDateTimeToDateTime(ADate:int64):TDateTime;
  35.  
  36.  
  37. implementation
  38. uses SysUtils, Windows;
  39.  
  40. function MakeGoodDate(Year, Month, Day: Word):integer;
  41. var
  42.   I: Integer;
  43.   DayTable: PDayTable;
  44. begin
  45.   Result := 0;
  46.   DayTable := @MonthDays[IsLeapYear(Year)];
  47.   if (Year >= 1) and (Year <= 9999) and (Month >= 1) and (Month <= 12) and
  48.     (Day >= 1) and (Day <= DayTable^[Month]) then
  49.    begin
  50.     for I := 1 to Month - 1 do Inc(Day, DayTable^[I]);
  51.     I := Year - 1;
  52.     Result := I * 365 + I div 4 - I div 100 + I div 400 + Day;
  53.    end;
  54. end;
  55.  
  56. function MakeGoodTime(Hour, Min, Sec, MSec: Word):integer;
  57. begin
  58.   Result := 0;
  59.   if (Hour < 24) and (Min < 60) and (Sec < 60) and (MSec < 1000) then
  60.     Result := Hour * 3600000 + Min * 60000 + Sec * 1000 + MSec;
  61. end;
  62.  
  63. function MakeGoodDateTime(Year, Month, Day, Hour, Min, Sec, MSec: Word):int64;
  64. begin
  65.  Result:=MakeGoodDate(Year, Month, Day);
  66.  Result:=Result*MSecsPerDay;
  67.  Result:=Result+MakeGoodTime(Hour, Min, Sec, MSec);
  68. end;
  69.  
  70. procedure DivMod(Dividend: Integer; Divisor: Word;
  71.   var Result, Remainder: Word);
  72. asm
  73.         PUSH    EBX
  74.         MOV     EBX,EDX
  75.         MOV     EDX,EAX
  76.         SHR     EDX,16
  77.         DIV     BX
  78.         MOV     EBX,Remainder
  79.         MOV     [ECX],AX
  80.         MOV     [EBX],DX
  81.         POP     EBX
  82. end;
  83.  
  84. procedure UnMakeGoodDate(ADate:integer;var Year, Month, Day: Word);
  85. const
  86.   D1 = 365;
  87.   D4 = D1 * 4 + 1;
  88.   D100 = D4 * 25 - 1;
  89.   D400 = D100 * 4 + 1;
  90. var
  91.   Y, M, D, I: Word;
  92.   T:integer;
  93.   DayTable: PDayTable;
  94. begin
  95.   T:=ADate;
  96.   if T <= 0 then
  97.   begin
  98.     Year := 0;
  99.     Month := 0;
  100.     Day := 0;
  101.   end else
  102.   begin
  103.     Dec(T);
  104.     Y := 1;
  105.     while T >= D400 do
  106.     begin
  107.       Dec(T, D400);
  108.       Inc(Y, 400);
  109.     end;
  110.     DivMod(T, D100, I, D);
  111.     if I = 4 then
  112.     begin
  113.       Dec(I);
  114.       Inc(D, D100);
  115.     end;
  116.     Inc(Y, I * 100);
  117.     DivMod(D, D4, I, D);
  118.     Inc(Y, I * 4);
  119.     DivMod(D, D1, I, D);
  120.     if I = 4 then
  121.     begin
  122.       Dec(I);
  123.       Inc(D, D1);
  124.     end;
  125.     Inc(Y, I);
  126.     DayTable := @MonthDays[IsLeapYear(Y)];
  127.     M := 1;
  128.     while True do
  129.     begin
  130.       I := DayTable^[M];
  131.       if D < I then Break;
  132.       Dec(D, I);
  133.       Inc(M);
  134.     end;
  135.     Year := Y;
  136.     Month := M;
  137.     Day := D + 1;
  138.   end;
  139. end;
  140.  
  141. procedure UnMakeGoodTime(ATime:integer;var Hour, Min, Sec, MSec: Word);
  142. var
  143.   MinCount, MSecCount: Word;
  144. begin
  145.   DivMod(ATime, 60000, MinCount, MSecCount);
  146.   DivMod(MinCount, 60, Hour, Min);
  147.   DivMod(MSecCount, 1000, Sec, MSec);
  148. end;
  149.  
  150. procedure UnMakeGoodDateTime(ADateTime:int64;var Year, Month, Day: Word;var Hour, Min, Sec, MSec: Word);
  151. begin
  152.  UnMakeGoodDate(ADateTime div MSecsPerDay,Year,Month,Day);
  153.  UnMakeGoodTime(ADateTime mod MSecsPerDay,Hour,Min,Sec,MSec);
  154. end;
  155.  
  156. function DateTimeToGoodDate(DDate:TDateTime):integer;
  157. begin
  158.  Result:=trunc(DDate)+DateDelta;
  159. end;
  160.  
  161. function DateTimeToGoodTime(DDate:TDateTime):integer;
  162. begin
  163.  Result:=trunc(MSecsPerDay*frac(DDate));
  164. end;
  165.  
  166. function DateTimeToGoodDateTime(DDate:TDateTime):int64;
  167. begin
  168.  Result:=(trunc(DDate)+DateDelta);
  169.  Result:=Result*MSecsPerDay+trunc(frac(DDate)*MSecsPerDay);
  170. end;
  171.  
  172. function GoodDateToDateTime(ADate:integer):TDateTime;
  173. begin
  174.  Result:=ADate-DateDelta;
  175. end;
  176.  
  177. function GoodTimeToDateTime(ADate:integer):TDateTime;
  178. begin
  179.  Result:=(ADate*1.0)/MSecsPerDay;
  180. end;
  181.  
  182. function GoodDateTimeToDateTime(ADate:int64):TDateTime;
  183. begin
  184.  Result:=((ADate div MSecsPerDay)-DateDelta)+((ADate mod MSecsPerDay)*1.0)/MSecsPerDay;
  185. end;
  186.  
  187. function GetLocalUDateTime:int64;
  188. var
  189.   SystemTime:TSystemTime;
  190.   DayTable:PDayTable;
  191. begin
  192.  Result:=0;
  193.  GetLocalTime(SystemTime);
  194.  
  195.  with SystemTime do begin
  196.   DayTable := @MonthDays[IsLeapYear(wYear)];
  197.   if (wHour >= 24) or (wMinute >= 60) or (wSecond >= 60) or (wMilliSeconds >= 1000) or
  198.      (wYear < 1) or (wYear > 9999) or (wMonth < 1) or (wMonth > 12) or
  199.      (wDay < 1) and (wDay > DayTable^[wMonth]) then exit;
  200.  
  201.   Result:=MakeUDate(wYear,wMonth,wDay)*24*60*60+MakeUTime(wHour,wMinute,wSecond);
  202.  end;
  203. end;
  204.  
  205. function GetLocalUDate:integer;
  206. var
  207.   SystemTime:TSystemTime;
  208.   DayTable: PDayTable;
  209. begin
  210.  Result:=0;
  211.  GetLocalTime(SystemTime);
  212.  
  213.  with SystemTime do begin
  214.   DayTable := @MonthDays[IsLeapYear(wYear)];
  215.   if (wYear < 1) or (wYear > 9999) or (wMonth < 1) or (wMonth > 12) or
  216.      (wDay < 1) and (wDay > DayTable^[wMonth]) then exit;
  217.  
  218.   Result:=MakeUDate(wYear,wMonth,wDay);
  219.  end;
  220. end;
  221.  
  222. function GetUDateFromUDateTime(UDateTime:int64):integer;
  223. begin
  224.  Result:=UDateTime div (24*60*60);
  225. end;
  226.  
  227. function GetUTimeFromUDateTime(UDateTime:int64):integer;
  228. begin
  229.  Result:=UDateTime mod (24*60*60);
  230. end;
  231.  
  232. procedure UnMakeUDate(UDate:integer;var Year, Month, Day: Word);
  233. const
  234.   D1 = 365;
  235.   D4 = D1 * 4 + 1;
  236.   D100 = D4 * 25 - 1;
  237.   D400 = D100 * 4 + 1;
  238. var
  239.   Y, M, D, I: Word;
  240.   T:integer;
  241.   DayTable: PDayTable;
  242. begin
  243.   T:=UDate;
  244.   if T <= 0 then
  245.   begin
  246.     Year := 0;
  247.     Month := 0;
  248.     Day := 0;
  249.   end else
  250.   begin
  251.     Dec(T);
  252.     Y := 1;
  253.     while T >= D400 do
  254.     begin
  255.       Dec(T, D400);
  256.       Inc(Y, 400);
  257.     end;
  258.     DivMod(T, D100, I, D);
  259.     if I = 4 then
  260.     begin
  261.       Dec(I);
  262.       Inc(D, D100);
  263.     end;
  264.     Inc(Y, I * 100);
  265.     DivMod(D, D4, I, D);
  266.     Inc(Y, I * 4);
  267.     DivMod(D, D1, I, D);
  268.     if I = 4 then
  269.     begin
  270.       Dec(I);
  271.       Inc(D, D1);
  272.     end;
  273.     Inc(Y, I);
  274.     DayTable := @MonthDays[IsLeapYear(Y)];
  275.     M := 1;
  276.     while True do
  277.     begin
  278.       I := DayTable^[M];
  279.       if D < I then Break;
  280.       Dec(D, I);
  281.       Inc(M);
  282.     end;
  283.     Year := Y+1970;
  284.     Month := M;
  285.     Day := D + 1;
  286.   end;
  287. end;
  288.  
  289. procedure UnMakeUTime(UTime:integer;var Hour, Min, Sec: Word);
  290. begin
  291.  Hour:=UTime div (60*60);
  292.  Min:=(UTime mod (60*60)) div 60;
  293.  Sec:=UTime mod 60;
  294. end;
  295.  
  296. function UDateToSDate(UDate:integer):string;
  297. var Year, Month, Day: Word;
  298. begin
  299.  UnMakeUDate(UDate,Year,Month,Day);
  300.  Format('%2d.%2d.%4d',[Day,Month,Year]);
  301. end;
  302.  
  303. function UTimeToSTime(UTime:integer):string;
  304. var Hour, Min, Sec: Word;
  305. begin
  306.  UnMakeUTime(UTime,Hour,Min,Sec);
  307.  Format('%2d:%2d:%2d',[Hour,Min,Sec]);
  308. end;
  309.  
  310. function MakeUDate(Year, Month, Day: Word):integer;
  311. var
  312.   I: Integer;
  313.   DayTable: PDayTable;
  314. begin
  315.   Result := 0;
  316.   DayTable := @MonthDays[IsLeapYear(Year)];
  317.   if (Year >= 1) and (Year <= 9999) and (Month >= 1) and (Month <= 12) and
  318.     (Day >= 1) and (Day <= DayTable^[Month]) then
  319.    begin
  320.     for I := 1 to Month - 1 do Inc(Day, DayTable^[I]);
  321.     I := Year - 1970;
  322.     Result := I * 365 + I div 4 - I div 100 + I div 400 + Day;
  323.    end;
  324. end;
  325.  
  326. function MakeUTime(Hour, Min, Sec: Word):integer;
  327. begin
  328.  Result := 0;
  329.  if (Hour < 24) and (Min < 60) and (Sec < 60) then
  330.    Result := Hour * 60*60 + Min * 60 + Sec;
  331. end;
  332.  
  333. function UDateToDateTime(UDate:integer):TDateTime;
  334. var Y,M,D:word;
  335. begin
  336.  if UDate=0 then Result:=0
  337.   else begin
  338.    UnMakeUDate(UDate,Y,M,D);
  339.    Result:=EncodeDate(Y,M,D);
  340.   end;
  341. end;
  342.  
  343. function UTimeToDateTime(UTime:integer):TDateTime;
  344. var H,M,S:word;
  345. begin
  346.  if UTime=0 then Result:=0
  347.   else begin
  348.    UnMakeUTime(UTime,H,M,S);
  349.    Result:=EncodeTime(H,M,S,0);
  350.   end;
  351. end;
  352.  
  353. function DateTimeToUDate(DateTime:TDateTime):integer;
  354. var Y,M,D:word;
  355. begin
  356.  DecodeDate(DateTime,Y,M,D);
  357.  Result:=MakeUDate(Y,M,D);
  358. end;
  359.  
  360. function DateTimeToUTime(DateTime:TDateTime):integer;
  361. var H,M,S,MS:word;
  362. begin
  363.  DecodeTime(DateTime,H,M,S,MS);
  364.  Result:=MakeUTime(H,M,S);
  365. end;
  366.  
  367. end.
  368.