time.pas 22 KB

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