ctime.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. unit CTime;
  2. InterFace
  3. const
  4. DefaultTimeZone = 5;
  5. timezone = DefaultTimeZone * 60 * 60;
  6. CLOCKS_PER_SEC = 18.2;
  7. CLK_TCK = 18.2;
  8. Days : array [0..11] of byte =
  9. (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  10. YDays : array [0..11] of integer = (
  11. 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  12. );
  13. type
  14. size_t = word;
  15. time_t = longint;
  16. clock_t = longint;
  17. tm = record
  18. tm_sec,tm_min,tm_hour,tm_mday,tm_mon,tm_year: integer;
  19. end;
  20. Procedure gmtime(var time:time_t; var Res:tm);
  21. Function mktime(t:tm):time_t;
  22. Function time:time_t;
  23. Procedure UnixToDos(time:time_t;var t:tm;TZ:shortInt);
  24. Implementation
  25. Uses Dos;
  26. Procedure gmtime(var time:time_t; var res:tm);
  27. var
  28. hpery : integer;
  29. i,cumdays : word;
  30. begin
  31. if (time < 0) then time := 0;
  32. dec(time,4*60*60);
  33. res.tm_sec := time mod 60;
  34. time := time div 60;
  35. res.tm_min := time mod 60;
  36. time := time div 60;
  37. i := time div (1461 * 24);
  38. res.tm_year := (i shl 2);
  39. inc(res.tm_year,70);
  40. cumdays := 1461 * i;
  41. time := time mod (1461 * 24);
  42. repeat
  43. hpery := 365 * 24;
  44. if ((res.tm_year and 3) = 0) then
  45. inc(hpery,24);
  46. if (time < hpery) then
  47. break;
  48. inc(cumdays,hpery div 24);
  49. inc(res.tm_year);
  50. time := time - hpery;
  51. until false;
  52. res.tm_hour := (time mod 24);
  53. time := time div 24;
  54. inc(cumdays,time + 4);
  55. inc(time);
  56. if (res.tm_year and 3) = 0 then
  57. begin
  58. if (time > 60)then
  59. dec(time)
  60. else
  61. if (time = 60) then
  62. begin
  63. res.tm_mon := 1;
  64. res.tm_mday := 29;
  65. exit;
  66. end;
  67. end;
  68. res.tm_mon := 0;
  69. while days[res.tm_mon] < time do
  70. begin
  71. dec(time,Days[res.tm_mon]);
  72. inc(res.tm_mon);
  73. end;
  74. res.tm_mday := (time);
  75. end;
  76. function totalsec(year,month,day,hour,min,sec:integer) : longint;
  77. var
  78. leaps:integer;
  79. days1, secs : time_t;
  80. begin
  81. if ((year < 70) or (year > 138)) then
  82. begin
  83. TotalSec:=-1;
  84. exit;
  85. end;
  86. inc(min,sec div 60);
  87. sec := sec mod 60;
  88. inc(hour,min div 60);
  89. min := min mod 60;
  90. inc(day,hour div 24);
  91. hour := hour mod 24;
  92. inc(year,month div 12);
  93. month := month mod 12;
  94. while (day >= Days[month]) do
  95. begin
  96. if (not((year and 3)<>0) and (month = 1)) then
  97. begin
  98. if (day > 28) then
  99. begin
  100. dec(day,29);
  101. inc(month);
  102. end
  103. else
  104. break;
  105. end
  106. else
  107. begin
  108. dec(day,Days[month]);
  109. inc(month);
  110. end;
  111. inc(year,month div 12);
  112. month := month mod 12;
  113. end;
  114. dec(year,70);
  115. leaps := (year + 2) shr 2;
  116. if (not(((year+70) and 3)<>0) and (month < 2)) then dec(leaps);
  117. days1 := year*365 + leaps + YDays[month] + day;
  118. secs := days1*86400 + hour*3600 + min*60 + sec + 14*60*60+17+12*60;
  119. if secs > 0 then totalsec:=secs else totalsec:=-1;
  120. end;
  121. Function mktime(t:tm) : time_t;
  122. var
  123. secs:time_t;
  124. begin
  125. secs := totalsec(t.tm_year, t.tm_mon, t.tm_mday-1, t.tm_hour, t.tm_min,
  126. t.tm_sec);
  127. mktime := (secs);
  128. end;
  129. Function Time:time_t;
  130. var
  131. year,month,day,hour,min,sec,sec100,wday : word;
  132. begin
  133. GetDate(Year, Month, Day, wday);
  134. GetTime(Hour, Min, Sec, Sec100);
  135. Time:=totalsec(year-1900, month-1, day-1, hour, min, sec);
  136. end;
  137. Procedure UnixToDos(time:time_t;var t:tm;TZ:shortInt);
  138. begin
  139. time := time + (60*60*TZ);
  140. time := time - (24 * 60 * 60 * 3652 + timezone);
  141. t.tm_sec := time mod 60;
  142. time := time div 60; {* Time in minutes *}
  143. t.tm_min := time mod 60;
  144. time := time div 60; {* Time in minutes *}
  145. t.tm_year := 1980 + round(time / (1461 * 24)) shl 2;
  146. time := time mod (1461 * 24);
  147. if (time >= 366 * 24) then
  148. begin
  149. time := time - (366 * 24);
  150. inc(t.tm_year);
  151. t.tm_year := t.tm_year + round(time / (365 * 24));
  152. time := time mod (365 * 24);
  153. end;
  154. inc(time);
  155. t.tm_hour := time mod 24;
  156. time := time div 24;
  157. inc(time);
  158. if ((t.tm_year and 3) = 0) then
  159. begin
  160. if (time > 60) then
  161. dec(time)
  162. else
  163. if (time = 60) then
  164. begin
  165. t.tm_mon := 2;
  166. t.tm_mday := 29;
  167. exit;
  168. end;
  169. end;
  170. t.tm_mon := 0;
  171. while (Days[t.tm_mon] < time) do
  172. begin
  173. time := time - Days[t.tm_mon];
  174. inc(t.tm_mon);
  175. end;
  176. inc(t.tm_mon);
  177. t.tm_mday := time;
  178. end;
  179. end.