Solved

Convert date/time to FILETIME

Posted on 1998-10-20
2
3,210 Views
Last Modified: 2009-01-09
Is there a Win32 or MFC function that converts a local timestamp specified as year, month, day, hour, minute, second to a local time FILETIME structure?

CTime will accept a FILETIME and will give you back year, month, day, hour, minute, second.  And you can construct a CTime by specifying year, month, day, hour, minute, second, but it will not return a FILETIME.

SystemTimeToFileTime() does this conversion, but it assumes SYSTEMTIME is UTC and FILETIME is local, so it also does timezone conversion, but in the wrong direction for my need.  There's also a SystemTimeToTzSpecificLocalTime() function, but again it's converting the UTC to local.  I could use this if there was a function to convert local to UTC.
0
Comment
Question by:clance
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1175663
1. There is a function which converts a CTime to a FILETIME in AFXIMPL.H (this lives in the MFC source directory - **not** the MFC include directory).

The function is prototyped :-

void AFX_CDECL AfxTimeToFileTime(const CTime& time, LPFILETIME pFileTime);


2. The code that implements this is in FILEST.CPP in the MFC source directory


3. This is an "undocumented" MFC function.  You won't find it in the help.


4.  Either include AFXIMPL.H and use this function, or if you don't like to use undocumented functions, copy the code from FILEST.CPP into your source under a different function name.

5. if you are interested in how it works, here's the code from FILEST.CPP

void AFX_CDECL AfxTimeToFileTime(const CTime& time, LPFILETIME pFileTime)
{
      SYSTEMTIME sysTime;
      sysTime.wYear = (WORD)time.GetYear();
      sysTime.wMonth = (WORD)time.GetMonth();
      sysTime.wDay = (WORD)time.GetDay();
      sysTime.wHour = (WORD)time.GetHour();
      sysTime.wMinute = (WORD)time.GetMinute();
      sysTime.wSecond = (WORD)time.GetSecond();
      sysTime.wMilliseconds = 0;

      // convert system time to local file time
      FILETIME localTime;
      if (!SystemTimeToFileTime((LPSYSTEMTIME)&sysTime, &localTime))
            CFileException::ThrowOsError((LONG)::GetLastError());

      // convert local file time to UTC file time
      if (!LocalFileTimeToFileTime(&localTime, pFileTime))
            CFileException::ThrowOsError((LONG)::GetLastError());
}


0
 

Author Comment

by:clance
ID: 1175664
Answer2000,
I swear I accepted your answer before, but I guess it didn't get processed.  Your solution worked, but in a very different way than I thought.

Essentually, what AfxTimeToFileTime() does is take the local time and converts it to the opposite direction of UTC, but then it makes the opposite conversion again leaving it result back to local time.  It works, but for all the wrong reasons.

Thanks
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question