Verifying time of execution of a call in ADVPL

Asked

Viewed 60 times

0

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

So I needed an insistence on his execution nTimeOut

Follows 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 above code has met my needs and ensures the execution attempts respecting the nTimeOut past. OK.

I wonder if there’s a more elegant or optimized to rewrite the line:

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

Considering all the cost of dismembering the cElapsed in hour, minute and seconds, turn them into seconds and add them up to finally compare with the nTimeOut.

2 answers

1


In essence it does not. I don’t like the solution as a whole, but I can’t say it’s not the best option in this case because I don’t know the problem itself and the consequences of doing what you’re doing (I just think it’s weird to have to do this and it seems like it’s a scam, but it’s just a baseless opinion, it doesn’t take it seriously). And there’s an unnecessary variable in the code.

When the line in question can only maintain a pattern and eliminate what is redundant, nothing more:

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

Otherwise it could create a function that does this and can call instead of writing all this other times, for being an implementation details makes sense, even if it is not using 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))

Then I could do:

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

I put in the Github for future reference.

0

The more elegant alternative would be not to calculate the time difference using the functions time() and elaptime(), but only the function seconds(). This function returns the number of seconds (accurate to thousandths) elapsed from midnight of the current day to the present time. You totally eliminate any need for time conversion. Here’s what code would look like based on your example, assuming that nTimeOut is expressed in seconds:

nTimeSec := seconds()
While seconds() - nTimeSec < nTimeOut 
    cResponse := VerifyOnServ(nTimeOut)
    If cResponse != nil
       EXIT
    EndIf
EndDo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.