FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour DateTimeDiff()
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
DateTimeDiff()
Posted: Wed Jul 02, 2025 01:36 PM
Hello friends,
Life goes on. When you dive deep into the core of FiveWin Harbour, it makes you sad that Mr. Rao is no longer with us. Still, we have to continue working hard and steadfastly on the Harbour/FiveWin project without him. I’ve tried to extend the `dateTimeDiff()` function. Maybe someone will take the time to test it.
Best regards,
Otto
#include "FiveWin.ch"

REQUEST DBFCDX
REQUEST HB_Lang_DE
REQUEST HB_CODEPAGE_DEWIN

//---------------------------------------------------------------------------//
// Portable macro for DateTimeDiff()
// Uses the C function DATETIME_DIFF under xHarbour
// Uses HB_DateTimeDiff under Harbour if available
#ifdef __XHARBOUR__
   #xtranslate DateTimeDiff( <y1>, <mo1>, <d1>, <h1>, <mi1>, <s1>, <y2>, <mo2>, <d2>, <h2>, <mi2>, <s2> ) => ;
                DATETIME_DIFF( <y1>, <mo1>, <d1>, <h1>, <mi1>, <s1>, <y2>, <mo2>, <d2>, <h2>, <mi2>, <s2> )
#else
   #xtranslate DateTimeDiff( <y1>, <mo1>, <d1>, <h1>, <mi1>, <s1>, <y2>, <mo2>, <d2>, <h2>, <mi2>, <s2> ) => ;
                HB_DateTimeDiff( HB_DateTimeCreate( <y1>, <mo1>, <d1>, <h1>, <mi1>, <s1> ), ;
                                 HB_DateTimeCreate( <y2>, <mo2>, <d2>, <h2>, <mi2>, <s2> ) )
#endif

//---------------------------------------------------------------------------//

FUNCTION Main()

   ? "Current date/time:", DateTime()

   // Calculate difference in seconds between two timestamps
   ? "Seconds between 2025-07-05 12:00:00 and 2025-07-06 12:00:00:"
   ? DateTimeDiff( 2025, 7, 5, 12, 0, 0, 2025, 7, 6, 12, 0, 0 )  // → 86400

   RETURN NIL

//---------------------------------------------------------------------------//
// C function to calculate time difference in seconds
// between two date-time combinations
// Embedded using #pragma BEGINDUMP, compiled by the C compiler

#pragma BEGINDUMP

#include <windows.h>   // Windows API types
#include <hbapi.h>     // xHarbour API access (parameters, return values)
#include <time.h>      // struct tm, mktime(), difftime()

HB_FUNC( DATETIME_DIFF )
{
   struct tm t1, t2;        // Two C structures for date/time
   time_t time1, time2;     // Unix timestamps
   double diff;             // Difference in seconds

   // Parameters 1–6: start timestamp
   int y1  = hb_parni(1);
   int mo1 = hb_parni(2);
   int d1  = hb_parni(3);
   int h1  = hb_parni(4);
   int mi1 = hb_parni(5);
   int s1  = hb_parni(6);

   // Parameters 7–12: end timestamp
   int y2  = hb_parni(7);
   int mo2 = hb_parni(8);
   int d2  = hb_parni(9);
   int h2  = hb_parni(10);
   int mi2 = hb_parni(11);
   int s2  = hb_parni(12);

   // Fill struct tm for start time
   t1.tm_year  = y1 - 1900;
   t1.tm_mon   = mo1 - 1;
   t1.tm_mday  = d1;
   t1.tm_hour  = h1;
   t1.tm_min   = mi1;
   t1.tm_sec   = s1;
   t1.tm_isdst = -1;  // Let the system determine DST

   // Fill struct tm for end time
   t2.tm_year  = y2 - 1900;
   t2.tm_mon   = mo2 - 1;
   t2.tm_mday  = d2;
   t2.tm_hour  = h2;
   t2.tm_min   = mi2;
   t2.tm_sec   = s2;
   t2.tm_isdst = -1;

   // Convert to time_t (Unix timestamps)
   time1 = mktime(&t1);
   time2 = mktime(&t2);

   // Calculate time difference in seconds
   diff = difftime(time2, time1);

   // Return value as LONG (safely cast from double)
   hb_retnl( (LONG) diff );
}

#pragma ENDDUMP
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: DateTimeDiff()
Posted: Wed Jul 02, 2025 04:04 PM

Master Otto, what is this command for?

Maestro Otto, ¿para qué es esta orden?

Gracias, tks.

Regards, saludos.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: DateTimeDiff()
Posted: Wed Jul 02, 2025 05:33 PM

João, to calculate difference in seconds between two timestamps.

Best regards,

Otto

