test for elapsed time

This forum is for eXpress++ general support.
Post Reply
Message
Author
Zdeno Bielik
Posts: 147
Joined: Thu Jan 28, 2010 9:24 am
Location: Nitra, Slovakia
Contact:

test for elapsed time

#1 Post by Zdeno Bielik »

Hi Roger,

I want compare/test differencies of elapsed time.
But when I run next code, it returns wrong values.
Please, can you look, where is problem?

Many thanks.

Regards,
Zdeno

Code: Select all

***********************
Function MyTestElapTime()
***********************

Local cDate := Date()
Local cTime := Time()
Local aDT := { { CToD( '21.03.2016' ), '08:58:00' }, ;
               { CToD( '21.03.2016' ), '08:59:00' }, ;
               { CToD( '21.03.2016' ), '09:00:00' }, ;
               { CToD( '21.03.2016' ), '09:01:00' }, ;
               { CToD( '21.03.2016' ), '09:02:00' }, ;
               ;
               { CToD( '21.03.2016' ), '16:58:00' }, ;
               { CToD( '21.03.2016' ), '16:59:00' }, ;
               { CToD( '21.03.2016' ), '16:00:00' }, ;
               { CToD( '21.03.2016' ), '16:01:00' }, ;
               { CToD( '21.03.2016' ), '16:02:00' }, ;
               ;
               { CToD( '21.03.2016' ), '23:58:00' }, ;
               { CToD( '21.03.2016' ), '23:59:00' }, ;
               ;
               { CToD( '22.03.2016' ), '00:00:00' }, ;
               { CToD( '22.03.2016' ), '00:01:00' }, ;
               { CToD( '22.03.2016' ), '00:02:00' }  ;
             }
Local i, naDT := Len( aDT )
Local nDiff
Local cText := ''

cText += ( DToC( cDate ) + ' ' + cTime + CRLF )
cText += CRLF

For i := 1 To naDT
    nDiff := ElapTime( cDate, cTime, aDT[i,1], aDT[i,2] )
    cText += ( DToC( aDT[i,1] ) + ' ' + aDT[i,2] + ' - ' + Str( nDiff, 12, 2 ) + CRLF )
*     wtf aDT[i], nDiff, '*'
Next
wtf cText

* wtf '***'

Return ( NIL )

*

*****************
FUNCTION ElapTime( dDate, cTime, dDateStart, cTimeStart )
*****************

LOCAL nSeconds, nSecondsStart, nDiff

nSeconds := Val(Dtos(dDate)) * 60 * 60 * 24 + TimeToSec(cTime)
nSecondsStart := Val(Dtos(dDateStart)) * 60 * 60 * 24 + TimeToSec(cTimeStart)
nDiff := nSeconds - nSecondsStart

* wtf nSeconds, nSecondsStart, nDiff

RETURN ( nSeconds - nSecondsStart )

* ----------

******************
FUNCTION TimeToSec( cTime )
******************

LOCAL nHours, nMinutes, nSeconds

If Empty( cTime )
   cTime := Time()
EndIf

nHours := Val(Substr(cTime,1,2))
nMinutes := Val(Substr(cTime,4,2))
nSeconds := Val(Substr(cTime,7,2))

RETURN ( (nHours * 60 * 24) + (nMinutes * 60) + nSeconds )

*

skiman
Posts: 1185
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: test for elapsed time

#2 Post by skiman »

Hi,

I would change this. I dont know if it will be a solution, but you won't get numbers of 13 digits long.

Code: Select all

*****************
FUNCTION ElapTime( dDate, cTime, dDateStart, cTimeStart )
*****************

LOCAL nSeconds, nSecondsStart, nDiff

nSeconds := TimeToSec(cTime)
nSecondsStart := (dDate-dDatestart) * 60 * 60 * 24 + TimeToSec(cTimeStart)
nDiff := nSeconds - nSecondsStart

* wtf nSeconds, nSecondsStart, nDiff

RETURN ( nSeconds - nSecondsStart )

* ----------
Best regards,

Chris.
www.aboservice.be

Zdeno Bielik
Posts: 147
Joined: Thu Jan 28, 2010 9:24 am
Location: Nitra, Slovakia
Contact:

Re: test for elapsed time

#3 Post by Zdeno Bielik »

Hi Roger, Chris,

ok, I figured it.
There was little mistake in Roger's function TimeToSec().
And I also added some code to function ElapTime() for „optional correcting“ returned values.
Just try run new code and all results are ok now.

Regards,
Zdeno

Code: Select all

***********************
Function MyTestElapTime()
***********************

