/
time.simba
368 lines (284 loc) · 7.49 KB
/
time.simba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
(*
Time
=======
Time related methods
*)
(*
TTimeFormat
~~~~~~~~~~~~
.. pascal::
type TTimeFormat = (Time_Formal, Time_Short, Time_Abbrev, Time_Bare, Time_FStop, Time_Formal_Long);
Formats used when converting time to string.
*)
type
TTimeFormat = (Time_Formal, Time_Short, Time_Abbrev, Time_Bare, Time_FStop, Time_Formal_Long);
const
ONE_SECOND = 1000;
ONE_MINUTE = ONE_SECOND * 60;
FIVE_MINUTES = ONE_MINUTE * 5;
THIRTY_MINUTES = ONE_MINUTE * 30;
ONE_HOUR = ONE_MINUTE * 60;
ONE_DAY = ONE_HOUR * 24;
(*
srl.MsToTime
~~~~~~~~~~~~~
.. pascal::
function TSRL.MsToTime(const MS: UInt64; const Format: TTimeFormat): String; constref;
Converts milliseconds to a string following the given time-format.
*)
function TSRL.MsToTime(const MS: UInt64; const Format: TTimeFormat): String; constref;
var
STA: array [0..5] of TVariantArray;
Time: TIntegerArray;
i, t, tl: Integer;
begin
Result := '';
if (Format = TIME_FORMAL_LONG) then
begin
tl := 7;
t := 6;
SetLength(Time, tl);
ConvertTime64(MS, Time[0], Time[1], Time[2], Time[3], Time[4], Time[5], Time[6]);
end else begin
tl := 3;
t := 2;
SetLength(Time, tl);
ConvertTime(MS, Time[0], Time[1], Time[2]);
end;
STA[Ord(Time_Formal)] := [' Hours, ', ' Minutes and ', ' Seconds', False, 0];
STA[Ord(Time_Short)] := ['h ', 'm ', 's', False, 2];
STA[Ord(Time_Abbrev)] := [' hr ', ' min ', ' sec', False, 0];
STA[Ord(Time_Bare)] := [':', ':', '', True, 2];
STA[Ord(Time_FStop)] := ['.', '.', '', True, 2];
STA[Ord(Time_Formal_Long)] := [' Years, ', ' Months, ', ' Weeks, ', ' Days, ', ' Hours, ', ' Minutes and ', ' Seconds', False, 0];
for i := 0 to t do
if (Time[i] > 0) or (STA[Ord(Format)][tl]) or (i = t) then
Result += PadZ(IntToStr(Time[i]), STA[Ord(Format)][tl+1]) + STA[Ord(Format)][i];
end;
(*
srl.TimeRunning
~~~~~~~~~~~~~~~~
.. pascal::
function TSRL.TimeRunning(TheType: TTimeFormat = Time_Formal_Long): String; constref;
Returns time the script has been running as a string with the given time-format.
*)
function TSRL.TimeRunning(TheType: TTimeFormat = Time_Formal_Long): String; constref;
begin
Result := MsToTime(GetTimeRunning(), TheType);
end;
(*
srl.WaitFunc
~~~~~~~~~~~~~
.. pascal::
function TSRL.WaitFunc(Func: function: Boolean; WaitPerLoop, MaxTime: Integer): Boolean; constref;
Waits till the given function returns ``True``, or otherwise time out defineds by ``MaxTime``.
*)
function TSRL.WaitFunc(Func: function: Boolean; WaitPerLoop, MaxTime: Integer): Boolean; constref;
var
t: UInt64;
begin
Result := False;
t := GetTickCount() + MaxTime;
while (GetTickCount() < t) do
begin
if (Func()) then
Exit(True);
Wait(WaitPerLoop);
end;
end;
(*
srl.WaitTypeFunc
~~~~~~~~~~~~~~~~~
.. pascal::
function TSRL.WaitTypeFunc(Func: function: Boolean of object; WaitPerLoop, MaxTime: Integer): Boolean; constref;
Waits till the given function returns ``True``, or otherwise time out defineds by ``MaxTime``.
*)
function TSRL.WaitTypeFunc(Func: function: Boolean of object; WaitPerLoop, MaxTime: Integer): Boolean; constref;
var
t: UInt64;
begin
Result := False;
t := GetTickCount() + MaxTime;
while (GetTickCount() < t) do
begin
if (Func()) then
Exit(True);
Wait(WaitPerLoop);
end;
end;
(*
TTimeMarker
~~~~~~~~~~~
.. code-block:: pascal
type TTimeMarker = record
time, startTime: LongWord;
paused: Boolean;
end;
Timer type which is useful for loops, timing and writing progress reports.
.. note::
- by Bart de Boer
*)
type
TTimeMarker = record
FTime, FStartTime, FPrevMark: UInt64;
FPaused: Boolean;
end;
(*
TTimeMarker.Start
~~~~~~~~~~~~~~~~~
.. code-block pascal
procedure TTimeMarker.Start();
Starts the timer. Can also be used when paused to continue where it left.
.. note::
- by Bart de Boer
Example:
.. code-block:: pascal
myTimer.Start();
*)
procedure TTimeMarker.Start();
begin
if (Self.FStartTime = 0) then
Self.FPrevMark := GetTickCount(); // used for adding time after a pause
if (not Self.FPaused) and (Self.FStartTime = 0) then
begin
Self.FStartTime := GetTickCount();
Self.FTime := 0;
end;
Self.FPaused := False;
end;
(*
TTimeMarker.Reset
~~~~~~~~~~~~~~~~~
.. code-block pascal
procedure TTimeMarker.Reset();
Stops the timer and resets it to zero.
.. note::
- by Bart de Boer
Example:
.. code-block:: pascal
myTimer.Reset();
*)
procedure TTimeMarker.Reset();
begin
Self.FPaused := False;
Self.FTime := 0;
Self.FStartTime := 0;
Self.FPrevMark := 0;
end;
(*
TTimeMarker.Pause
~~~~~~~~~~~~~~~~~
.. code-block pascal
procedure TTimeMarker.Pause();
Pauses the timer. It can be continued with start().
.. note::
- by Bart de Boer
Example:
.. code-block:: pascal
myTimer.Pause();
TakeABreak(90000);
myTimer.Start();
*)
procedure TTimeMarker.Pause();
begin
Self.FTime := Self.FTime + (GetTickCount() - Self.FPrevMark);
Self.FPaused := True;
end;
(*
TTimeMarker.GetTime
~~~~~~~~~~~~~~~~~~~
.. code-block pascal
function TTimeMarker.GetTime(): UInt64;
Gets the time from the timer. Returns zero if the timer was not set.
.. note::
- by Bart de Boer
Example:
.. code-block:: pascal
myTimer.start();
repeat
DoStuff;
until(myTimer.GetTime() > 60000);
*)
function TTimeMarker.GetTime(): UInt64;
begin
if not Self.FPaused then
Result := Self.FTime + (GetTickCount() - Self.FPrevMark)
else
Result := Self.FTime;
end;
(*
TTimeMarker.GetTotalTime
~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block pascal
function TTimeMarker.GetTotalTime(): UInt64;
Gets the time from the timer including the time it was paused. Returns zero if
the timer was not set.
.. note::
- by Bart de Boer
Example:
.. code-block:: pascal
BreakTime := MyTimer.GetTotalTime() - MyTimer.GetTime();
*)
function TTimeMarker.GetTotalTime(): UInt64;
begin
if (Self.FStartTime > 0) then
Result := GetTickCount() - Self.FStartTime;
end;
(*
TCountDown
~~~~~~~~~~~~~~~~~~~
A neat and simple timer type.
Example:
.. code-block:: pascal
myTimer.Init(3000); //3000ms
while not myTimer.IsFinished() do
{do something};
myTimer.Restart(Random(-200,200)); //3000 +/- 200ms
while not myTimer.IsFinished() do
{do something};
*)
type
TCountDown = record
Length: UInt64;
Timeout: UInt64;
end;
procedure TCountDown.Init(Time: UInt32);
begin
self.Length := Time;
self.Timeout := GetTickCount() + Time;
end;
procedure TCountDown.Restart(Rand:Int32=0);
begin
self.Timeout := GetTickCount() + (Length + Rand);
end;
function TCountDown.TimeRemaining(): UInt32;
begin
if not self.IsFinished() then
Result := self.Timeout - GetTickCount();
end;
function TCountDown.IsFinished(): Boolean;
begin
Result := GetTickCount() >= self.Timeout;
end;
{$IFDEF WINDOWS}
function PerformanceTimer(): Double;
var
frequency,counter: ^UInt64;
function QPF(F: ^UInt64): LongBool; static; external 'QueryPerformanceFrequency@Kernel32.dll '+{$IFDEF CPU386}'stdcall'{$ELSE}'win64'{$ENDIF};
function QPC(C: ^UInt64): LongBool; static; external 'QueryPerformanceCounter@Kernel32.dll '+{$IFDEF CPU386}'stdcall'{$ELSE}'win64'{$ENDIF};
begin
frequency := GetMem(SizeOf(UInt64)); // at least qword aligned
counter := GetMem(SizeOf(UInt64)); // ...
if QPF(frequency) and QPC(counter) then
Result := counter^ / frequency^ * 1000
else
Result := GetTickCount();
FreeMem(frequency);
FreeMem(counter);
end;
{$ELSE}
function PerformanceTimer(): Double;
begin
Result := GetTickCount();
end;
{$ENDIF}