Solved

Convert date/time to FILETIME

Posted on 1998-10-20
2
3,095 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
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now