time.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. { $Id$ }
  2. {*********************[ TIME UNIT ]************************}
  3. { }
  4. { System independent TIME unit }
  5. { }
  6. { Copyright (c) 1996, 1997, 1998, 1999 by Leon de Boer }
  7. { [email protected] - primary e-mail address }
  8. { [email protected] - backup e-mail address }
  9. { }
  10. {****************[ THIS CODE IS FREEWARE ]*****************}
  11. { }
  12. { This sourcecode is released for the purpose to }
  13. { promote the pascal language on all platforms. You may }
  14. { redistribute it and/or modify with the following }
  15. { DISCLAIMER. }
  16. { }
  17. { This SOURCE CODE is distributed "AS IS" WITHOUT }
  18. { WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR }
  19. { ANY OTHER WARRANTIES WHETHER EXPRESSED OR IMPLIED. }
  20. { }
  21. {*****************[ SUPPORTED PLATFORMS ]******************}
  22. { 16 and 32 Bit compilers }
  23. { DOS - Turbo Pascal 7.0 + (16 Bit) }
  24. { DPMI - Turbo Pascal 7.0 + (16 Bit) }
  25. { - FPC 0.9912+ (GO32V2) (32 Bit) }
  26. { WINDOWS - Turbo Pascal 7.0 + (16 Bit) }
  27. { - Delphi 1.0+ (16 Bit) }
  28. { WIN95/NT - Delphi 2.0+ (32 Bit) }
  29. { - Virtual Pascal 2.0+ (32 Bit) }
  30. { - Speedsoft Sybil 2.0+ (32 Bit) }
  31. { - FPC 0.9912+ (32 Bit) }
  32. { OS2 - Virtual Pascal 1.0+ (32 Bit) }
  33. { - Speed Pascal 1.0+ (32 Bit) }
  34. { - C'T patch to BP (16 Bit) }
  35. { }
  36. {******************[ REVISION HISTORY ]********************}
  37. { Version Date Fix }
  38. { ------- --------- --------------------------------- }
  39. { 1.00 06 Dec 96 First multi platform release. }
  40. { 1.10 06 Jul 97 New functiions added. }
  41. { 1.20 22 Jul 97 FPC pascal compiler added. }
  42. { 1.30 29 Aug 97 Platform.inc sort added. }
  43. { 1.40 13 Oct 97 Delphi 2/3 32 bit code added. }
  44. { 1.50 06 Nov 97 Speed pascal code added. }
  45. { 1.60 05 May 98 Virtual pascal 2.0 compiler added. }
  46. { 1.61 07 Jul 99 Speedsoft SYBIL 2.0 code added. }
  47. {**********************************************************}
  48. UNIT Time;
  49. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  50. INTERFACE
  51. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  52. {====Include file to sort compiler platform out =====================}
  53. {$I Platform.inc}
  54. {====================================================================}
  55. {==== Compiler directives ===========================================}
  56. {$IFNDEF PPC_FPC} { FPC doesn't support these switches }
  57. {$F-} { Short calls are okay }
  58. {$A+} { Word Align Data }
  59. {$B-} { Allow short circuit boolean evaluations }
  60. {$O+} { This unit may be overlaid }
  61. {$G+} { 286 Code optimization - if you're on an 8088 get a real computer }
  62. {$E+} { Emulation is on }
  63. {$N-} { No 80x87 code generation }
  64. {$ENDIF}
  65. {$X+} { Extended syntax is ok }
  66. {$R-} { Disable range checking }
  67. {$S-} { Disable Stack Checking }
  68. {$I-} { Disable IO Checking }
  69. {$Q-} { Disable Overflow Checking }
  70. {$V-} { Turn off strict VAR strings }
  71. {====================================================================}
  72. {***************************************************************************}
  73. { INTERFACE ROUTINES }
  74. {***************************************************************************}
  75. {-CurrentMinuteOfDay-------------------------------------------------
  76. Returns the number of minutes since midnight of a current system time.
  77. 19Jun97 LdB (Range: 0 - 1439)
  78. ---------------------------------------------------------------------}
  79. FUNCTION CurrentMinuteOfDay: Word;
  80. {-CurrentSecondOfDay-------------------------------------------------
  81. Returns the number of seconds since midnight of current system time.
  82. 24Jun97 LdB (Range: 0 - 86399)
  83. ---------------------------------------------------------------------}
  84. FUNCTION CurrentSecondOfDay: LongInt;
  85. {-CurrentSec100OfDay-------------------------------------------------
  86. Returns the 1/100ths of a second since midnight of current system time.
  87. 24Jun97 LdB (Range: 0 - 8639999)
  88. ---------------------------------------------------------------------}
  89. FUNCTION CurrentSec100OfDay: LongInt;
  90. {-MinuteOfDay--------------------------------------------------------
  91. Returns the number of minutes since midnight of a valid given time.
  92. 19Jun97 LdB (Range: 0 - 1439)
  93. ---------------------------------------------------------------------}
  94. FUNCTION MinuteOfDay (Hour24, Minute: Word): Word;
  95. {-SecondOfDay--------------------------------------------------------
  96. Returns the number of seconds since midnight of a valid given time.
  97. 19Jun97 LdB (Range: 0 - 86399)
  98. ---------------------------------------------------------------------}
  99. FUNCTION SecondOfDay (Hour24, Minute, Second: Word): LongInt;
  100. {-SetTime------------------------------------------------------------
  101. Set the operating systems time clock to the given values. If values
  102. are invalid this function will fail without notification.
  103. 06Nov97 LdB
  104. ---------------------------------------------------------------------}
  105. PROCEDURE SetTime (Hour, Minute, Second, Sec100: Word);
  106. {-GetTime------------------------------------------------------------
  107. Returns the current time settings of the operating system.
  108. 06Nov97 LdB
  109. ---------------------------------------------------------------------}
  110. PROCEDURE GetTime (Var Hour, Minute, Second, Sec100: Word);
  111. {-MinutesToTime------------------------------------------------------
  112. Returns the time in hours and minutes of a given number of minutes.
  113. 19Jun97 LdB
  114. ---------------------------------------------------------------------}
  115. PROCEDURE MinutesToTime (Md: LongInt; Var Hour24, Minute: Word);
  116. {-SecondsToTime------------------------------------------------------
  117. Returns the time in hours, mins and secs of a given number of seconds.
  118. 19Jun97 LdB
  119. ---------------------------------------------------------------------}
  120. PROCEDURE SecondsToTime (Sd: LongInt; Var Hour24, Minute, Second: Word);
  121. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  122. IMPLEMENTATION
  123. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  124. {$IFDEF OS_WINDOWS} { WIN/NT CODE }
  125. {$IFNDEF PPC_SPEED} { NON SPEED COMPILER }
  126. {$IFDEF PPC_FPC} { FPC WINDOWS COMPILER }
  127. USEs Windows; { Standard unit }
  128. {$ELSE} { OTHER COMPILERS }
  129. USES WinTypes, WinProcs; { Standard units }
  130. {$ENDIF}
  131. {$ELSE} { SPEEDSOFT COMPILER }
  132. USES WinBase; { Standard unit }
  133. TYPE TSystemTime = SystemTime; { Type fix up }
  134. {$ENDIF}
  135. {$ENDIF}
  136. {$IFDEF OS_OS2} { OS2 COMPILERS }
  137. {$IFDEF PPC_VIRTUAL} { VIRTUAL PASCAL }
  138. USES OS2Base; { Standard unit }
  139. {$ENDIF}
  140. {$IFDEF PPC_SPEED} { SPEED PASCAL }
  141. USES BseDos, Os2Def; { Standard unit }
  142. {$ENDIF}
  143. {$IFDEF PPC_BPOS2} { C'T PATCH TO BP CODE }
  144. USES DosTypes, DosProcs; { Standard unit }
  145. TYPE DateTime = TDateTime; { Type correction }
  146. {$ENDIF}
  147. {$ENDIF}
  148. {$ifdef OS_UNIX}
  149. USES Dos;
  150. {$endif OS_UNIX}
  151. {$ifdef OS_GO32}
  152. USES Dos;
  153. {$endif OS_GO32}
  154. {***************************************************************************}
  155. { INTERFACE ROUTINES }
  156. {***************************************************************************}
  157. {---------------------------------------------------------------------------}
  158. { CurrentMinuteOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 24Jun97 LdB}
  159. {---------------------------------------------------------------------------}
  160. FUNCTION CurrentMinuteOfDay: Word;
  161. VAR Hour, Minute, Second, Sec100: Word;
  162. BEGIN
  163. GetTime(Hour, Minute, Second, Sec100); { Get current time }
  164. CurrentMinuteOfDay := (Hour * 60) + Minute; { Minute from midnight }
  165. END;
  166. {---------------------------------------------------------------------------}
  167. { CurrentSecondOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 24Jun97 LdB}
  168. {---------------------------------------------------------------------------}
  169. FUNCTION CurrentSecondOfDay: LongInt;
  170. VAR Hour, Minute, Second, Sec100: Word;
  171. BEGIN
  172. GetTime(Hour, Minute, Second, Sec100); { Get current time }
  173. CurrentSecondOfDay := (LongInt(Hour) * 3600) +
  174. (Minute * 60) + Second; { Second from midnight }
  175. END;
  176. {---------------------------------------------------------------------------}
  177. { CurrentSec100OfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 24Jun97 LdB}
  178. {---------------------------------------------------------------------------}
  179. FUNCTION CurrentSec100OfDay: LongInt;
  180. VAR Hour, Minute, Second, Sec100: Word;
  181. BEGIN
  182. GetTime(Hour, Minute, Second, Sec100); { Get current time }
  183. CurrentSec100OfDay := (LongInt(Hour) * 360000) +
  184. (LongInt(Minute) * 6000) + (Second*100)+ Sec100; { Sec100 from midnight }
  185. END;
  186. {---------------------------------------------------------------------------}
  187. { MinuteOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  188. {---------------------------------------------------------------------------}
  189. FUNCTION MinuteOfDay (Hour24, Minute: Word): Word;
  190. BEGIN
  191. MinuteOfDay := (Hour24 * 60) + Minute; { Minute from midnight }
  192. END;
  193. {---------------------------------------------------------------------------}
  194. { SecondOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  195. {---------------------------------------------------------------------------}
  196. FUNCTION SecondOfDay (Hour24, Minute, Second: Word): LongInt;
  197. BEGIN
  198. SecondOfDay := (LongInt(Hour24) * 3600) +
  199. (Minute * 60) + Second; { Second from midnight }
  200. END;
  201. {---------------------------------------------------------------------------}
  202. { SetTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 06Nov97 LdB }
  203. {---------------------------------------------------------------------------}
  204. PROCEDURE SetTime (Hour, Minute, Second, Sec100: Word);
  205. {$IFDEF OS_DOS} { DOS/DPMI CODE }
  206. {$IFDEF ASM_BP} { BP COMPATABLE ASM }
  207. ASSEMBLER;
  208. ASM
  209. MOV CH, BYTE PTR Hour; { Fetch hour }
  210. MOV CL, BYTE PTR Minute; { Fetch minute }
  211. MOV DH, BYTE PTR Second; { Fetch second }
  212. MOV DL, BYTE PTR Sec100; { Fetch hundredths }
  213. MOV AX, $2D00; { Set function id }
  214. PUSH BP; { Safety save register }
  215. INT $21; { Set the time }
  216. POP BP; { Restore register }
  217. END;
  218. {$ENDIF}
  219. {$IFDEF ASM_FPC} { FPC COMPATABLE ASM }
  220. BEGIN
  221. ASM
  222. MOVB Hour, %CH; { Fetch hour }
  223. MOVB Minute, %CL; { Fetch minute }
  224. MOVB Second, %DH; { Fetch second }
  225. MOVB Sec100, %DL; { Fetch hundredths }
  226. MOVW $0x2D00, %AX; { Set function id }
  227. PUSHL %EBP; { Save register }
  228. INT $0x21; { BIOS set time }
  229. POPL %EBP; { Restore register }
  230. END;
  231. END;
  232. {$ENDIF}
  233. {$ENDIF}
  234. {$IFDEF OS_WINDOWS} { WIN/NT CODE }
  235. {$IFDEF BIT_16} { 16 BIT WINDOWS CODE }
  236. ASSEMBLER;
  237. ASM
  238. MOV CH, BYTE PTR Hour; { Fetch hour }
  239. MOV CL, BYTE PTR Minute; { Fetch minute }
  240. MOV DH, BYTE PTR Second; { Fetch second }
  241. MOV DL, BYTE PTR Sec100; { Fetch hundredths }
  242. MOV AX, $2D00; { Set function id }
  243. PUSH BP; { Safety save register }
  244. INT $21; { Set the time }
  245. POP BP; { Restore register }
  246. END;
  247. {$ENDIF}
  248. {$IFDEF BIT_32} { 32 BIT WINDOWS CODE }
  249. VAR DT: TSystemTime;
  250. BEGIN
  251. {$IFDEF PPC_FPC} { FPC WINDOWS COMPILER }
  252. GetLocalTime(@DT); { Get the date/time }
  253. {$ELSE} { OTHER COMPILERS }
  254. GetLocalTime(DT); { Get the date/time }
  255. {$ENDIF}
  256. DT.wHour := Hour; { Transfer hour }
  257. DT.wMinute := Minute; { Transfer minute }
  258. DT.wSecond := Second; { Transfer seconds }
  259. DT.wMilliseconds := Sec100 * 10; { Transfer millisecs }
  260. SetLocalTime(DT); { Set the date/time }
  261. END;
  262. {$ENDIF}
  263. {$ENDIF}
  264. {$IFDEF OS_OS2} { OS2 CODE }
  265. VAR DT: DateTime;
  266. BEGIN
  267. DosGetDateTime(DT); { Get the date/time }
  268. DT.Hours := Hour; { Transfer hour }
  269. DT.Minutes := Minute; { Transfer minute }
  270. DT.Seconds := Second; { Transfer seconds }
  271. DT.Hundredths := Sec100; { Transfer hundredths }
  272. DosSetDateTime(DT); { Set the time }
  273. END;
  274. {$ENDIF}
  275. {$ifdef OS_UNIX}
  276. BEGIN
  277. {settime is dummy in Linux}
  278. END;
  279. {$endif OS_UNIX}
  280. {---------------------------------------------------------------------------}
  281. { GetTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 06Nov97 LdB }
  282. {---------------------------------------------------------------------------}
  283. PROCEDURE GetTime (Var Hour, Minute, Second, Sec100: Word);
  284. {$IFDEF OS_DOS} { DOS/DPMI CODE }
  285. {$IFDEF ASM_BP} { BP COMPATABLE ASM }
  286. ASSEMBLER;
  287. ASM
  288. MOV AX, $2C00; { Set function id }
  289. PUSH BP; { Safety save register }
  290. INT $21; { System get time }
  291. POP BP; { Restore register }
  292. XOR AH, AH; { Clear register }
  293. CLD; { Strings go forward }
  294. MOV AL, DL; { Transfer register }
  295. LES DI, Sec100; { ES:DI -> hundredths }
  296. STOSW; { Return hundredths }
  297. MOV AL, DH; { Transfer register }
  298. LES DI, Second; { ES:DI -> seconds }
  299. STOSW; { Return seconds }
  300. MOV AL, CL; { Transfer register }
  301. LES DI, Minute; { ES:DI -> minutes }
  302. STOSW; { Return minutes }
  303. MOV AL, CH; { Transfer register }
  304. LES DI, Hour; { ES:DI -> hours }
  305. STOSW; { Return hours }
  306. END;
  307. {$ENDIF}
  308. {$IFDEF OS_GO32} { FPC COMPATABLE ASM }
  309. BEGIN
  310. (* ASM
  311. MOVW $0x2C00, %AX; { Set function id }
  312. PUSHL %EBP; { Save register }
  313. INT $0x21; { System get time }
  314. POPL %EBP; { Restore register }
  315. XORB %AH, %AH; { Clear register }
  316. MOVB %DL, %AL; { Transfer register }
  317. MOVL Sec100, %EDI; { EDI -> Sec100 }
  318. MOVW %AX, (%EDI); { Return Sec100 }
  319. MOVB %DH, %AL; { Transfer register }
  320. MOVL Second, %EDI; { EDI -> Second }
  321. MOVW %AX, (%EDI); { Return Second }
  322. MOVB %CL, %AL; { Transfer register }
  323. MOVL Minute, %EDI; { EDI -> Minute }
  324. MOVW %AX, (%EDI); { Return minute }
  325. MOVB %CH, %AL; { Transfer register }
  326. MOVL Hour, %EDI; { EDI -> Hour }
  327. MOVW %AX, (%EDI); { Return hour }
  328. END; *)
  329. { direct call of real interrupt seems to render the system
  330. unstable on Win2000 because some registers are not properly
  331. restored if a mouse interrupt is generated while the Dos
  332. interrupt is called... PM }
  333. Dos.GetTime(Hour,Minute,Second,Sec100);
  334. END;
  335. {$ENDIF}
  336. {$ENDIF}
  337. {$IFDEF OS_WINDOWS} { WIN/NT CODE }
  338. {$IFDEF BIT_16} { 16 BIT WINDOWS CODE }
  339. ASSEMBLER;
  340. ASM
  341. MOV AX, $2C00; { Set function id }
  342. PUSH BP; { Safety save register }
  343. INT $21; { System get time }
  344. POP BP; { Restore register }
  345. XOR AH, AH; { Clear register }
  346. CLD; { Strings go forward }
  347. MOV AL, DL; { Transfer register }
  348. LES DI, Sec100; { ES:DI -> hundredths }
  349. STOSW; { Return hundredths }
  350. MOV AL, DH; { Transfer register }
  351. LES DI, Second; { ES:DI -> seconds }
  352. STOSW; { Return seconds }
  353. MOV AL, CL; { Transfer register }
  354. LES DI, Minute; { ES:DI -> minutes }
  355. STOSW; { Return minutes }
  356. MOV AL, CH; { Transfer register }
  357. LES DI, Hour; { ES:DI -> hours }
  358. STOSW; { Return hours }
  359. END;
  360. {$ENDIF}
  361. {$IFDEF BIT_32} { 32 BIT WINDOWS CODE }
  362. VAR DT: TSystemTime;
  363. BEGIN
  364. {$IFDEF PPC_FPC} { FPC WINDOWS COMPILER }
  365. GetLocalTime(@DT); { Get the date/time }
  366. {$ELSE} { OTHER COMPILERS }
  367. GetLocalTime(DT); { Get the date/time }
  368. {$ENDIF}
  369. Hour := DT.wHour; { Transfer hour }
  370. Minute := DT.wMinute; { Transfer minute }
  371. Second := DT.wSecond; { Transfer seconds }
  372. Sec100 := DT.wMilliseconds DIV 10; { Transfer hundredths }
  373. END;
  374. {$ENDIF}
  375. {$ENDIF}
  376. {$IFDEF OS_OS2} { OS2 CODE }
  377. VAR DT: DateTime;
  378. BEGIN
  379. DosGetDateTime(DT); { Get the date/time }
  380. Hour := DT.Hours; { Transfer hour }
  381. Minute := DT.Minutes; { Transfer minute }
  382. Second := DT.Seconds; { Transfer seconds }
  383. Sec100 := DT.Hundredths; { Transfer hundredths }
  384. END;
  385. {$ENDIF}
  386. {$ifdef OS_UNIX}
  387. BEGIN
  388. Dos.GetTime(Hour,Minute,Second,Sec100);
  389. END;
  390. {$endif OS_UNIX}
  391. {---------------------------------------------------------------------------}
  392. { MinutesToTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  393. {---------------------------------------------------------------------------}
  394. PROCEDURE MinutesToTime (Md: LongInt; Var Hour24, Minute: Word);
  395. BEGIN
  396. Hour24 := Md DIV 60; { Hours of time }
  397. Minute := Md MOD 60; { Minutes of time }
  398. END;
  399. {---------------------------------------------------------------------------}
  400. { SecondsToTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  401. {---------------------------------------------------------------------------}
  402. PROCEDURE SecondsToTime (Sd: LongInt; Var Hour24, Minute, Second: Word);
  403. BEGIN
  404. Hour24 := Sd DIV 3600; { Hours of time }
  405. Minute := Sd MOD 3600 DIV 60; { Minutes of time }
  406. Second := Sd MOD 60; { Seconds of time }
  407. END;
  408. END.
  409. {
  410. $Log$
  411. Revision 1.8 2002-09-09 08:02:49 pierre
  412. * avoid instabilities on win2000
  413. Revision 1.7 2002/09/07 15:06:38 peter
  414. * old logs removed and tabs fixed
  415. Revision 1.6 2002/06/04 11:12:41 marco
  416. * Renamefest
  417. Revision 1.5 2002/06/03 20:26:16 pierre
  418. * use local time for windows
  419. }