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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
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
jkrCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.