Yeah, it was actually working. Turns out the problem was my conversion to a time_t was failing, because I forgot to subtract 1900 from the year. I always got the same value (the unsigned interpretation of -1), so I thought GetFileTime was always returning the same result.
Anyway, since I already wrote the code to use the Windows GetFileTime() API, here it is. The ugly 64-bit arithmetic turned out to be unnecessary. It can be fairly easily modified to get creation or last-access times (which CVI's GetFileTime() will not do.)
Now I just need to sort out why my time_t results are 70 years higher than TestStand's Seconds(False) function. I suspect TestStand is using mktime() internally, and incorrectly subtracting 1970 from the year, instead of 1900.
-------------
#include
int DLLEXPORT __stdcall GetFileTimestamp( const char *filename, time_t *timestamp )
{
int error = 0;
HANDLE f = INVALID_HANDLE_VALUE;
FILETIME ftWrite;
SYSTEMTIME stUTC;
struct tm t;
time_t tt;
f = CreateFile( filename, GENERIC_READ, 0/*dwShareMode*/, NULL/*lpSecurityAttributes*/,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0/*hTemplateFile*/ );
if( f == INVALID_HANDLE_VALUE ) {
errChk( -1 );
}
// Get FILETIME, 100ns intervals since 1601 (UTC time)
#undef GetFileTime // Don't want the CVI version.
if( !GetFileTime( f, NULL/*lpCreationTime*/, NULL/*lpLastAccessTime*/, &ftWrite ) ) {
errChk( -2 );
}
// Convert to SYSTEMTIME, Y/M/D/h/m/s structure (still UTC)
if( !FileTimeToSystemTime(&ftWrite, &stUTC) ) {
errChk( -3 );
}
// Convert to Unix time_t (seconds since 1970, still UTC)
memset( &t, 0, sizeof(t) );
t.tm_year = stUTC.wYear-1900;
t.tm_mon = stUTC.wMonth-1; // struct tm uses 0-based month for some reason.
t.tm_mday = stUTC.wDay;
t.tm_hour = stUTC.wHour;
t.tm_min = stUTC.wMinute;
t.tm_sec = stUTC.wSecond;
t.tm_isdst = 0; // UTC has no daylight savings time, I believe.
tt = mktime(&t);
*timestamp = tt;
Error:
if( f != INVALID_HANDLE_VALUE )
CloseHandle(f);
return( error );
}