Posts: 1283
Joined: Fri Feb 10, 2006 02:34 PM
Re: DateTimeDiff()
Posted: Thu Jul 03, 2025 05:30 PM
Hi,
	a := HB_StrToTS( '2025-01-01 00:00:00' ) 	//	String To Timestamp
	b := HB_StrToTS( '2025-01-01 00:00:10' ) 
	
	?  HB_Sec( b ) - HB_Sec( a )
C.
Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
HIX -> https://github.com/carles9000/hix
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: DateTimeDiff()
Posted: Thu Jul 03, 2025 05:46 PM

Carles, thank you.

I implemented it myself because HB_Sec() is not available in xHarbour.

According to ChatGPT, my approach using mktime() and difftime() from the C standard library is system-level, precise, and performs very well — even across leap years and day boundaries. It's also considered faster and more direct than emulating the Harbour-internal HB_Sec() logic.

Best regards,

Otto

Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: DateTimeDiff()
Posted: Thu Jul 03, 2025 06:35 PM
? Secs() 
? Seconds()
Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1286
Joined: Mon Feb 25, 2008 02:54 PM
Re: DateTimeDiff()
Posted: Fri Jul 11, 2025 08:15 PM
/*****************************************************************************/
function FT_Elapsed(dStart, dEnd, cTimeStart, cTimeEnd)
/*
*  RETORNA UM ARRAY QUE CONTEM OS SEGUINTES DADOS:
*
*     aRetVal[1,1]  INTEIRO DIAS     aRetVal[1,2] TOTAL DIAS     (nn.nnnn)
*     aRetVal[2,1]  INTEIRO HORAS    aRetVal[2,2] TOTAL HORAS    (nn.nnnn)
*     aRetVal[3,1]  INTEIRO MINUTOS  aRetVal[3,2] TOTAL MINUTOS  (nn.nnnn)
*     aRetVal[4,1]  INTEIRO SEGUNDOS aRetVal[4,2] TOTAL SEGUNDOS (nn)

*  EXEMPLOS:
*     FT_ELAPSED( CTOD('11/28/90'), CTOD('11/30/90'), '08:00:00', '12:10:30' )
*     VAI RETORNAR:
*
*     aRetVal[1,1] ->  2 (DIAS)      aRetVal[1,2] ->    2.1740  DIAS
*     aRetVal[2,1] ->  4 (HORAS)     aRetVal[2,2] ->   52.1750  HORAS
*     aRetVal[3,1] -> 10 (MINUTOS)   aRetVal[3,2] -> 3130.5000  MINUTOS
*     aRetVal[4,1] -> 30 (SEGUNDOS)  aRetVal[4,2] -> 187830     SEGUNDOS
*/
  Local nTotalSec, nCtr, nConstant, nTemp, aRetVal[4,2]

  if ! ( valtype(dStart) $ 'DC' )
     dStart     := date()
  elseif valtype(dStart) == 'C'
     cTimeStart := dStart
     dStart     := date()
  endif

  if ! ( valtype(dEnd) $ 'DC' )
     dEnd     := date()
  elseif valtype(dEnd) == 'C'
     cTimeEnd := dEnd
     dEnd     := date()
  endif

  IIf( valtype(cTimeStart) != 'C', cTimeStart := '00:00:00', )
  IIf( valtype(cTimeEnd)   != 'C', cTimeEnd   := '00:00:00', )

  nTotalSec  := (dEnd - dStart) * 86400                              + ;
                VAL(cTimeEnd)   *  3600                              + ;
                VAL(SUBSTR(cTimeEnd,AT(':', cTimeEnd)+1,2)) * 60     + ;
                IF(RAT(':', cTimeEnd) == AT(':', cTimeEnd), 0,         ;
                VAL(SUBSTR(cTimeEnd,RAT(':', cTimeEnd)+1)))          - ;
                VAL(cTimeStart) * 3600                               - ;
                VAL(SUBSTR(cTimeStart,AT(':', cTimeStart)+1,2)) * 60 - ;
                IF(RAT(':', cTimeStart) == AT(':', cTimeStart), 0,     ;
                VAL(SUBSTR(cTimeStart,RAT(':', cTimeStart)+1)))

  nTemp := nTotalSec

  for nCtr = 1 to 4
     nConstant := if(nCtr == 1, 86400, if(nCtr == 2, 3600, if( nCtr == 3, 60, 1)))
     aRetVal[nCtr,1] := int(nTemp/nConstant)
     aRetval[nCtr,2] := nTotalSec / nConstant
     nTemp -= aRetVal[nCtr,1] * nConstant
  next

return aRetVal
ubiratanmga@gmail.com

FWH24.04
BCC7.3
HARBOUR3.2
xMate
Pelles´C
TDolphin

Continue the discussion