Time Conversion

Time Conversion

Postby Jeff Barnes » Mon Oct 25, 2010 2:41 pm

Hi,

I need to change a Time value into a decimal format so I can do some calculations.
Is there a function that does this?

Ex: Total Time Worked = 8:45:00 (8 hours, 45 minutes, 0 seconds)
In a decimal format it would be 8.75 hours.

I need a formula that would calculate this as the Total Time Worked will be a changing variable.
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Time Conversion

Postby PeterHarmes » Mon Oct 25, 2010 2:46 pm

I use the following code by calculating the total hours between a start date & timer and an end date & time, you can pick the bones out of this!!:

Code: Select all  Expand view
        IF m_astart == m_aend .AND. LEN(ALLTRIM(m_asttime)) == 5 .AND. ;
            LEN(ALLTRIM(m_aedtime)) == 5
            dec_strt := VAL(LEFT(m_asttime,2)) + (VAL(RIGHT(m_asttime,2)) / 60)
            dec_end  := VAL(LEFT(m_aedtime,2)) + (VAL(RIGHT(m_aedtime,2)) / 60)
            IF dec_end - dec_strt > 0
                m_tothrs := dec_end - dec_strt
            ENDIF
        ELSEIF m_astart < m_aend .AND. LEN(ALLTRIM(m_asttime)) == 5 .AND. ;
            LEN(ALLTRIM(m_aedtime)) == 5
            dec_strt := VAL(LEFT(m_asttime,2)) + (VAL(RIGHT(m_asttime,2)) / 60)
            dec_end  := VAL(LEFT(m_aedtime,2)) + (VAL(RIGHT(m_aedtime,2)) / 60)
            dec_end  += 24 * (m_aend - m_astart)
            IF dec_end - dec_strt > 0
                m_tothrs := dec_end - dec_strt
            ENDIF
        ENDIF
PeterHarmes
 
Posts: 363
Joined: Wed Feb 15, 2006 2:06 pm
Location: Oxford, England

Re: Time Conversion

Postby James Bott » Mon Oct 25, 2010 3:35 pm

Code: Select all  Expand view
// Time Functions

       
// Time Difference
// Examples: tdiff( "08:12:22", "22:45:09"), tdiff( 455, 1789 )
// Result will be in time or seconds format depending on what was passed.
FUNCTION TDIFF(uStart, uEnd)
   local uResult
   IF VALTYPE( uStart ) = "N"
       uResult = uEnd - uStart
   ELSE
       uResult = STOT( TTOS( uEnd ) - TTOS( uStart ) )
   ENDIF
RETURN uResult



// Time To Seconds
FUNCTION TTOS( cTime )
   default cTime:= time()
RETURN ( VAL( SUBSTR( cTime, 1, 2 )) * 3600  + ;
         VAL( SUBSTR( cTime, 4, 2 )) * 60    + ;
         VAL( SUBSTR( cTime, 7, 2 )) ;
       )


// Seconds To Time      
FUNCTION STOT(nSeconds)
   IF nSeconds > 86400
      nSeconds = MOD( nSeconds, 86400 )
   ENDIF
RETURN STRZERO( INT( MOD( nSeconds / 3600, 24 )), 2, 0 ) + ':' + ;
       STRZERO( INT( MOD( nSeconds /   60, 60 )), 2, 0 ) + ':' + ;
       STRZERO( INT( MOD( nSeconds       , 60 )), 2, 0 )
       
// EOF
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Time Conversion

Postby StefanHaupt » Tue Oct 26, 2010 8:14 am

Jeff,

these formulas should work

Code: Select all  Expand view
Function Time2Dec (nMin,nSec)
Return ( ((nMin*60)+nSec)*100/3600 )

Function Dec2Time (nDec)
Local nTemp := nDec*60
Local nMin := Int (nTemp)
Local nSec := (nTemp-nMin)*60
Return ( StrZero(nMin,2)+":"+StrZero(nSec,2) )
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Time Conversion

Postby James Bott » Tue Oct 26, 2010 1:03 pm

Stephan,

Under some conditions you may have rounding errors. By default the decimal position is set to 2. Ideally, your functions should change to a much larger SET DECIMAL, do the calculations, then set it back to whatever it was when the function was called. The app also would need to have the decimals set higher than 2 or any calculations done with the decimal times could be off.

