time.pas 21 KB

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