Local cDate := Date()
Local cTime := Time()
Local aDT := { { CToD( '23.03.2016' ), '08:58:00' }, ;
               { CToD( '23.03.2016' ), '08:59:00' }, ;
               { CToD( '23.03.2016' ), '09:00:00' }, ;
               { CToD( '23.03.2016' ), '09:01:00' }, ;
               { CToD( '23.03.2016' ), '09:02:00' }, ;
               ;
               ;
               { CToD( '24.03.2016' ), '08:58:00' }, ;
               { CToD( '24.03.2016' ), '08:59:00' }, ;
               { CToD( '24.03.2016' ), '09:00:00' }, ;
               { CToD( '24.03.2016' ), '09:01:00' }, ;
               { CToD( '24.03.2016' ), '09:02:00' }, ;
               ;
               ;
               { CToD( '25.03.2016' ), '08:58:00' }, ;
               { CToD( '25.03.2016' ), '08:59:00' }, ;
               { CToD( '25.03.2016' ), '09:00:00' }, ;
               { CToD( '25.03.2016' ), '09:01:00' }, ;
               { CToD( '25.03.2016' ), '09:02:00' }, ;
               ;
               { CToD( '25.03.2016' ), '16:58:00' }, ;
               { CToD( '25.03.2016' ), '16:59:00' }, ;
               { CToD( '25.03.2016' ), '17:00:00' }, ;
               { CToD( '25.03.2016' ), '17:01:00' }, ;
               { CToD( '25.03.2016' ), '17:02:00' }, ;
               ;
               { CToD( '25.03.2016' ), '23:58:00' }, ;
               { CToD( '25.03.2016' ), '23:59:00' }, ;
               ;
               ;
               { CToD( '26.03.2016' ), '00:00:00' }, ;
               { CToD( '26.03.2016' ), '00:01:00' }, ;
               { CToD( '26.03.2016' ), '00:02:00' }, ;
               ;
               { CToD( '26.03.2016' ), '08:58:00' }, ;
               { CToD( '26.03.2016' ), '08:59:00' }, ;
               { CToD( '26.03.2016' ), '09:00:00' }, ;
               { CToD( '26.03.2016' ), '09:01:00' }, ;
               { CToD( '26.03.2016' ), '09:02:00' }, ;
               ;
               { CToD( '26.03.2016' ), '16:58:00' }, ;
               { CToD( '26.03.2016' ), '16:59:00' }, ;
               { CToD( '26.03.2016' ), '17:00:00' }, ;
               { CToD( '26.03.2016' ), '17:01:00' }, ;
               { CToD( '26.03.2016' ), '17:02:00' }  ;
               ;
             }
Local i, naDT := Len( aDT )
Local nDiff
Local cText := ''
Local lCorrect := .F.

cText += ( DToC( cDate ) + ' ' + cTime + CRLF )
cText += CRLF

cText += ( 'lCorrect == .F.' + CRLF + CRLF )

lCorrect := .F.

For i := 1 To naDT
    wtf aDT[i,1], DToS( aDT[i,1] ), Val( DToS( aDT[i,1] ) )
    nDiff := ElapTime( cDate, cTime, aDT[i,1], aDT[i,2], lCorrect )
    cText += ( DToC( aDT[i,1] ) + ' ' + aDT[i,2] + ' - ' + Str( nDiff, 12, 2 ) + CRLF )
*     wtf aDT[i], nDiff, '*'
Next
wtf cText

cText += CRLF
cText += CRLF

*

lCorrect := .T.

cText += ( 'lCorrect == .T.' + CRLF + CRLF )

For i := 1 To naDT
    wtf aDT[i,1], DToS( aDT[i,1] ), Val( DToS( aDT[i,1] ) )
    nDiff := ElapTime( cDate, cTime, aDT[i,1], aDT[i,2], lCorrect )
    cText += ( DToC( aDT[i,1] ) + ' ' + aDT[i,2] + ' - ' + Str( nDiff, 12, 2 ) + CRLF )
*     wtf aDT[i], nDiff, '*'
Next
wtf cText

* wtf '***'

Return ( NIL )

*

******************
FUNCTION ElapTime( dDate, cTime, dDateStart, cTimeStart, lCorrect )
******************

LOCAL nSeconds:=0, nSecondsStart, nDiff

nSeconds := Val(Dtos(dDate)) * 60 * 60 * 24 + TimeToSec(cTime)
nSecondsStart := Val(Dtos(dDateStart)) * 60 * 60 * 24 + TimeToSec(cTimeStart)
nDiff := nSeconds - nSecondsStart

* correct returned calculated negative values from "future" dates...
If lCorrect
   If ( nDiff < 0 )
      nDiff *= -1
