FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Time Conversion
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Time Conversion
Posted: Mon Oct 25, 2010 02: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)
Posts: 363
Joined: Wed Feb 15, 2006 02:06 PM
Re: Time Conversion
Posted: Mon Oct 25, 2010 02: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 (fw): Select all Collapse
        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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Time Conversion
Posted: Mon Oct 25, 2010 03:35 PM
Code (fw): Select all Collapse
// 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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 824
Joined: Thu Oct 13, 2005 07:39 AM
Re: Time Conversion
Posted: Tue Oct 26, 2010 08:14 AM
Jeff,

these formulas should work

Code (fw): Select all Collapse
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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Time Conversion
Posted: Tue Oct 26, 2010 01: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

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 824
Joined: Thu Oct 13, 2005 07:39 AM
Re: Time Conversion
Posted: Tue Oct 26, 2010 03:57 PM
Hi James,

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

Code (fw): Select all Collapse
#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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Time Conversion
Posted: Tue Oct 26, 2010 06: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

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Time Conversion
Posted: Tue Oct 26, 2010 06:26 PM

Stefan,

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

Regards,
James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 824
Joined: Thu Oct 13, 2005 07:39 AM
Re: Time Conversion
Posted: Wed Oct 27, 2010 07: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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Time Conversion
Posted: Wed Oct 27, 2010 02: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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Time Conversion
Posted: Wed Oct 27, 2010 02:10 PM

You have to link ct.lib.

EMG

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Time Conversion
Posted: Wed Oct 27, 2010 02:12 PM

Thanks, Enrico, that explains it.

I still wonder what the difference is?

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Time Conversion
Posted: Wed Oct 27, 2010 02: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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Time Conversion
Posted: Wed Oct 27, 2010 02:20 PM

Got it. Thanks again Enrico.

Regards,
James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10

Continue the discussion