time.pas 23 KB

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