Page 1 of 1

Compare two hours

PostPosted: Sun Mar 29, 2009 2:12 pm
by Marco Turco
Hi,
anyone know a function to compare two hours ?

Imagine you have cTime1="10.55" and cTime2="13.44",
is there a function to know if cTime1>cTime2 ?

Thanks in advance

Re: Compare two hours

PostPosted: Sun Mar 29, 2009 2:35 pm
by Armando
Marco:

Take a look at the TimeToSec function of xHarbour.

This function converts the time in seconds and then you can compare seconds against seconds

Regards

Re: Compare two hours

PostPosted: Sun Mar 29, 2009 2:37 pm
by pablovidal
Code: Select all  Expand view


Local cTime1 := "10:00"
Local cTime2 := "13:20"

If TimeAsSeconds( cTime1 ) <= TimeAsSeconds( cTime2 )
 ....
EndIf

FUNCTION TimeAsSeconds( cTime )
RETURN VAL( cTime ) * 3600 + VAL( SUBSTR( cTime, 4 ) ) * 60 + VAL( SUBSTR( cTime, 7 ) )

 

Re: Compare two hours

PostPosted: Sun Mar 29, 2009 3:19 pm
by Rick Lipkin
Just a quick observation .. why can't you not change them to values and compare the numbers ?

Code: Select all  Expand view

Imagine you have cTime1="10.55" and cTime2="13.44",
is there a function to know if cTime1>cTime2 ?

 



Code: Select all  Expand view


lOK := .F.

nTIME1 := VAL("10.55")
nTIME2 := VAL("13.44")

nDIFF := nTIME1 - nTIME2

IF nDIFF > 0
    lOK := .T.  
ELSE
    lOK := .F.
ENDIF

RETURN(lOK)

 


On the other hand still as "char" cTime2 would always be greater than cTime1 as you have them defined .. am I missing something here ?? :?:


Rick Lipkin

Re: Compare two hours

PostPosted: Sun Mar 29, 2009 3:39 pm
by sygecom
For xHarbour:
MsgInfo(ElapTime( "10:55:00", "13:44:00" ))

Re: Compare two hours

PostPosted: Sun Mar 29, 2009 7:05 pm
by Marco Turco
Ok. I am using TimeToSec() for this special situation.

Thanks to all for the great support.

Re: Compare two hours

PostPosted: Tue Mar 31, 2009 10:10 am
by Silvio
Marco Turco wrote:Hi,
anyone know a function to compare two hours ?

Imagine you have cTime1="10.55" and cTime2="13.44",
is there a function to know if cTime1>cTime2 ?

Thanks in advance


Marco,
when I come back home Ican send you a function
( date1,ora1,date2,ora2)
I use it to rent beach services

Re: Compare two hours

PostPosted: Thu Apr 02, 2009 7:25 am
by Zupan Miran
I use this function:
Code: Select all  Expand view

Function DRazlika(dStartDate,cStartTime,dEndDate,cEndTime)
Return (((dEndDate - dStartDate) * 86400) - TimeToSec(cStartTime)) + TimeToSec(cEndTime)
 


... and in PRG code is somethnig like this
Code: Select all  Expand view

//*** End_Time, Start_Time are string ("12:15", "13:15")
        dd2 :=alltrim(End_Time)
        dd1 :=alltrim(Start_Time)
        dd3 :=DRazlika(@StartDate,@dd1,@End_Date,@dd2)    //*** Return dd3 in seconds
        dd4 :=int(dd3/86400)                           //*** No of days
        dd5 :=dd3-(dd4*86400)                        
        dd6 :=int(dd5/3600)                            //*** No in hours
        dd7 :=(dd5-dd6*3600)/60                     //*** rest in minutes
...
...
 @ row ,4 say "Result: "+str(dd4,2)+" days "+str(dd6,2)+" hours "+str(dd7,2)+" min"

 


Regards
Zupan Miran
Slovenia