nss: upgrade to release 3.73
[LibreOffice.git] / wizards / source / scriptforge / SF_Timer.xba
blobf352e1135744b2a9b9641ace0f032a7f220d82cd
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_Timer" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === Full documentation is available on https://help.libreoffice.org/ ===
6 REM =======================================================================================================================
8 Option Compatible
9 Option ClassModule
11 Option Explicit
13 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
14 &apos;&apos;&apos; SF_Timer
15 &apos;&apos;&apos; ========
16 &apos;&apos;&apos; Class for management of scripts execution performance
17 &apos;&apos;&apos; A Timer measures durations. It can be suspended, resumed, restarted
18 &apos;&apos;&apos; Duration properties are expressed in seconds with a precision of 3 decimal digits
19 &apos;&apos;&apos;
20 &apos;&apos;&apos; Service invocation example:
21 &apos;&apos;&apos; Dim myTimer As Variant
22 &apos;&apos;&apos; myTimer = CreateScriptService(&quot;Timer&quot;)
23 &apos;&apos;&apos; myTimer = CreateScriptService(&quot;Timer&quot;, True) &apos; =&gt; To start timer immediately
24 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
26 REM ================================================================== EXCEPTIONS
28 REM ============================================================= PRIVATE MEMBERS
30 Private [Me] As Object
31 Private [_Parent] As Object
32 Private ObjectType As String &apos; Must be &quot;TIMER&quot;
33 Private ServiceName As String
34 Private _TimerStatus As Integer &apos; inactive, started, suspended or stopped
35 Private _StartTime As Double &apos; Moment when timer started, restarted
36 Private _EndTime As Double &apos; Moment when timer stopped
37 Private _SuspendTime As Double &apos; Moment when timer suspended
38 Private _SuspendDuration As Double &apos; Duration of suspended status as a difference of times
40 REM ============================================================ MODULE CONSTANTS
42 Private Const STATUSINACTIVE = 0
43 Private Const STATUSSTARTED = 1
44 Private Const STATUSSUSPENDED = 2
45 Private Const STATUSSTOPPED = 3
47 Private Const DSECOND As Double = 1 / (24 * 60 * 60) &apos; Duration of 1 second as compared to 1.0 = 1 day
49 REM ===================================================== CONSTRUCTOR/DESTRUCTOR
51 REM -----------------------------------------------------------------------------
52 Private Sub Class_Initialize()
53 Set [Me] = Nothing
54 Set [_Parent] = Nothing
55 ObjectType = &quot;TIMER&quot;
56 ServiceName = &quot;ScriptForge.Timer&quot;
57 _TimerStatus = STATUSINACTIVE
58 _StartTime = 0
59 _EndTime = 0
60 _SuspendTime = 0
61 _SuspendDuration = 0
62 End Sub &apos; ScriptForge.SF_Timer Constructor
64 REM -----------------------------------------------------------------------------
65 Private Sub Class_Terminate()
66 Call Class_Initialize()
67 End Sub &apos; ScriptForge.SF_Timer Destructor
69 REM -----------------------------------------------------------------------------
70 Public Function Dispose() As Variant
71 Call Class_Terminate()
72 Set Dispose = Nothing
73 End Function &apos; ScriptForge.SF_Timer Explicit destructor
75 REM ================================================================== PROPERTIES
77 REM -----------------------------------------------------------------------------
78 Public Function Duration() As Double
79 &apos;&apos;&apos; Returns the actual (out of suspensions) time elapsed since start or between start and stop
80 &apos;&apos;&apos; Args:
81 &apos;&apos;&apos; Returns:
82 &apos;&apos;&apos; A Double expressing the duration in seconds
83 &apos;&apos;&apos; Example:
84 &apos;&apos;&apos; myTimer.Duration returns 1.234 (1 sec, 234 ms)
86 Duration = _PropertyGet(&quot;Duration&quot;)
88 End Function &apos; ScriptForge.SF_Timer.Duration
90 REM -----------------------------------------------------------------------------
91 Property Get IsStarted() As Boolean
92 &apos;&apos;&apos; Returns True if timer is started or suspended
93 &apos;&apos;&apos; Example:
94 &apos;&apos;&apos; myTimer.IsStarted
96 IsStarted = _PropertyGet(&quot;IsStarted&quot;)
98 End Property &apos; ScriptForge.SF_Timer.IsStarted
100 REM -----------------------------------------------------------------------------
101 Property Get IsSuspended() As Boolean
102 &apos;&apos;&apos; Returns True if timer is started and suspended
103 &apos;&apos;&apos; Example:
104 &apos;&apos;&apos; myTimer.IsSuspended
106 IsSuspended = _PropertyGet(&quot;IsSuspended&quot;)
108 End Property &apos; ScriptForge.SF_Timer.IsSuspended
110 REM -----------------------------------------------------------------------------
111 Public Function SuspendDuration() As Double
112 &apos;&apos;&apos; Returns the actual time elapsed while suspended since start or between start and stop
113 &apos;&apos;&apos; Args:
114 &apos;&apos;&apos; Returns:
115 &apos;&apos;&apos; A Double expressing the duration in seconds
116 &apos;&apos;&apos; Example:
117 &apos;&apos;&apos; myTimer.SuspendDuration returns 1.234 (1 sec, 234 ms)
119 SuspendDuration = _PropertyGet(&quot;SuspendDuration&quot;)
121 End Function &apos; ScriptForge.SF_Timer.SuspendDuration
123 REM -----------------------------------------------------------------------------
124 Public Function TotalDuration() As Double
125 &apos;&apos;&apos; Returns the actual time elapsed (including suspensions) since start or between start and stop
126 &apos;&apos;&apos; Args:
127 &apos;&apos;&apos; Returns:
128 &apos;&apos;&apos; A Double expressing the duration in seconds
129 &apos;&apos;&apos; Example:
130 &apos;&apos;&apos; myTimer.TotalDuration returns 1.234 (1 sec, 234 ms)
132 TotalDuration = _PropertyGet(&quot;TotalDuration&quot;)
134 End Function &apos; ScriptForge.SF_Timer.TotalDuration
136 REM ===================================================================== METHODS
138 REM -----------------------------------------------------------------------------
139 Public Function Continue() As Boolean
140 &apos;&apos;&apos; Halt suspension of a running timer
141 &apos;&apos;&apos; Args:
142 &apos;&apos;&apos; Returns:
143 &apos;&apos;&apos; True if successful, False if the timer is not suspended
144 &apos;&apos;&apos; Examples:
145 &apos;&apos;&apos; myTimer.Continue()
147 Const cstThisSub = &quot;Timer.Continue&quot;
148 Const cstSubArgs = &quot;&quot;
150 Check:
151 Continue = False
152 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
154 Try:
155 If _TimerStatus = STATUSSUSPENDED Then
156 _TimerStatus = STATUSSTARTED
157 _SuspendDuration = _SuspendDuration + _Now() - _SuspendTime
158 _SuspendTime = 0
159 Continue = True
160 End If
162 Finally:
163 SF_Utils._ExitFunction(cstThisSub)
164 Exit Function
165 End Function &apos; ScriptForge.SF_Timer.Continue
167 REM -----------------------------------------------------------------------------
168 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
169 &apos;&apos;&apos; Return the actual value of the given property
170 &apos;&apos;&apos; Args:
171 &apos;&apos;&apos; PropertyName: the name of the property as a string
172 &apos;&apos;&apos; Returns:
173 &apos;&apos;&apos; The actual value of the property
174 &apos;&apos;&apos; Exceptions
175 &apos;&apos;&apos; ARGUMENTERROR The property does not exist
176 &apos;&apos;&apos; Examples:
177 &apos;&apos;&apos; myTimer.GetProperty(&quot;Duration&quot;)
179 Const cstThisSub = &quot;Timer.GetProperty&quot;
180 Const cstSubArgs = &quot;PropertyName&quot;
182 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
183 GetProperty = Null
185 Check:
186 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
187 If Not SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
188 End If
190 Try:
191 GetProperty = _PropertyGet(PropertyName)
193 Finally:
194 SF_Utils._ExitFunction(cstThisSub)
195 Exit Function
196 Catch:
197 GoTo Finally
198 End Function &apos; ScriptForge.SF_Timer.Properties
200 REM -----------------------------------------------------------------------------
201 Public Function Methods() As Variant
202 &apos;&apos;&apos; Return the list or methods of the Timer class as an array
204 Methods = Array( _
205 &quot;Continue&quot; _
206 , &quot;Restart&quot; _
207 , &quot;Start&quot; _
208 , &quot;Suspend&quot; _
209 , &quot;Terminate&quot; _
212 End Function &apos; ScriptForge.SF_Timer.Methods
214 REM -----------------------------------------------------------------------------
215 Public Function Properties() As Variant
216 &apos;&apos;&apos; Return the list or properties of the Timer class as an array
218 Properties = Array( _
219 &quot;Duration&quot; _
220 , &quot;IsStarted&quot; _
221 , &quot;IsSuspended&quot; _
222 , &quot;SuspendDuration&quot; _
223 , &quot;TotalDuration&quot; _
226 End Function &apos; ScriptForge.SF_Timer.Properties
228 REM -----------------------------------------------------------------------------
229 Public Function Restart() As Boolean
230 &apos;&apos;&apos; Terminate the timer and restart a new clean timer
231 &apos;&apos;&apos; Args:
232 &apos;&apos;&apos; Returns:
233 &apos;&apos;&apos; True if successful, False if the timer is inactive
234 &apos;&apos;&apos; Examples:
235 &apos;&apos;&apos; myTimer.Restart()
237 Const cstThisSub = &quot;Timer.Restart&quot;
238 Const cstSubArgs = &quot;&quot;
240 Check:
241 Restart = False
242 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
244 Try:
245 If _TimerStatus &lt;&gt; STATUSINACTIVE Then
246 If _TimerStatus &lt;&gt; STATUSSTOPPED Then Terminate()
247 Start()
248 Restart = True
249 End If
251 Finally:
252 SF_Utils._ExitFunction(cstThisSub)
253 Exit Function
254 End Function &apos; ScriptForge.SF_Timer.Restart
256 REM -----------------------------------------------------------------------------
257 Public Function SetProperty(Optional ByVal PropertyName As Variant _
258 , Optional ByRef Value As Variant _
259 ) As Boolean
260 &apos;&apos;&apos; Set a new value to the given property
261 &apos;&apos;&apos; Args:
262 &apos;&apos;&apos; PropertyName: the name of the property as a string
263 &apos;&apos;&apos; Value: its new value
264 &apos;&apos;&apos; Exceptions
265 &apos;&apos;&apos; ARGUMENTERROR The property does not exist
267 Const cstThisSub = &quot;Timer.SetProperty&quot;
268 Const cstSubArgs = &quot;PropertyName, Value&quot;
270 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
271 SetProperty = False
273 Check:
274 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
275 If Not SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
276 End If
278 Try:
279 Select Case UCase(PropertyName)
280 Case Else
281 End Select
283 Finally:
284 SF_Utils._ExitFunction(cstThisSub)
285 Exit Function
286 Catch:
287 GoTo Finally
288 End Function &apos; ScriptForge.SF_Timer.SetProperty
290 REM -----------------------------------------------------------------------------
291 Public Function Start() As Boolean
292 &apos;&apos;&apos; Start a new clean timer
293 &apos;&apos;&apos; Args:
294 &apos;&apos;&apos; Returns:
295 &apos;&apos;&apos; True if successful, False if the timer is already started
296 &apos;&apos;&apos; Examples:
297 &apos;&apos;&apos; myTimer.Start()
299 Const cstThisSub = &quot;Timer.Start&quot;
300 Const cstSubArgs = &quot;&quot;
302 Check:
303 Start = False
304 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
306 Try:
307 If _TimerStatus = STATUSINACTIVE Or _TimerStatus = STATUSSTOPPED Then
308 _TimerStatus = STATUSSTARTED
309 _StartTime = _Now()
310 _EndTime = 0
311 _SuspendTime = 0
312 _SuspendDuration = 0
313 Start = True
314 End If
316 Finally:
317 SF_Utils._ExitFunction(cstThisSub)
318 Exit Function
319 End Function &apos; ScriptForge.SF_Timer.Start
321 REM -----------------------------------------------------------------------------
322 Public Function Suspend() As Boolean
323 &apos;&apos;&apos; Suspend a running timer
324 &apos;&apos;&apos; Args:
325 &apos;&apos;&apos; Returns:
326 &apos;&apos;&apos; True if successful, False if the timer is not started or already suspended
327 &apos;&apos;&apos; Examples:
328 &apos;&apos;&apos; myTimer.Suspend()
330 Const cstThisSub = &quot;Timer.Suspend&quot;
331 Const cstSubArgs = &quot;&quot;
333 Check:
334 Suspend = False
335 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
337 Try:
338 If _TimerStatus = STATUSSTARTED Then
339 _TimerStatus = STATUSSUSPENDED
340 _SuspendTime = _Now()
341 Suspend = True
342 End If
344 Finally:
345 SF_Utils._ExitFunction(cstThisSub)
346 Exit Function
347 End Function &apos; ScriptForge.SF_Timer.Suspend
349 REM -----------------------------------------------------------------------------
350 Public Function Terminate() As Boolean
351 &apos;&apos;&apos; Terminate a running timer
352 &apos;&apos;&apos; Args:
353 &apos;&apos;&apos; Returns:
354 &apos;&apos;&apos; True if successful, False if the timer is neither started nor suspended
355 &apos;&apos;&apos; Examples:
356 &apos;&apos;&apos; myTimer.Terminate()
358 Const cstThisSub = &quot;Timer.Terminate&quot;
359 Const cstSubArgs = &quot;&quot;
361 Check:
362 Terminate = False
363 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
365 Try:
366 If _TimerStatus = STATUSSTARTED Or _TimerStatus = STATUSSUSPENDED Then
367 If _TimerSTatus = STATUSSUSPENDED Then Continue()
368 _TimerStatus = STATUSSTOPPED
369 _EndTime = _Now()
370 Terminate = True
371 End If
373 Finally:
374 SF_Utils._ExitFunction(cstThisSub)
375 Exit Function
376 End Function &apos; ScriptForge.SF_Timer.Terminate
378 REM =========================================================== PRIVATE FUNCTIONS
380 REM -----------------------------------------------------------------------------
381 Private Function _Now() As Double
382 &apos;&apos;&apos; Returns the current date and time
383 &apos;&apos;&apos; Uses the Calc NOW() function to get a higher precision than the usual Basic Now() function
384 &apos;&apos;&apos; Args:
385 &apos;&apos;&apos; Returns:
386 &apos;&apos;&apos; The actual time as a number
387 &apos;&apos;&apos; The integer part represents the date, the decimal part represents the time
389 _Now = SF_Session.ExecuteCalcFunction(&quot;NOW&quot;)
391 End Function &apos; ScriptForge.SF_Timer._Now
393 REM -----------------------------------------------------------------------------
394 Private Function _PropertyGet(Optional ByVal psProperty As String)
395 &apos;&apos;&apos; Return the named property
396 &apos;&apos;&apos; Args:
397 &apos;&apos;&apos; psProperty: the name of the property
399 Dim dDuration As Double &apos; Computed duration
400 Dim cstThisSub As String
401 Dim cstSubArgs As String
403 cstThisSub = &quot;Timer.get&quot; &amp; psProperty
404 cstSubArgs = &quot;&quot;
405 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
407 Select Case UCase(psProperty)
408 Case UCase(&quot;Duration&quot;)
409 Select Case _TimerStatus
410 Case STATUSINACTIVE : dDuration = 0.0
411 Case STATUSSTARTED
412 dDuration = _Now() - _StartTime - _SuspendDuration
413 Case STATUSSUSPENDED
414 dDuration = _SuspendTime - _StartTime - _SuspendDuration
415 Case STATUSSTOPPED
416 dDuration = _EndTime - _StartTime - _SuspendDuration
417 End Select
418 _PropertyGet = Fix(dDuration * 1000 / DSECOND) / 1000
419 Case UCase(&quot;IsStarted&quot;)
420 _PropertyGet = ( _TimerStatus = STATUSSTARTED Or _TimerStatus = STATUSSUSPENDED )
421 Case UCase(&quot;IsSuspended&quot;)
422 _PropertyGet = ( _TimerStatus = STATUSSUSPENDED )
423 Case UCase(&quot;SuspendDuration&quot;)
424 Select Case _TimerStatus
425 Case STATUSINACTIVE : dDuration = 0.0
426 Case STATUSSTARTED, STATUSSTOPPED
427 dDuration = _SuspendDuration
428 Case STATUSSUSPENDED
429 dDuration = _Now() - _SuspendTime + _SuspendDuration
430 End Select
431 _PropertyGet = Fix(dDuration * 1000 / DSECOND) / 1000
432 Case UCase(&quot;TotalDuration&quot;)
433 Select Case _TimerStatus
434 Case STATUSINACTIVE : dDuration = 0.0
435 Case STATUSSTARTED, STATUSSUSPENDED
436 dDuration = _Now() - _StartTime
437 Case STATUSSTOPPED
438 dDuration = _EndTime - _StartTime
439 End Select
440 _PropertyGet = Fix(dDuration * 1000 / DSECOND) / 1000
441 End Select
443 Finally:
444 SF_Utils._ExitFunction(cstThisSub)
445 Exit Function
446 End Function &apos; ScriptForge.SF_Timer._PropertyGet
448 REM -----------------------------------------------------------------------------
449 Private Function _Repr() As String
450 &apos;&apos;&apos; Convert the Timer instance to a readable string, typically for debugging purposes (DebugPrint ...)
451 &apos;&apos;&apos; Args:
452 &apos;&apos;&apos; Return:
453 &apos;&apos;&apos; &quot;[Timer] Duration:xxx.yyy
455 Const cstTimer = &quot;[Timer] Duration: &quot;
456 Const cstMaxLength = 50 &apos; Maximum length for items
458 _Repr = cstTimer &amp; Replace(SF_Utils._Repr(Duration), &quot;.&quot;, &quot;&quot;&quot;&quot;)
460 End Function &apos; ScriptForge.SF_Timer._Repr
462 REM ============================================ END OF SCRIPTFORGE.SF_TIMER
463 </script:module>