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 =======================================================================================================================
13 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
14 ''' SF_Timer
15 ''' ========
16 ''' Class for management of scripts execution performance
17 ''' A Timer measures durations. It can be suspended, resumed, restarted
18 ''' Duration properties are expressed in seconds with a precision of
3 decimal digits
20 ''' Service invocation example:
21 ''' Dim myTimer As Variant
22 ''' myTimer = CreateScriptService(
"Timer
")
23 ''' myTimer = CreateScriptService(
"Timer
", True)
' =
> To start timer immediately
24 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
26 REM ================================================================== EXCEPTIONS
28 REM ============================================================= PRIVATE MEMBERS
30 Private [Me] As Object
31 Private [_Parent] As Object
32 Private ObjectType As String
' Must be
"TIMER
"
33 Private ServiceName As String
34 Private _TimerStatus As Integer
' inactive, started, suspended or stopped
35 Private _StartTime As Double
' Moment when timer started, restarted
36 Private _EndTime As Double
' Moment when timer stopped
37 Private _SuspendTime As Double
' Moment when timer suspended
38 Private _SuspendDuration As Double
' 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)
' Duration of
1 second as compared to
1.0 =
1 day
49 REM ===================================================== CONSTRUCTOR/DESTRUCTOR
51 REM -----------------------------------------------------------------------------
52 Private Sub Class_Initialize()
54 Set [_Parent] = Nothing
55 ObjectType =
"TIMER
"
56 ServiceName =
"ScriptForge.Timer
"
57 _TimerStatus = STATUSINACTIVE
62 End Sub
' ScriptForge.SF_Timer Constructor
64 REM -----------------------------------------------------------------------------
65 Private Sub Class_Terminate()
66 Call Class_Initialize()
67 End Sub
' ScriptForge.SF_Timer Destructor
69 REM -----------------------------------------------------------------------------
70 Public Function Dispose() As Variant
71 Call Class_Terminate()
73 End Function
' ScriptForge.SF_Timer Explicit destructor
75 REM ================================================================== PROPERTIES
77 REM -----------------------------------------------------------------------------
78 Public Function Duration() As Double
79 ''' Returns the actual (out of suspensions) time elapsed since start or between start and stop
80 ''' Args:
81 ''' Returns:
82 ''' A Double expressing the duration in seconds
83 ''' Example:
84 ''' myTimer.Duration returns
1.234 (
1 sec,
234 ms)
86 Duration = _PropertyGet(
"Duration
")
88 End Function
' ScriptForge.SF_Timer.Duration
90 REM -----------------------------------------------------------------------------
91 Property Get IsStarted() As Boolean
92 ''' Returns True if timer is started or suspended
93 ''' Example:
94 ''' myTimer.IsStarted
96 IsStarted = _PropertyGet(
"IsStarted
")
98 End Property
' ScriptForge.SF_Timer.IsStarted
100 REM -----------------------------------------------------------------------------
101 Property Get IsSuspended() As Boolean
102 ''' Returns True if timer is started and suspended
103 ''' Example:
104 ''' myTimer.IsSuspended
106 IsSuspended = _PropertyGet(
"IsSuspended
")
108 End Property
' ScriptForge.SF_Timer.IsSuspended
110 REM -----------------------------------------------------------------------------
111 Public Function SuspendDuration() As Double
112 ''' Returns the actual time elapsed while suspended since start or between start and stop
113 ''' Args:
114 ''' Returns:
115 ''' A Double expressing the duration in seconds
116 ''' Example:
117 ''' myTimer.SuspendDuration returns
1.234 (
1 sec,
234 ms)
119 SuspendDuration = _PropertyGet(
"SuspendDuration
")
121 End Function
' ScriptForge.SF_Timer.SuspendDuration
123 REM -----------------------------------------------------------------------------
124 Public Function TotalDuration() As Double
125 ''' Returns the actual time elapsed (including suspensions) since start or between start and stop
126 ''' Args:
127 ''' Returns:
128 ''' A Double expressing the duration in seconds
129 ''' Example:
130 ''' myTimer.TotalDuration returns
1.234 (
1 sec,
234 ms)
132 TotalDuration = _PropertyGet(
"TotalDuration
")
134 End Function
' ScriptForge.SF_Timer.TotalDuration
136 REM ===================================================================== METHODS
138 REM -----------------------------------------------------------------------------
139 Public Function Continue() As Boolean
140 ''' Halt suspension of a running timer
141 ''' Args:
142 ''' Returns:
143 ''' True if successful, False if the timer is not suspended
144 ''' Examples:
145 ''' myTimer.Continue()
147 Const cstThisSub =
"Timer.Continue
"
148 Const cstSubArgs =
""
152 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
155 If _TimerStatus = STATUSSUSPENDED Then
156 _TimerStatus = STATUSSTARTED
157 _SuspendDuration = _SuspendDuration + _Now() - _SuspendTime
163 SF_Utils._ExitFunction(cstThisSub)
165 End Function
' ScriptForge.SF_Timer.Continue
167 REM -----------------------------------------------------------------------------
168 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
169 ''' Return the actual value of the given property
170 ''' Args:
171 ''' PropertyName: the name of the property as a string
172 ''' Returns:
173 ''' The actual value of the property
174 ''' Exceptions
175 ''' ARGUMENTERROR The property does not exist
176 ''' Examples:
177 ''' myTimer.GetProperty(
"Duration
")
179 Const cstThisSub =
"Timer.GetProperty
"
180 Const cstSubArgs =
"PropertyName
"
182 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
186 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
187 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
191 GetProperty = _PropertyGet(PropertyName)
194 SF_Utils._ExitFunction(cstThisSub)
198 End Function
' ScriptForge.SF_Timer.Properties
200 REM -----------------------------------------------------------------------------
201 Public Function Methods() As Variant
202 ''' Return the list or methods of the Timer class as an array
205 "Continue
" _
206 ,
"Restart
" _
207 ,
"Start
" _
208 ,
"Suspend
" _
209 ,
"Terminate
" _
212 End Function
' ScriptForge.SF_Timer.Methods
214 REM -----------------------------------------------------------------------------
215 Public Function Properties() As Variant
216 ''' Return the list or properties of the Timer class as an array
218 Properties = Array( _
219 "Duration
" _
220 ,
"IsStarted
" _
221 ,
"IsSuspended
" _
222 ,
"SuspendDuration
" _
223 ,
"TotalDuration
" _
226 End Function
' ScriptForge.SF_Timer.Properties
228 REM -----------------------------------------------------------------------------
229 Public Function Restart() As Boolean
230 ''' Terminate the timer and restart a new clean timer
231 ''' Args:
232 ''' Returns:
233 ''' True if successful, False if the timer is inactive
234 ''' Examples:
235 ''' myTimer.Restart()
237 Const cstThisSub =
"Timer.Restart
"
238 Const cstSubArgs =
""
242 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
245 If _TimerStatus
<> STATUSINACTIVE Then
246 If _TimerStatus
<> STATUSSTOPPED Then Terminate()
252 SF_Utils._ExitFunction(cstThisSub)
254 End Function
' ScriptForge.SF_Timer.Restart
256 REM -----------------------------------------------------------------------------
257 Public Function SetProperty(Optional ByVal PropertyName As Variant _
258 , Optional ByRef Value As Variant _
260 ''' Set a new value to the given property
261 ''' Args:
262 ''' PropertyName: the name of the property as a string
263 ''' Value: its new value
264 ''' Exceptions
265 ''' ARGUMENTERROR The property does not exist
267 Const cstThisSub =
"Timer.SetProperty
"
268 Const cstSubArgs =
"PropertyName, Value
"
270 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
274 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
275 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
279 Select Case UCase(PropertyName)
284 SF_Utils._ExitFunction(cstThisSub)
288 End Function
' ScriptForge.SF_Timer.SetProperty
290 REM -----------------------------------------------------------------------------
291 Public Function Start() As Boolean
292 ''' Start a new clean timer
293 ''' Args:
294 ''' Returns:
295 ''' True if successful, False if the timer is already started
296 ''' Examples:
297 ''' myTimer.Start()
299 Const cstThisSub =
"Timer.Start
"
300 Const cstSubArgs =
""
304 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
307 If _TimerStatus = STATUSINACTIVE Or _TimerStatus = STATUSSTOPPED Then
308 _TimerStatus = STATUSSTARTED
317 SF_Utils._ExitFunction(cstThisSub)
319 End Function
' ScriptForge.SF_Timer.Start
321 REM -----------------------------------------------------------------------------
322 Public Function Suspend() As Boolean
323 ''' Suspend a running timer
324 ''' Args:
325 ''' Returns:
326 ''' True if successful, False if the timer is not started or already suspended
327 ''' Examples:
328 ''' myTimer.Suspend()
330 Const cstThisSub =
"Timer.Suspend
"
331 Const cstSubArgs =
""
335 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
338 If _TimerStatus = STATUSSTARTED Then
339 _TimerStatus = STATUSSUSPENDED
340 _SuspendTime = _Now()
345 SF_Utils._ExitFunction(cstThisSub)
347 End Function
' ScriptForge.SF_Timer.Suspend
349 REM -----------------------------------------------------------------------------
350 Public Function Terminate() As Boolean
351 ''' Terminate a running timer
352 ''' Args:
353 ''' Returns:
354 ''' True if successful, False if the timer is neither started nor suspended
355 ''' Examples:
356 ''' myTimer.Terminate()
358 Const cstThisSub =
"Timer.Terminate
"
359 Const cstSubArgs =
""
363 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
366 If _TimerStatus = STATUSSTARTED Or _TimerStatus = STATUSSUSPENDED Then
367 If _TimerSTatus = STATUSSUSPENDED Then Continue()
368 _TimerStatus = STATUSSTOPPED
374 SF_Utils._ExitFunction(cstThisSub)
376 End Function
' ScriptForge.SF_Timer.Terminate
378 REM =========================================================== PRIVATE FUNCTIONS
380 REM -----------------------------------------------------------------------------
381 Private Function _Now() As Double
382 ''' Returns the current date and time
383 ''' Uses the Calc NOW() function to get a higher precision than the usual Basic Now() function
384 ''' Args:
385 ''' Returns:
386 ''' The actual time as a number
387 ''' The integer part represents the date, the decimal part represents the time
389 _Now = SF_Session.ExecuteCalcFunction(
"NOW
")
391 End Function
' ScriptForge.SF_Timer._Now
393 REM -----------------------------------------------------------------------------
394 Private Function _PropertyGet(Optional ByVal psProperty As String)
395 ''' Return the named property
396 ''' Args:
397 ''' psProperty: the name of the property
399 Dim dDuration As Double
' Computed duration
400 Dim cstThisSub As String
401 Dim cstSubArgs As String
403 cstThisSub =
"Timer.get
" & psProperty
404 cstSubArgs =
""
405 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
407 Select Case UCase(psProperty)
408 Case UCase(
"Duration
")
409 Select Case _TimerStatus
410 Case STATUSINACTIVE : dDuration =
0.0
412 dDuration = _Now() - _StartTime - _SuspendDuration
414 dDuration = _SuspendTime - _StartTime - _SuspendDuration
416 dDuration = _EndTime - _StartTime - _SuspendDuration
418 _PropertyGet = Fix(dDuration *
1000 / DSECOND) /
1000
419 Case UCase(
"IsStarted
")
420 _PropertyGet = ( _TimerStatus = STATUSSTARTED Or _TimerStatus = STATUSSUSPENDED )
421 Case UCase(
"IsSuspended
")
422 _PropertyGet = ( _TimerStatus = STATUSSUSPENDED )
423 Case UCase(
"SuspendDuration
")
424 Select Case _TimerStatus
425 Case STATUSINACTIVE : dDuration =
0.0
426 Case STATUSSTARTED, STATUSSTOPPED
427 dDuration = _SuspendDuration
429 dDuration = _Now() - _SuspendTime + _SuspendDuration
431 _PropertyGet = Fix(dDuration *
1000 / DSECOND) /
1000
432 Case UCase(
"TotalDuration
")
433 Select Case _TimerStatus
434 Case STATUSINACTIVE : dDuration =
0.0
435 Case STATUSSTARTED, STATUSSUSPENDED
436 dDuration = _Now() - _StartTime
438 dDuration = _EndTime - _StartTime
440 _PropertyGet = Fix(dDuration *
1000 / DSECOND) /
1000
444 SF_Utils._ExitFunction(cstThisSub)
446 End Function
' ScriptForge.SF_Timer._PropertyGet
448 REM -----------------------------------------------------------------------------
449 Private Function _Repr() As String
450 ''' Convert the Timer instance to a readable string, typically for debugging purposes (DebugPrint ...)
451 ''' Args:
452 ''' Return:
453 ''' "[Timer] Duration:xxx.yyy
455 Const cstTimer =
"[Timer] Duration:
"
456 Const cstMaxLength =
50 ' Maximum length for items
458 _Repr = cstTimer
& Replace(SF_Utils._Repr(Duration),
".
",
"""")
460 End Function
' ScriptForge.SF_Timer._Repr
462 REM ============================================ END OF SCRIPTFORGE.SF_TIMER