How to use a unix timestamp for an adDate ADO Parameter in c++ without COleDateTime

How to use a unix timestamp for an adDate ADO Parameter in c++ without COleDateTime

Post by peterlo » Mon, 19 Mar 2007 22:44:28


Just thought I'd share this because it has been bugging me for
hours...

When you're calling a SQL Server stored procedure with a DATE
parameter, this is how you can add the parameter in ADO (without the
need for COleDateTime)....



double * UnixTimestampToVariantTime(time_t unixTimestamp);

....

dtmGenerated = UnixTimestampToVariantTime(time(NULL)); // This
converts the current unix timestamp (calculated by time(NULL)).

sqlCmdUpdateEvents->Parameters->Append(sqlCmdUpdateEvents-
(ParameterDirectionEnum)adParamInput, sizeof(DATE),
(DATE)*dtmGenerated));

....

// Converts a unix timestamp into one that ADO can decipher
double * UnixTimestampToVariantTime(time_t unixTimestamp)
{
double *dblDBTS = new double;
SYSTEMTIME dbDate;
ZeroMemory(&dbDate, sizeof(SYSTEMTIME));

// Get the tm Struct for the specified unix timestamp
struct tm *timeUnits = localtime(&unixTimestamp);

// Translate the tm struct into a SYSTEMTIME struct
dbDate.wDay = (WORD)timeUnits->tm_mday;
dbDate.wDayOfWeek = (WORD)timeUnits->tm_wday;
dbDate.wHour = (WORD)timeUnits->tm_hour;
dbDate.wMilliseconds = (WORD)0;
dbDate.wMinute = (WORD)timeUnits->tm_min;
dbDate.wMonth = (WORD)timeUnits->tm_mon+1;
dbDate.wSecond = (WORD)timeUnits->tm_sec;
dbDate.wYear = (WORD)timeUnits->tm_year+1900;

// Calculate and return the Variant time for the populated SYSTEMTIME
struct
SystemTimeToVariantTime((LPSYSTEMTIME)&dbDate, dblDBTS);
return dblDBTS;
}




... Just snippets, but you get the idea.