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
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