They would also be more useful if they handled hours too. Better yet, if they handled the HH:MM:SS format.

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Time Conversion

Postby StefanHaupt » Tue Oct 26, 2010 3:57 pm

Hi James,

here is a revised version of the functions with a small sample. Now they handle the hours also.

Code: Select all  Expand view
#include "Fivewin.ch"

PROCEDURE Main ()

LOCAL cTime := ""
LOCAL nTime := Time2Dec ("08:45:30")

MsgInfo ("Time: 08:45:30, decimal: "+Str (nTime,12,8)+CRLF+;
         "decimal: "+Str (nTime,12,8)+", Time: "+ Dec2Time(nTime),;
         "Convert Time to decimal and vice versa")

RETURN (nil)


// Convert a timestring to its decimal equivalent
//-----------------------------------------------
Function Time2Dec (cTime)

  LOCAL nTime := 0.0, nDec := 0.0
  LOCAL nHour := VAL (StrToken (cTime, 1,":"))
  LOCAL nMin  := VAL (StrToken (cTime, 2,":"))
  LOCAL nSec  := VAL (StrToken (cTime, 3,":"))
 
  nDec := ((nMin*60)+nSec)*100/3600
  nTime := nHour + (nDec/100)

Return (nTime)


// convert decimal time back to real time
//-----------------------------------------
Function Dec2Time (nDec)

  LOCAL cTime := ""
  Local nTemp := Frac (nDec)*60
  LOCAL nHour := INT (nDec)
  Local nMin  := Int (nTemp)
  Local nSec  := (nTemp-nMin)*60

  cTime := StrZero (nHour,2,0)+":"+StrZero(nMin,2)+":"+StrZero(nSec,2)

Return (cTime)

// get the fraction of a decimal number
//-------------------------------------
FUNCTION Frac (nNum)

RETURN ( ABS(nNum)-Floor(ABS(nNum)) )
 


I made some tests with SET DECIMAL, this setting has no influence on the calculation. SET DECIMALS only affects the output on the screen, but only if SET FIXED is ON. IF SET FIXED is OFF, SET DECIMALS is ignored.
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Time Conversion

Postby James Bott » Tue Oct 26, 2010 6:21 pm

Stefan,

I tried to test your code but I don't have the Floor() function. Is this part of (x)Harbour or FWH? Or, did you write this function yourself?

How is Floor() different from Int()?

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Time Conversion

Postby James Bott » Tue Oct 26, 2010 6:26 pm

Stefan,

Ok, I just changed Floor() to Int() and it seems to be working fine. Good work.

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Time Conversion

Postby StefanHaupt » Wed Oct 27, 2010 7:25 am

James,

Floor() is part of xharbour and returns the next lower integer value. In this case you can replace it with Int ().
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Time Conversion

Postby James Bott » Wed Oct 27, 2010 2:03 pm

Stefan,

Floor() is part of xharbour and returns the next lower integer value.


Strange, I am using the latest FWH/xHarbour versions (10.9) and Floor() errors out. By the "next lower integer value" do you mean the same value as Int() returns or the next lower one than that?

When I look up the definitions of Floor and Int they appear to be the same.

Floor: Returns the largest integer less than or equal to the specified number.
http://msdn.microsoft.com/en-us/library ... floor.aspx

Int: a numeric function that converts a numeric value to an integer by truncating--not rounding--all digits to the right of the decimal point. (from Clipper manual)

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Time Conversion

Postby Enrico Maria Giordano » Wed Oct 27, 2010 2:10 pm

You have to link ct.lib.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8711
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Time Conversion

Postby James Bott » Wed Oct 27, 2010 2:12 pm

Thanks, Enrico, that explains it.

I still wonder what the difference is?

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Time Conversion

Postby Enrico Maria Giordano » Wed Oct 27, 2010 2:17 pm

The difference lies on how the negative numbers are treated:

? floor (1.1) --> 1.0
? floor (-1.1) --> -2.0

The function FLOOR() determines the biggest integer that is smaller
than <nNumber>.


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8711
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Time Conversion

Postby James Bott » Wed Oct 27, 2010 2:20 pm

Got it. Thanks again Enrico.

Regards,
James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: nageswaragunupudi and 33 guests