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
Solved

Convert date/time to FILETIME

Posted on 1998-10-20
2
3,152 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

792 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