time.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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_FPC} { FPC }
  144. USES Dos, DosCalls; { Standard unit }
  145. TYPE DateTime = TDateTime; { Type correction }
  146. {$ENDIF}
  147. {$IFDEF PPC_BPOS2} { C'T PATCH TO BP CODE }
  148. USES DosTypes, DosProcs; { Standard unit }
  149. TYPE DateTime = TDateTime; { Type correction }
  150. {$ENDIF}
  151. {$ENDIF}
  152. {$ifdef OS_UNIX}
  153. USES Dos;
  154. {$endif OS_UNIX}
  155. {$ifdef OS_GO32}
  156. USES Dos;
  157. {$endif OS_GO32}
  158. {***************************************************************************}
  159. { INTERFACE ROUTINES }
  160. {***************************************************************************}
  161. {---------------------------------------------------------------------------}
  162. { CurrentMinuteOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 24Jun97 LdB}
  163. {---------------------------------------------------------------------------}
  164. FUNCTION CurrentMinuteOfDay: Word;
  165. VAR Hour, Minute, Second, Sec100: Word;
  166. BEGIN
  167. GetTime(Hour, Minute, Second, Sec100); { Get current time }
  168. CurrentMinuteOfDay := (Hour * 60) + Minute; { Minute from midnight }
  169. END;
  170. {---------------------------------------------------------------------------}
  171. { CurrentSecondOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 24Jun97 LdB}
  172. {---------------------------------------------------------------------------}
  173. FUNCTION CurrentSecondOfDay: LongInt;
  174. VAR Hour, Minute, Second, Sec100: Word;
  175. BEGIN
  176. GetTime(Hour, Minute, Second, Sec100); { Get current time }
  177. CurrentSecondOfDay := (LongInt(Hour) * 3600) +
  178. (Minute * 60) + Second; { Second from midnight }
  179. END;
  180. {---------------------------------------------------------------------------}
  181. { CurrentSec100OfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 24Jun97 LdB}
  182. {---------------------------------------------------------------------------}
  183. FUNCTION CurrentSec100OfDay: LongInt;
  184. VAR Hour, Minute, Second, Sec100: Word;
  185. BEGIN
  186. GetTime(Hour, Minute, Second, Sec100); { Get current time }
  187. CurrentSec100OfDay := (LongInt(Hour) * 360000) +
  188. (LongInt(Minute) * 6000) + (Second*100)+ Sec100; { Sec100 from midnight }
  189. END;
  190. {---------------------------------------------------------------------------}
  191. { MinuteOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  192. {---------------------------------------------------------------------------}
  193. FUNCTION MinuteOfDay (Hour24, Minute: Word): Word;
  194. BEGIN
  195. MinuteOfDay := (Hour24 * 60) + Minute; { Minute from midnight }
  196. END;
  197. {---------------------------------------------------------------------------}
  198. { SecondOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  199. {---------------------------------------------------------------------------}
  200. FUNCTION SecondOfDay (Hour24, Minute, Second: Word): LongInt;
  201. BEGIN
  202. SecondOfDay := (LongInt(Hour24) * 3600) +
  203. (Minute * 60) + Second; { Second from midnight }
  204. END;
  205. {---------------------------------------------------------------------------}
  206. { SetTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 06Nov97 LdB }
  207. {---------------------------------------------------------------------------}
  208. PROCEDURE SetTime (Hour, Minute, Second, Sec100: Word);
  209. {$IFDEF OS_DOS} { DOS/DPMI CODE }
  210. {$IFDEF ASM_BP} { BP COMPATABLE ASM }
  211. ASSEMBLER;
  212. ASM
  213. MOV CH, BYTE PTR Hour; { Fetch hour }
  214. MOV CL, BYTE PTR Minute; { Fetch minute }
  215. MOV DH, BYTE PTR Second; { Fetch second }
  216. MOV DL, BYTE PTR Sec100; { Fetch hundredths }
  217. MOV AX, $2D00; { Set function id }
  218. PUSH BP; { Safety save register }
  219. INT $21; { Set the time }
  220. POP BP; { Restore register }
  221. END;
  222. {$ENDIF}
  223. {$IFDEF ASM_FPC} { FPC COMPATABLE ASM }
  224. BEGIN
  225. ASM
  226. MOVB Hour, %CH; { Fetch hour }
  227. MOVB Minute, %CL; { Fetch minute }
  228. MOVB Second, %DH; { Fetch second }
  229. MOVB Sec100, %DL; { Fetch hundredths }
  230. MOVW $0x2D00, %AX; { Set function id }
  231. PUSHL %EBP; { Save register }
  232. INT $0x21; { BIOS set time }
  233. POPL %EBP; { Restore register }
  234. END;
  235. END;
  236. {$ENDIF}
  237. {$ENDIF}
  238. {$IFDEF OS_WINDOWS} { WIN/NT CODE }
  239. {$IFDEF BIT_16} { 16 BIT WINDOWS CODE }
  240. ASSEMBLER;
  241. ASM
  242. MOV CH, BYTE PTR Hour; { Fetch hour }
  243. MOV CL, BYTE PTR Minute; { Fetch minute }
  244. MOV DH, BYTE PTR Second; { Fetch second }
  245. MOV DL, BYTE PTR Sec100; { Fetch hundredths }
  246. MOV AX, $2D00; { Set function id }
  247. PUSH BP; { Safety save register }
  248. INT $21; { Set the time }
  249. POP BP; { Restore register }
  250. END;
  251. {$ENDIF}
  252. {$IFDEF BIT_32} { 32 BIT WINDOWS CODE }
  253. VAR DT: TSystemTime;
  254. BEGIN
  255. {$IFDEF PPC_FPC} { FPC WINDOWS COMPILER }
  256. GetLocalTime(@DT); { Get the date/time }
  257. {$ELSE} { OTHER COMPILERS }
  258. GetLocalTime(DT); { Get the date/time }
  259. {$ENDIF}
  260. DT.wHour := Hour; { Transfer hour }
  261. DT.wMinute := Minute; { Transfer minute }
  262. DT.wSecond := Second; { Transfer seconds }
  263. DT.wMilliseconds := Sec100 * 10; { Transfer millisecs }
  264. SetLocalTime(DT); { Set the date/time }
  265. END;
  266. {$ENDIF}
  267. {$ENDIF}
  268. {$IFDEF OS_OS2} { OS2 CODE }
  269. VAR DT: DateTime;
  270. BEGIN
  271. DosGetDateTime(DT); { Get the date/time }
  272. DT.Hours := Hour; { Transfer hour }
  273. DT.Minutes := Minute; { Transfer minute }
  274. DT.Seconds := Second; { Transfer seconds }
  275. DT.Hundredths := Sec100; { Transfer hundredths }
  276. DosSetDateTime(DT); { Set the time }
  277. END;
  278. {$ENDIF}
  279. {$ifdef OS_UNIX}
  280. BEGIN
  281. {settime is dummy in Linux}
  282. END;
  283. {$endif OS_UNIX}
  284. {---------------------------------------------------------------------------}
  285. { GetTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 06Nov97 LdB }
  286. {---------------------------------------------------------------------------}
  287. PROCEDURE GetTime (Var Hour, Minute, Second, Sec100: Word);
  288. {$IFDEF OS_DOS} { DOS/DPMI CODE }
  289. {$IFDEF ASM_BP} { BP COMPATABLE ASM }
  290. ASSEMBLER;
  291. ASM
  292. MOV AX, $2C00; { Set function id }
  293. PUSH BP; { Safety save register }
  294. INT $21; { System get time }
  295. POP BP; { Restore register }
  296. XOR AH, AH; { Clear register }
  297. CLD; { Strings go forward }
  298. MOV AL, DL; { Transfer register }
  299. LES DI, Sec100; { ES:DI -> hundredths }
  300. STOSW; { Return hundredths }
  301. MOV AL, DH; { Transfer register }
  302. LES DI, Second; { ES:DI -> seconds }
  303. STOSW; { Return seconds }
  304. MOV AL, CL; { Transfer register }
  305. LES DI, Minute; { ES:DI -> minutes }
  306. STOSW; { Return minutes }
  307. MOV AL, CH; { Transfer register }
  308. LES DI, Hour; { ES:DI -> hours }
  309. STOSW; { Return hours }
  310. END;
  311. {$ENDIF}
  312. {$IFDEF OS_GO32} { FPC COMPATABLE ASM }
  313. BEGIN
  314. (* ASM
  315. MOVW $0x2C00, %AX; { Set function id }
  316. PUSHL %EBP; { Save register }
  317. INT $0x21; { System get time }
  318. POPL %EBP; { Restore register }
  319. XORB %AH, %AH; { Clear register }
  320. MOVB %DL, %AL; { Transfer register }
  321. MOVL Sec100, %EDI; { EDI -> Sec100 }
  322. MOVW %AX, (%EDI); { Return Sec100 }
  323. MOVB %DH, %AL; { Transfer register }
  324. MOVL Second, %EDI; { EDI -> Second }
  325. MOVW %AX, (%EDI); { Return Second }
  326. MOVB %CL, %AL; { Transfer register }
  327. MOVL Minute, %EDI; { EDI -> Minute }
  328. MOVW %AX, (%EDI); { Return minute }
  329. MOVB %CH, %AL; { Transfer register }
  330. MOVL Hour, %EDI; { EDI -> Hour }
  331. MOVW %AX, (%EDI); { Return hour }
  332. END; *)
  333. { direct call of real interrupt seems to render the system
  334. unstable on Win2000 because some registers are not properly
  335. restored if a mouse interrupt is generated while the Dos
  336. interrupt is called... PM }
  337. Dos.GetTime(Hour,Minute,Second,Sec100);
  338. END;
  339. {$ENDIF}
  340. {$ENDIF}
  341. {$IFDEF OS_WINDOWS} { WIN/NT CODE }
  342. {$IFDEF BIT_16} { 16 BIT WINDOWS CODE }
  343. ASSEMBLER;
  344. ASM
  345. MOV AX, $2C00; { Set function id }
  346. PUSH BP; { Safety save register }
  347. INT $21; { System get time }
  348. POP BP; { Restore register }
  349. XOR AH, AH; { Clear register }
  350. CLD; { Strings go forward }
  351. MOV AL, DL; { Transfer register }
  352. LES DI, Sec100; { ES:DI -> hundredths }
  353. STOSW; { Return hundredths }
  354. MOV AL, DH; { Transfer register }
  355. LES DI, Second; { ES:DI -> seconds }
  356. STOSW; { Return seconds }
  357. MOV AL, CL; { Transfer register }
  358. LES DI, Minute; { ES:DI -> minutes }
  359. STOSW; { Return minutes }
  360. MOV AL, CH; { Transfer register }
  361. LES DI, Hour; { ES:DI -> hours }
  362. STOSW; { Return hours }
  363. END;
  364. {$ENDIF}
  365. {$IFDEF BIT_32} { 32 BIT WINDOWS CODE }
  366. VAR DT: TSystemTime;
  367. BEGIN
  368. {$IFDEF PPC_FPC} { FPC WINDOWS COMPILER }
  369. GetLocalTime(@DT); { Get the date/time }
  370. {$ELSE} { OTHER COMPILERS }
  371. GetLocalTime(DT); { Get the date/time }
  372. {$ENDIF}
  373. Hour := DT.wHour; { Transfer hour }
  374. Minute := DT.wMinute; { Transfer minute }
  375. Second := DT.wSecond; { Transfer seconds }
  376. Sec100 := DT.wMilliseconds DIV 10; { Transfer hundredths }
  377. END;
  378. {$ENDIF}
  379. {$ENDIF}
  380. {$IFDEF OS_OS2} { OS2 CODE }
  381. VAR DT: DateTime;
  382. BEGIN
  383. DosGetDateTime(DT); { Get the date/time }
  384. Hour := DT.Hours; { Transfer hour }
  385. Minute := DT.Minutes; { Transfer minute }
  386. Second := DT.Seconds; { Transfer seconds }
  387. Sec100 := DT.Hundredths; { Transfer hundredths }
  388. END;
  389. {$ENDIF}
  390. {$ifdef OS_UNIX}
  391. BEGIN
  392. Dos.GetTime(Hour,Minute,Second,Sec100);
  393. END;
  394. {$endif OS_UNIX}
  395. {---------------------------------------------------------------------------}
  396. { MinutesToTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  397. {---------------------------------------------------------------------------}
  398. PROCEDURE MinutesToTime (Md: LongInt; Var Hour24, Minute: Word);
  399. BEGIN
  400. Hour24 := Md DIV 60; { Hours of time }
  401. Minute := Md MOD 60; { Minutes of time }
  402. END;
  403. {---------------------------------------------------------------------------}
  404. { SecondsToTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  405. {---------------------------------------------------------------------------}
  406. PROCEDURE SecondsToTime (Sd: LongInt; Var Hour24, Minute, Second: Word);
  407. BEGIN
  408. Hour24 := Sd DIV 3600; { Hours of time }
  409. Minute := Sd MOD 3600 DIV 60; { Minutes of time }
  410. Second := Sd MOD 60; { Seconds of time }
  411. END;
  412. END.
  413. {
  414. $Log$
  415. Revision 1.11 2002-10-12 19:39:00 hajny
  416. * FPC/2 support
  417. Revision 1.10 2002/09/24 16:48:24 hajny
  418. * fix for TDateTime clash
  419. Revision 1.9 2002/09/22 19:42:22 hajny
  420. + FPC/2 support added
  421. Revision 1.8 2002/09/09 08:02:49 pierre
  422. * avoid instabilities on win2000
  423. Revision 1.7 2002/09/07 15:06:38 peter
  424. * old logs removed and tabs fixed
  425. Revision 1.6 2002/06/04 11:12:41 marco
  426. * Renamefest
  427. Revision 1.5 2002/06/03 20:26:16 pierre
  428. * use local time for windows
  429. }