Click here to Skip to main content
12,350,443 members (23,927 online)
Click here to Skip to main content
Add your own
alternative version

Stats

4.4K views
52 downloads
2 bookmarked
Posted

How to Decode the CIM_DATETIME Format Employed in WMI Queries Using C++

, 6 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A robust approach to decoding CIM_DATETIME for C++ programmers

Introduction

This will show how a C++ programmer may convert a time value returned from a WMI query into a format she/he will be more familiar with.

Background

WMI queries use a date and time format called CIM_DATETIME (Common Information Model). In the MSDN example Getting WMI Data from the local computer, the WIN32_OperatingSystem class contains this property:

datetime LastBootUpTime;

This is a CIM_DATETIME format string. It is also known as DMTF (Distributed Management Task Force) datetime format. Date and time are encoded as a string containing UTC time with an appended offset in minutes for the current locale. This is the offset of local time from UTC time. Encoded time values have a one microsecond resolution.
Scripting services are provided with SWbemDateTime() to parse this format. C# programmers may employ ManagementDateTimeConverter.ToDateTime:

// Convert DMTF datetime to System.DateTime
string dmtfDate = "20140408141835.999999+480";
DateTime dt = ManagementDateTimeConverter.ToDateTime(dmtfDate);

For C++ programmers in an unmanaged environment, they can if they wish parse the string directly. This is a perfectly acceptable approach. If in the future the encoded format is altered, then dependent code would be affected. Fortunately, SWbemDateTime() also exposes a COM interface which can be instantiated in this way:

ISWbemDateTime *pSWbemDateTime = NULL;
HRESULT hr = CoCreateInstance(CLSID_SWbemDateTime, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSWbemDateTime));

Using the Code

SWbemDateTime() can convert CIM_DATETIME values to and from FILETIME and VT_DATE values. VT_DATE is a lower precision format (500 millisecond) so this tip will concentrate on the FILETIME format which is probably of more interest to C++ programmers. The function CIMDateTimetoFiletime() below may be used to convert a CIM_DATETIME to a FILETIME in either local time or UTC time.

// Convert CIM_DATETIME to FILETIME either as UTC or local time.
// Input:  CIM_DATETIME as LPCWSTR
// Input: bool blocaltime: true compute local time else compute UTC time
// Output: FILETIME pointer
// Return: true for success
bool CIMDateTimetoFiletime(__in LPCWSTR cimdatetime, __out LPFILETIME filetime,
 __in const bool blocaltime = true)
{
 bool bSuccess = false;
 // Get a SWbemDateTime object.
 ISWbemDateTime *pSWbemDateTime = NULL;
 HRESULT hr = CoCreateInstance(CLSID_SWbemDateTime, NULL, CLSCTX_INPROC_SERVER,
  IID_PPV_ARGS(&pSWbemDateTime));
 if (SUCCEEDED(hr))
 {
  BSTR timebstr = SysAllocString(cimdatetime);
  if (timebstr)
  {
   // Set time value.
   hr = pSWbemDateTime->put_Value(timebstr);
   if (SUCCEEDED(hr))
   {
    BSTR bstrFileTime;
    // Get a FILETIME.
    hr = pSWbemDateTime->GetFileTime(blocaltime?VARIANT_TRUE:VARIANT_FALSE,
     &bstrFileTime);
    if (SUCCEEDED(hr))
    {
     ULARGE_INTEGER tempFT;
     // 64-bit value representing the number of 100-nanosecond intervals 
     // since January 1, 1601 (UTC).
     tempFT.QuadPart = _wtoi64(bstrFileTime); 
     // FILETIME is a structure of two 32-bit integers.
     filetime->dwLowDateTime = tempFT.LowPart;
     filetime->dwHighDateTime = tempFT.HighPart;
     bSuccess = true;
     SysFreeString(bstrFileTime);
    }
   }
   SysFreeString(timebstr);
  }
  pSWbemDateTime->Release();
 }
 return bSuccess;
}

The project supplied uses the example from MSDN to extract the LastBootTime and convert to FILETIME as both UTC and local times.

History

  • v1.00 07-04-2014

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

pwasser
Engineer (retired)
Australia Australia
Started Electrical Engineering, then graduated in Physics (and Mathematics). Hated batch Fortran and began real programming in Basic on PDP11, HP9825 and CP/M Z80 then dBase, Pascal, C and others. Nowadays mostly C++ and C# on Windows. Have worked in the fields of Geophysics, Biophysics, Cartography, NDT and SCADA. Playing with Arduino.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 7 Apr 2014
Article Copyright 2014 by pwasser
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid