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



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-
CreateParameter("@TimeGenerated", (DataTypeEnum)adDBTimestamp,
(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.

.



Relevant Pages

  • Re: Error when calling System.Runtime.InteropServices.Marshal.PtrToStructure
    ... correct declaration for SYSTEMTIME: ... public struct SYSTEMTIME ... public int JobId; ... public string pPrinterName; ...
    (microsoft.public.dotnet.framework.interop)
  • Re: newbie could somone please tell me why this code does not work
    ... Change your struct to look like this: -- Note the use of StructLayout ... > public static extern bool GetSystemTime(ref SystemTime ...
    (microsoft.public.dotnet.languages.csharp)
  • problem with time.h
    ... I'm trying to pass a pointer to (struct osa_localtime) and get the ... } SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME; ... void OSAGetLocaleTime(SYSTEMTIME *local_time); ... int test_osa_local_time ...
    (comp.lang.c)
  • Date and Time issues
    ... Now I would lke to port System::DateTime and for now I am using ... a SYSTEMFILE structure to hold time information. ... } SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME; ... Or do I need to use a time_t struct? ...
    (microsoft.public.vc.mfc)