Checking call execution time in ADVPL

0

My VerifyOnServ(nTimeOut) , is not always obeying nTimeOut passed, either because of it or the server with which it communicates.

Then I needed an insistence on its execution obeying nTimeOut

Follow the code:

cTic := Time()
While .Not. lExpired

    cResponse := VerifyOnServ(nTimeOut)
    cTac := Time()

    If cResponse != nil
       exit
    EndIf

    cElapsed := ElapTime(cTic, cTac)
    nElapsed := VAL(SUBSTR(cElapsed, 1, 2))*(3600) + VAL(SUBSTR(cElapsed, 4, 2))*(60) + VAL(SUBSTR(cElapsed, 7, 2))

    lExpired := nElapsed > nTimeOut
EndDo

The code above has met my needs and guarantees that the execution attempts will be respecting the nTimeOut passed. OK.

I would like to know if there is a better smart or optimized mode to rewrite the line:

nElapsed := VAL(SUBSTR(cElapsed, 1, 2))*(3600) + VAL(SUBSTR(cElapsed, 4, 2))*(60) + VAL(SUBSTR(cElapsed, 7, 2))

Given the full cost of breaking down cElapsed in hour, minute, and seconds, turn them into seconds and add them to finally compare with nTimeOut .

    
asked by anonymous 19.12.2018 / 16:00

1 answer

0

In essence it does not. I do not like the solution as a whole, but I can not say that it is not the best option in this case because I do not know the problem itself and the consequences of doing what it is doing (I just find it odd to have to do this and it sounds like gambiarra, but just opinion without basis, does not take seriously). And you have unnecessary variable in the code.

When the line in question gives only to keep a pattern and eliminate what is redundant, nothing more than this:

nElapsed := Val(Substr(cElapsed, 1, 2)) * 3600 + Val(Substr(cElapsed, 4, 2)) * 60 + Val(Substr(cElapsed, 7, 2))

Outside this could create a function that does this and can call instead of writing all this other times, being an implementation details makes sense, even if it will not be used in other places (and I think it would be useful other times), something like this:

Function ElapTime2Sec(cElapsed)
Return Val(Substr(cElapsed, 1, 2)) * 3600 + Val(Substr(cElapsed, 4, 2)) * 60 + Val(Substr(cElapsed, 7, 2))

Here you could do:

cTic := Time()
While .T.
    If VerifyOnServ(nTimeOut) != nil .or. ElapTime2Sec(ElapTime(cTic, Time())) <= nTimeOut
       Exit
    EndIf
EndDo
    
19.12.2018 / 16:14