SYSTEMTIME struct in Windows and Chilkat

To work with dates and times in Chilkat C++, the CkDateTime class acts as the central hub, while CkDtObj is used as a data container for individual date/time components (Year, Month, Day, etc.).

Important Note for Chilkat v11.0.0+: Chilkat recently removed the direct GetAsSystemTime and SetFromSystemTime methods to maintain cross-platform consistency. The standard way to bridge to a Windows SYSTEMTIME now is via the CkDtObj class.

1. Get the Current Date/Time

To initialize a CkDateTime object with the current system time:

#include <CkDateTime.h>

CkDateTime dt;
// Sets the object to the current system time (UTC)
dt.SetFromCurrentSystemTime();

2. Populate a SYSTEMTIME from CkDateTime

To get the values out of CkDateTime and into a Microsoft SYSTEMTIME struct, you use ToDtObj

#include <CkDateTime.h>
#include <CkDtObj.h>
#include <Windows.h>

// 1. Get the components (use true for Local time, false for UTC)
CkDtObj dtObj;
dt.ToDtObj(true,dtObj);

SYSTEMTIME st;
st.wYear         = (WORD)dtObj.get_Year();
st.wMonth        = (WORD)dtObj.get_Month();
st.wDay          = (WORD)dtObj.get_Day();
st.wHour         = (WORD)dtObj.get_Hour();
st.wMinute       = (WORD)dtObj.get_Minute();
st.wSecond       = (WORD)dtObj.get_Second();
st.wMilliseconds = 0; // CkDtObj has second-level precision

// The "Windows Trick" to populate wDayOfWeek automatically:
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
FileTimeToSystemTime(&ft, &st);

// Now st.wDayOfWeek is correctly populated (0 = Sunday, 1 = Monday, etc.)

3. Load a CkDateTime from a SYSTEMTIME

To go the other direction, you populate a CkDtObj with your SYSTEMTIME values and then tell the CkDateTime object to load itself from that object.

SYSTEMTIME st;
GetLocalTime(&st); // Example: Get current local time into st

// 1. Create a CkDtObj and populate its properties
CkDtObj dtObj;
dtObj.put_Year(st.wYear);
dtObj.put_Month(st.wMonth);
dtObj.put_Day(st.wDay);
dtObj.put_Hour(st.wHour);
dtObj.put_Minute(st.wMinute);
dtObj.put_Second(st.wSecond);

// 2. Load the CkDateTime object
CkDateTime dt;
dt.SetFromDtObj(dtObj);

// Now 'dt' contains the time from your SYSTEMTIME struct.

Key Considerations:

  • Local vs. UTC: ToDtObj(true,dtObj) returns the local time based on the computer's timezone settings, while ToDtObj(false,dtObj) returns UTC.
  • Precision: CkDtObj provides second-level precision. If you require millisecond precision for your SYSTEMTIME, you may need to use dt.GetAsTimestamp(true) or dt.GetAsIso8601(true) and parse the fractional seconds manually, though for most application logic, second precision is sufficient.