*       wtf nDiff pause
   EndIf
EndIf

* wtf nSeconds, nSecondsStart, nDiff, '***'

* RETURN ( nSeconds - nSecondsStart )
RETURN ( nDiff )

* ----------

******************
FUNCTION TimeToSec( cTime )
******************

LOCAL nHours, nMinutes, nSeconds
Local nSecs

If Empty( cTime )
   cTime := Time()
EndIf

nHours := Val(Substr(cTime,1,2))       
nMinutes := Val(Substr(cTime,4,2))  
nSeconds := Val(Substr(cTime,7,2))  

* wtf cTime, nHours, nMinutes, nSeconds
* wtf nHours * 60 * 60
* wtf nMinutes * 60
* wtf nSeconds

* nSecs := ( (nHours * 60 * 24) + (nMinutes * 60) + nSeconds )     && <<< was wrong value 24
nSecs := ( (nHours * 60 * 60 ) + (nMinutes * 60) + nSeconds )       && <<< is now right value 60
* wtf nSecs, '*'

* RETURN ( (nHours * 60 * 24) + (nMinutes * 60) + nSeconds )
RETURN ( nSecs )

*

skiman
Posts: 1185
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: test for elapsed time

#4 Post by skiman »

Yes, of course an hour is 3600 seconds.
Best regards,

Chris.
www.aboservice.be

Zdeno Bielik
Posts: 147
Joined: Thu Jan 28, 2010 9:24 am
Location: Nitra, Slovakia
Contact:

Re: test for elapsed time

#5 Post by Zdeno Bielik »

Hi,

I finally found why sometimes function ElapTime returned terrible values for some dates/times.
With next fix and one new function it works OK in every situations.
Instead of calling Val(dDate) I call new fnc. DateAsSecs( dDate ).

Regards
Zdeno

Code: Select all

If ( ElapTime3( Date(),Time(),dDate,cTime) > ( 60 * 60 * ( /*2*/ 1 * 24 ) ) )
...
EndIf


******************
FUNCTION ElapTime3( dDate, cTime, dDateStart, cTimeStart, lCorrect )
******************

LOCAL nSeconds, nSecondsStart, nDiff

If ( Len( cTime ) == 5 )        && 18:30
   cTime += ':00'
EndIf
If ( Len( cTimeStart ) == 5 )   && 18:30
   cTimeStart += ':00'
EndIf

nSeconds := ( DateAsSecs( dDate ) + TimeToSec(cTime) )

nSecondsStart := ( DateAsSecs( dDateStart ) + TimeToSec(cTimeStart) )

nDiff := nSeconds - nSecondsStart


If ( lCorrect == NIL )
   lCorrect := .F.
EndIf

* correct returned calculated negative values from "future" dates...
If lCorrect
   If ( nDiff < 0 )
      nDiff *= -1
   EndIf
EndIf

RETURN ( nDiff )

* ----------

*******************
Function DateAsSecs( _dDate )
*******************

Local nDays  := 0
Local nSecs  := 0
*
* Local dStartDate := CToD( '01.01.1970' )
* Local nStartDate := 1970
Local dStartDate := CToD( '01.01.2016' )        && in this app I will don't compare older values/dates than 01.01.2016
Local nStartDate := 2016
*
Local nYear     := Year( _dDate )
Local nDoY      := DoY( _dDate )
Local nDaysSecs := ( nDoY * 86400 /*60 * 60 * 24*/ )
*
Local i

For i := nStartDate To nYear
    If IsLeap( CToD( '01.01.' + Str( i, 4 ) ) )
       nDays += 366
    Else
       nDays += 365
    EndIf
Next

nSecs := ( nDays * 60 * 60 * 24 )

nSecs += nDaysSecs

Return ( nSecs )

*

User avatar
Auge_Ohr
Posts: 1407
Joined: Wed Feb 24, 2010 3:44 pm

Re: test for elapsed time

#6 Post by Auge_Ohr »

have a look at X:\ALASKA\XPPW32\SOURCE\COMPAT\compat87.prg ( v1.9x )
greetings by OHR
Jimmy

Zdeno Bielik
Posts: 147
Joined: Thu Jan 28, 2010 9:24 am
Location: Nitra, Slovakia
Contact:

Re: test for elapsed time

#7 Post by Zdeno Bielik »

I also watched that included functions before programming the functions of the DateAsSecs, but that functions compares only times in one day, but I need to compare the difference in time of several days and then write it in the form of days / hours / minutes / seconds

and depending on how much time is left before the beginning of the ordered actions / events, then it offered the option of the bank transfer (over three days) too, if less, so only the possibility of paying by credit card or in cash


