time.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. {***************************************************************************}
  149. { INTERFACE ROUTINES }
  150. {***************************************************************************}
  151. {---------------------------------------------------------------------------}
  152. { CurrentMinuteOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 24Jun97 LdB}
  153. {---------------------------------------------------------------------------}
  154. FUNCTION CurrentMinuteOfDay: Word;
  155. VAR Hour, Minute, Second, Sec100: Word;
  156. BEGIN
  157. GetTime(Hour, Minute, Second, Sec100); { Get current time }
  158. CurrentMinuteOfDay := (Hour * 60) + Minute; { Minute from midnight }
  159. END;
  160. {---------------------------------------------------------------------------}
  161. { CurrentSecondOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 24Jun97 LdB}
  162. {---------------------------------------------------------------------------}
  163. FUNCTION CurrentSecondOfDay: LongInt;
  164. VAR Hour, Minute, Second, Sec100: Word;
  165. BEGIN
  166. GetTime(Hour, Minute, Second, Sec100); { Get current time }
  167. CurrentSecondOfDay := (LongInt(Hour) * 3600) +
  168. (Minute * 60) + Second; { Second from midnight }
  169. END;
  170. {---------------------------------------------------------------------------}
  171. { CurrentSec100OfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 24Jun97 LdB}
  172. {---------------------------------------------------------------------------}
  173. FUNCTION CurrentSec100OfDay: LongInt;
  174. VAR Hour, Minute, Second, Sec100: Word;
  175. BEGIN
  176. GetTime(Hour, Minute, Second, Sec100); { Get current time }
  177. CurrentSec100OfDay := (LongInt(Hour) * 360000) +
  178. (LongInt(Minute) * 6000) + (Second*100)+ Sec100; { Sec100 from midnight }
  179. END;
  180. {---------------------------------------------------------------------------}
  181. { MinuteOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  182. {---------------------------------------------------------------------------}
  183. FUNCTION MinuteOfDay (Hour24, Minute: Word): Word;
  184. BEGIN
  185. MinuteOfDay := (Hour24 * 60) + Minute; { Minute from midnight }
  186. END;
  187. {---------------------------------------------------------------------------}
  188. { SecondOfDay -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  189. {---------------------------------------------------------------------------}
  190. FUNCTION SecondOfDay (Hour24, Minute, Second: Word): LongInt;
  191. BEGIN
  192. SecondOfDay := (LongInt(Hour24) * 3600) +
  193. (Minute * 60) + Second; { Second from midnight }
  194. END;
  195. {---------------------------------------------------------------------------}
  196. { SetTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 06Nov97 LdB }
  197. {---------------------------------------------------------------------------}
  198. PROCEDURE SetTime (Hour, Minute, Second, Sec100: Word);
  199. {$IFDEF OS_DOS} { DOS/DPMI CODE }
  200. {$IFDEF ASM_BP} { BP COMPATABLE ASM }
  201. ASSEMBLER;
  202. ASM
  203. MOV CH, BYTE PTR Hour; { Fetch hour }
  204. MOV CL, BYTE PTR Minute; { Fetch minute }
  205. MOV DH, BYTE PTR Second; { Fetch second }
  206. MOV DL, BYTE PTR Sec100; { Fetch hundredths }
  207. MOV AX, $2D00; { Set function id }
  208. PUSH BP; { Safety save register }
  209. INT $21; { Set the time }
  210. POP BP; { Restore register }
  211. END;
  212. {$ENDIF}
  213. {$IFDEF ASM_FPC} { FPC COMPATABLE ASM }
  214. BEGIN
  215. ASM
  216. MOVB Hour, %CH; { Fetch hour }
  217. MOVB Minute, %CL; { Fetch minute }
  218. MOVB Second, %DH; { Fetch second }
  219. MOVB Sec100, %DL; { Fetch hundredths }
  220. MOVW $0x2D00H, %AX; { Set function id }
  221. PUSHL %EBP; { Save register }
  222. INT $0x21H; { BIOS set time }
  223. POPL %EBP; { Restore register }
  224. END;
  225. END;
  226. {$ENDIF}
  227. {$ENDIF}
  228. {$IFDEF OS_WINDOWS} { WIN/NT CODE }
  229. {$IFDEF BIT_16} { 16 BIT WINDOWS CODE }
  230. ASSEMBLER;
  231. ASM
  232. MOV CH, BYTE PTR Hour; { Fetch hour }
  233. MOV CL, BYTE PTR Minute; { Fetch minute }
  234. MOV DH, BYTE PTR Second; { Fetch second }
  235. MOV DL, BYTE PTR Sec100; { Fetch hundredths }
  236. MOV AX, $2D00; { Set function id }
  237. PUSH BP; { Safety save register }
  238. INT $21; { Set the time }
  239. POP BP; { Restore register }
  240. END;
  241. {$ENDIF}
  242. {$IFDEF BIT_32} { 32 BIT WINDOWS CODE }
  243. VAR DT: TSystemTime;
  244. BEGIN
  245. {$IFDEF PPC_FPC} { FPC WINDOWS COMPILER }
  246. GetSystemTime(@DT); { Get the date/time }
  247. {$ELSE} { OTHER COMPILERS }
  248. GetSystemTime(DT); { Get the date/time }
  249. {$ENDIF}
  250. DT.wHour := Hour; { Transfer hour }
  251. DT.wMinute := Minute; { Transfer minute }
  252. DT.wSecond := Second; { Transfer seconds }
  253. DT.wMilliseconds := Sec100 * 10; { Transfer millisecs }
  254. SetSystemTime(DT); { Set the date/time }
  255. END;
  256. {$ENDIF}
  257. {$ENDIF}
  258. {$IFDEF OS_OS2} { OS2 CODE }
  259. VAR DT: DateTime;
  260. BEGIN
  261. DosGetDateTime(DT); { Get the date/time }
  262. DT.Hours := Hour; { Transfer hour }
  263. DT.Minutes := Minute; { Transfer minute }
  264. DT.Seconds := Second; { Transfer seconds }
  265. DT.Hundredths := Sec100; { Transfer hundredths }
  266. DosSetDateTime(DT); { Set the time }
  267. END;
  268. {$ENDIF}
  269. {---------------------------------------------------------------------------}
  270. { GetTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 06Nov97 LdB }
  271. {---------------------------------------------------------------------------}
  272. PROCEDURE GetTime (Var Hour, Minute, Second, Sec100: Word);
  273. {$IFDEF OS_DOS} { DOS/DPMI CODE }
  274. {$IFDEF ASM_BP} { BP COMPATABLE ASM }
  275. ASSEMBLER;
  276. ASM
  277. MOV AX, $2C00; { Set function id }
  278. PUSH BP; { Safety save register }
  279. INT $21; { System get time }
  280. POP BP; { Restore register }
  281. XOR AH, AH; { Clear register }
  282. CLD; { Strings go forward }
  283. MOV AL, DL; { Transfer register }
  284. LES DI, Sec100; { ES:DI -> hundredths }
  285. STOSW; { Return hundredths }
  286. MOV AL, DH; { Transfer register }
  287. LES DI, Second; { ES:DI -> seconds }
  288. STOSW; { Return seconds }
  289. MOV AL, CL; { Transfer register }
  290. LES DI, Minute; { ES:DI -> minutes }
  291. STOSW; { Return minutes }
  292. MOV AL, CH; { Transfer register }
  293. LES DI, Hour; { ES:DI -> hours }
  294. STOSW; { Return hours }
  295. END;
  296. {$ENDIF}
  297. {$IFDEF ASM_FPC} { FPC COMPATABLE ASM }
  298. BEGIN
  299. ASM
  300. MOVW $0x2C00H, %AX; { Set function id }
  301. PUSHL %EBP; { Save register }
  302. INT $0x21H; { System get time }
  303. POPL %EBP; { Restore register }
  304. XORB %AH, %AH; { Clear register }
  305. MOVB %DL, %AL; { Transfer register }
  306. MOVL Sec100, %EDI; { EDI -> Sec100 }
  307. MOVW %AX, (%EDI); { Return Sec100 }
  308. MOVB %DH, %AL; { Transfer register }
  309. MOVL Second, %EDI; { EDI -> Second }
  310. MOVW %AX, (%EDI); { Return Second }
  311. MOVB %CL, %AL; { Transfer register }
  312. MOVL Minute, %EDI; { EDI -> Minute }
  313. MOVW %AX, (%EDI); { Return minute }
  314. MOVB %CH, %AL; { Transfer register }
  315. MOVL Hour, %EDI; { EDI -> Hour }
  316. MOVW %AX, (%EDI); { Return hour }
  317. END;
  318. END;
  319. {$ENDIF}
  320. {$ENDIF}
  321. {$IFDEF OS_WINDOWS} { WIN/NT CODE }
  322. {$IFDEF BIT_16} { 16 BIT WINDOWS CODE }
  323. ASSEMBLER;
  324. ASM
  325. MOV AX, $2C00; { Set function id }
  326. PUSH BP; { Safety save register }
  327. INT $21; { System get time }
  328. POP BP; { Restore register }
  329. XOR AH, AH; { Clear register }
  330. CLD; { Strings go forward }
  331. MOV AL, DL; { Transfer register }
  332. LES DI, Sec100; { ES:DI -> hundredths }
  333. STOSW; { Return hundredths }
  334. MOV AL, DH; { Transfer register }
  335. LES DI, Second; { ES:DI -> seconds }
  336. STOSW; { Return seconds }
  337. MOV AL, CL; { Transfer register }
  338. LES DI, Minute; { ES:DI -> minutes }
  339. STOSW; { Return minutes }
  340. MOV AL, CH; { Transfer register }
  341. LES DI, Hour; { ES:DI -> hours }
  342. STOSW; { Return hours }
  343. END;
  344. {$ENDIF}
  345. {$IFDEF BIT_32} { 32 BIT WINDOWS CODE }
  346. VAR DT: TSystemTime;
  347. BEGIN
  348. {$IFDEF PPC_FPC} { FPC WINDOWS COMPILER }
  349. GetSystemTime(@DT); { Get the date/time }
  350. {$ELSE} { OTHER COMPILERS }
  351. GetSystemTime(DT); { Get the date/time }
  352. {$ENDIF}
  353. Hour := DT.wHour; { Transfer hour }
  354. Minute := DT.wMinute; { Transfer minute }
  355. Second := DT.wSecond; { Transfer seconds }
  356. Sec100 := DT.wMilliseconds DIV 10; { Transfer hundredths }
  357. END;
  358. {$ENDIF}
  359. {$ENDIF}
  360. {$IFDEF OS_OS2} { OS2 CODE }
  361. VAR DT: DateTime;
  362. BEGIN
  363. DosGetDateTime(DT); { Get the date/time }
  364. Hour := DT.Hours; { Transfer hour }
  365. Minute := DT.Minutes; { Transfer minute }
  366. Second := DT.Seconds; { Transfer seconds }
  367. Sec100 := DT.Hundredths; { Transfer hundredths }
  368. END;
  369. {$ENDIF}
  370. {---------------------------------------------------------------------------}
  371. { MinutesToTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  372. {---------------------------------------------------------------------------}
  373. PROCEDURE MinutesToTime (Md: LongInt; Var Hour24, Minute: Word);
  374. BEGIN
  375. Hour24 := Md DIV 60; { Hours of time }
  376. Minute := Md MOD 60; { Minutes of time }
  377. END;
  378. {---------------------------------------------------------------------------}
  379. { SecondsToTime -> Platforms DOS/DPMI/WIN/NT/OS2 - Updated 19Jun97 LdB }
  380. {---------------------------------------------------------------------------}
  381. PROCEDURE SecondsToTime (Sd: LongInt; Var Hour24, Minute, Second: Word);
  382. BEGIN
  383. Hour24 := Sd DIV 3600; { Hours of time }
  384. Minute := Sd MOD 3600 DIV 60; { Minutes of time }
  385. Second := Sd MOD 60; { Seconds of time }
  386. END;
  387. END.
  388. {
  389. $Log$
  390. Revision 1.2 2000-08-24 12:00:22 marco
  391. * CVS log and ID tags
  392. }