time.pas 22 KB

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