something line this:
cPaymentTo := Secs2HrMn( nElapTime, .T. )

* do 2 dní 05 hod. 07 min. 54 sek.
* do 0 dní 21 hod. 37 min. 04 sek.
* do 1 deň 08 hod. 44 min. 39 sek.
* do 22 dní 15 hod. 01 min. 17 sek.
* do 257 dní 09 hod. 41 min. 28 sek.


If ( nElapTime > M->pnBankTransferLimit /*3 days*/ )
aPaymentsType := { "cash", "credit-card" }
wtf "you must pay to 3 calendar days! otherwise reservation will be canceled..."
Else
aPaymentsType := { "bank-transfer", "cash", "credit-card" }
wtf "you must pay to " + cPaymentTo + "! otherwise reservation will be canceled..."
EndIf



**************************************************************************
* This Function calculates and displays the time left in ( nSeconds )
* [ ddd:hh:mm:ss ] format.
**************************************************************************
FUNCTION Secs2HrMn( nSeconds, lAddInfo )

local nDays := 0
local nHour := 0
local nMin := 0
local nSec := 0

local cDays := ""
local cHour := ""
local cMin := ""
local cSec := ""

local cTimeLeft := ""

If ( lAddInfo == NIL )
lAddInfo := .F.
EndIf

nDays := int( nSeconds / 86400 )

nSeconds -= ( nDays * 86400 )
* cTimeLeft += PADL( alltrim( str( nDays ) ), 3, "0" ) + IIf( lAddInfo, ' dní ', ':' )
cTimeLeft += alltrim( str( nDays ) ) + IIf( lAddInfo, VratPDT( nDays ) /*' dní '*/, ':' )

nHour := int( nSeconds / 3600 )

nSeconds -= ( nHour * 3600 )
cHour := PADL( alltrim( str( nHour ) ), 2, "0" )
cTimeLeft += cHour + IIf( lAddInfo, ' hod. ', ':' )

nMin := int( nSeconds / 60 )

nSeconds -= ( nMin * 60 )
cMin := PADL( alltrim( str( nMin ) ), 2, "0" )
cTimeLeft += cMin + IIf( lAddInfo, ' min. ', ':' )

nSec := int( nSeconds )
cSec := PADL( alltrim( str( nSec ) ), 2, "0" )

cTimeLeft += cSec + IIf( lAddInfo, ' sek.', '' )

RETURN ( cTimeLeft )

*

****************
Function VratPDT( _nDays )
****************

Local cRet := ''

Do Case
Case ( _nDays == 0 )
cRet := ' dní '
Case ( _nDays == 1 )
cRet := ' deň '
* Case ( _nDays >= 2 ) .and. ( _nDays <= 4 )
* cRet := ' dní '
Otherwise
cRet := ' dní '
EndCase

Return ( cRet )

*

User avatar
Auge_Ohr
Posts: 1407
Joined: Wed Feb 24, 2010 3:44 pm

Re: test for elapsed time

#8 Post by Auge_Ohr »

Zdeno Bielik wrote:I finally found why sometimes function ElapTime returned terrible values for some dates/times.
With next fix and one new function it works OK in every situations.
Instead of calling Val(dDate) I call new fnc. DateAsSecs( dDate ).

Code: Select all

If ( ElapTime3( Date(),Time(),dDate,cTime) > ( 60 * 60 * ( /*2*/ 1 * 24 ) ) )
...
EndIf
******************
FUNCTION ElapTime3( dDate, cTime, dDateStart, cTimeStart, lCorrect )
******************
nSeconds := ( DateAsSecs( dDate ) + TimeToSec(cTime) )
nSecondsStart := ( DateAsSecs( dDateStart ) + TimeToSec(cTimeStart) )
nDiff := nSeconds - nSecondsStart
if you want to Difference in Seconds ( or MilliSec. ) use Windows API with ot4xb

Code: Select all

#include "OT4XB.CH"

PROCEDURE MAIN()
LOCAL st1  := FileTime64():NEW()
LOCAL st2  := FileTime64():NEW()
LOCAL st3

   st1:SetDateTime( DATE()    , TIME()     )
   st2:SetDateTime( DATE()+99 , "23:59:00" )

   st3 := ft64_ElapSeconds(st1,st2)

// Seconds
? st3
// Days
? INT(st3/60/60/24)
WAIT
RETURN
more about FileTime64() read here
ot4xb-wiki -docs - FILETIME64.zip
Doc File FileTime64()
(179.65 KiB) Downloaded 720 times
greetings by OHR
Jimmy

Post Reply