Solved

Convert date/time to FILETIME

Posted on 1998-10-20
2
3,191 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 May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

736 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