How do I parse a string into a FILETIME?

Raymond Chen

Public Service Announcement:Daylight Saving Time ends in most parts of the United States this weekend.Other parts of the world may change on a different day from theUnited States.

The NLS functions in Win32 providefunctions to convert aSYSTEMTIME into a string,but it does not provide any functions to perform the reverseconversion.Here are few things you can try:

The OLE automationVarDateFromStr conversion function converts a string intoa DATE.From there, you can convert it to some other format.

BOOL SystemTimeFromStr(__in LPCWSTR psz, LCID lcid, __out LPSYSTEMTIME pst)
  DATE date;
  return SUCCEEDED(VarDateFromStr(psz, lcid, 0, &date)) &&
         VariantTimeToSystemTime(date, pst);
BOOL FileTimeFromStr(__in LPCWSTR psz, LCID lcid, __out LPFILETIME pft)
  return SystemTimeFromStr(psz, lcid, &st) &&
         SystemTimeToFileTime(&st, pft);

If you have something inwhich parses CIMdatetimeformat(whichThe Scripting Guys liken to Klingon)you can use theSWbemDateTime object.Since this is a scripting object, using it from C++ is rathercumbersome.

BOOL FileTimeFromCIMDateTime(__in LPCWSTR psz, __out LPFILETIME pft)
 BOOL fSuccess = FALSE;
 ISWbemDateTime *pDateTime;
 HRESULT hr = CoCreateInstance(__uuidof(SWbemDateTime), 0,
                 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDateTime));
 if (SUCCEEDED(hr)) {
  BSTR bstr = SysAllocString(psz);
  if (bstr) {
   hr = pDateTime->put_Value(bstr);
   if (SUCCEEDED(hr)) {
    BSTR bstrFT;
    hr = pDateTime->GetFileTime(VARIANT_FALSE, &bstrFT);
    if (SUCCEEDED(hr)) {
     __int64 i64FT = _wtoi64(bstrFT);
     pft->dwLowDateTime = LODWORD(i64FT);
     pft->dwHighDateTime = HIDWORD(i64FT);
     fSuccess = TRUE;
 return fSuccess;

From the managed side, you haveDate­Time.Try­ParseandDate­Time.Parse­Exact methods.

I leave you to investigate the time zone and locale issues associatedwith these techniques.(Because I can’t be bothered.)