How to convert Win32 FILETIME to Unix time_t in C++

Any one how to do it in Visual Studio C++
tommym121Asked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Sorry, 'FileTimeToSystemTime()' (http://msdn.microsoft.com/en-us/library/ms724280%28VS.85%29.aspx) is even easier for that:
#include <time.h>

time_t FILETIME_to_time_t(const FILETIME *lpFileTime) {

  time_t result;

  SYSTEMTIME st;

  struct tm tmp;

  FileTimeToSystemTime(lpFileTime,&st);

  memset(&tmp,0,sizeof(struct tm));

  tmp.tm_mday = st.wDay;
  tmp.tm_mon  = st.wMonth - 1;
  tmp.tm_year = st.wYear - 1900;

  tmp.tm_sec  = st.wSecond;
  tmp.tm_min  = st.wMinute;
  tmp.tm_hour = st.wHour;

  result = mktime(&tmp);

  return result;
} 

Open in new window

0
 
phoffricConnect With a Mentor Commented:
0
 
pgnatyukCommented:
I think it should be something like that:
//      FILETIME flTime
__int64 t2 = flTime.dwHighDateTime;
t2 <<= 32;
t2 |= flTime.dwLowDateTime;

opposite:
MSDN. Converting a time_t Value to a File Time
http://msdn.microsoft.com/en-us/library/ms724228(v=VS.85).aspx

Other related things:
CodeProject. WinAPI - simple operations with datetime
http://www.codeproject.com/KB/datetime/winapi_datetime_ops.aspx

Using the API to perform time/date arithmetic to find relative times
http://edn.embarcadero.com/article/26051
0
 
jkrCommented:
One simple way would be to use 'FileTimeToDosDateTime()' (http://msdn.microsoft.com/en-us/library/ms724274%28VS.85%29.aspx) with 'mktime()' (http://www.cplusplus.com/reference/clibrary/ctime/mktime/):
#include <time.h>

time_t FILETIME_to_time_t(const FILETIME *lpFileTime) {

  time_t result;

  DWORD dwDate, dwTime;

  struct tm tmp;

  FileTimeToDosDateTime(lpFileTime,&dwDate,&dwTime);

  memset(&tmp,0,sizeof(struct tm));

  tmp.tm_mday = dwDate & 0x1F;
  tmp.tm_mon  = (dwDate >> 4) & 0x0F;
  tmp.tm_year = (dwDate >> 9) + 1980 - 1900;

  tmp.tm_sec  = dwTime & 0x0F
  tmp.tm_min  = (dwTime >> 4) & 0x1F;
  tmp.tm_hour = dwTime >> 10;

  result = mktime(&tmp);

  return result;
} 

Open in new window

0
All Courses

From novice to tech pro — start learning today.