Solved

Convert date/time to FILETIME

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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…

773 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