Create and initialize a new date item with market day validation
InitializeDateItem creates and initializes a new date item from either a FILETIME
or LARGE_INTEGER representation, with automatic validation to ensure the date is a valid market day
(not a weekend or holiday).
This function serves as the primary interface for creating date items within the date log system. It ensures that all created date items represent valid market trading days by:
DATE_ITEM structureFILETIME or LARGE_INTEGERHRESULT _Must_inspect_result_ _Success_(hDateItem != NULL && SUCCEEDED(return))
InitializeDateItem(
_In_ _Pre_valid_ DATE_LOG_HANDLE hDateLogHandle,
_In_opt_ _When_(pLargeInteger == NULL, _Notnull_) PFILETIME pFileTime,
_In_opt_ _When_(pFileTime == NULL, _Notnull_) PLARGE_INTEGER pLargeInteger,
_Out_ DATE_ITEM_HANDLE* hDateItem
);
Standard C calling convention with SAL annotations for strict parameter validation and output guarantees.
_Must_inspect_result_ annotation
indicates that callers must check the return value. Ignoring the return code may result in undefined
behavior or resource leaks.
[In] DATE_LOG_HANDLE
Handle to the date log that provides the context for date validation and holidays reference.
InitializeDateLogHandle()ERR_INVALID_DATELOG_HANDLE because holidays handles cannot be used to create date items.
[In, Optional] PFILETIME
Pointer to a FILETIME structure representing the date to initialize.
pLargeInteger is NULLpLargeInteger must be NULL (mutual exclusivity enforced by SAL)FILETIME (100-nanosecond intervals since 1601)_When_(pLargeInteger == NULL, _Notnull_) enforces that when
pLargeInteger is NULL, pFileTime must be provided (non-NULL).
[In, Optional] PLARGE_INTEGER
Pointer to a LARGE_INTEGER structure representing the date to initialize.
pFileTime is NULLpFileTime must be NULL (mutual exclusivity enforced by SAL)_When_(pFileTime == NULL, _Notnull_) enforces that when
pFileTime is NULL, pLargeInteger must be provided (non-NULL).
[Out] DATE_ITEM_HANDLE*
Pointer to receive the newly created date item handle.
DATE_ITEM_HANDLEFreeDateItem()*hDateItem != NULL is guaranteed. If this contract is violated (function returns success
but *hDateItem == NULL), the function will fail with ERROR_INVALID_HANDLE.
Date item was successfully created and initialized. The *hDateItem parameter contains a valid handle to the new date item representing a market day.
The hDateLogHandle is invalid or does not reference a valid date log context. This occurs when:
IsValidDateLogHandle(hDateLogHandle) returns FALSEInvalid parameter combination. This occurs when:
pFileTime and pLargeInteger are NULLhDateItem is NULLThe provided date represents a weekend (Saturday or Sunday) or is listed in the holidays collection. Market trading does not occur on these days, so the date item cannot be created.
Unexpected error during holiday or market day validation. This typically indicates a failure in IsFiletimeMarketHoliday() or internal date conversion routines.
The handle type is invalid or not supported for date item creation. This can occur if:
// Recommended error checking pattern
DATE_ITEM_HANDLE hNewDate = NULL;
HRESULT hr = InitializeDateItem(
hDateLog,
&fileTime,
NULL,
&hNewDate
);
if (SUCCEEDED(hr) && hNewDate != NULL) {
// Date item successfully created
printf("Date item created successfully\n");
// Use hNewDate...
FreeDateItem(hNewDate);
} else if (hr == ERR_DATE_IS_HOLIDAY) {
printf("Cannot create date item - weekend or holiday\n");
} else if (FAILED(hr)) {
printf("Failed to create date item: 0x%08X\n", hr);
}
The function first validates input parameters:
hDateLogHandle must be valid via IsValidDateLogHandle()pFileTime or pLargeInteger must be provided (mutual exclusivity)hDateItem output pointer must not be NULLThe function determines and validates the handle type:
The function checks that the provided date is a valid market day:
IsFiletimeMarketHoliday(pFileTime, pHolidays)ERROR_UNIDENTIFIED_ERRORERR_DATE_IS_HOLIDAYLARGE_INTEGER_TO_FILETIME macroIsFiletimeMarketHoliday() on the converted FILETIMEOnce validated, create the new date item:
NewDateItem(pFileTime, pLargeInteger, hDateItem)Immediately after creation, enforce the output contract:
*hDateItem == NULL, return ERROR_INVALID_HANDLENewDateItem() implementationThe function classifies dates as holidays if they are:
FileTimeToSystemTime, SystemTimeToFileTime) which operate in local time or UTC
depending on the FILETIME source. Ensure consistency with the holidays list timezone.
When IsFiletimeMarketHoliday() is called, it returns one of three values:
| Return Value | Meaning | Action Taken |
|---|---|---|
| -1 | Error during validation | Return ERROR_UNIDENTIFIED_ERROR to caller |
| 0 | Valid market day (not holiday, not weekend) | Proceed to date item creation via NewDateItem() |
| > 0 (typically 1) | Holiday or weekend detected | Return ERR_DATE_IS_HOLIDAY to caller |
The function validates handles in this order:
IsValidDateLogHandle(hDateLogHandle) — immediate rejection if falseThe SAL annotations enforce that exactly one date input is provided:
pFileTime is provided, pLargeInteger must be NULLpLargeInteger is provided, pFileTime must be NULL
// Get current date in FILETIME format
SYSTEMTIME st = { 0 };
GetLocalTime(&st);
st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
FILETIME ft = { 0 };
SystemTimeToFileTime(&st, &ft);
// Create date item (will fail if today is a weekend or holiday)
DATE_ITEM_HANDLE hDate = NULL;
HRESULT hr = InitializeDateItem(hDateLog, &ft, NULL, &hDate);
if (SUCCEEDED(hr) && hDate != NULL) {
printf("Successfully created date item for today\n");
FreeDateItem(hDate);
} else if (hr == ERR_DATE_IS_HOLIDAY) {
printf("Today is not a market day\n");
} else {
printf("Error: 0x%08X\n", hr);
}
// Create a date for a specific day using LARGE_INTEGER
// Windows FILETIME = 100-nanosecond intervals since 1601-01-01
LARGE_INTEGER li = { 0 };
// For example: 132700000000000000 represents a specific date
li.QuadPart = 132700000000000000LL;
DATE_ITEM_HANDLE hDate = NULL;
HRESULT hr = InitializeDateItem(hDateLog, NULL, &li, &hDate);
if (SUCCEEDED(hr) && hDate != NULL) {
printf("Date item created from LARGE_INTEGER\n");
FreeDateItem(hDate);
}
// Try to create a date item, retry with next day if it's a holiday
DATE_ITEM_HANDLE hDate = NULL;
LARGE_INTEGER liDate = { 0 };
// Start with a target date
liDate.QuadPart = 132700000000000000LL;
// Attempt creation; if it's a holiday, try the next day
for (int attempts = 0; attempts < 10; attempts++) {
HRESULT hr = InitializeDateItem(hDateLog, NULL, &liDate, &hDate);
if (SUCCEEDED(hr) && hDate != NULL) {
printf("Found valid market day\n");
FreeDateItem(hDate);
break;
} else if (hr == ERR_DATE_IS_HOLIDAY) {
// Skip to next day (86400000000000 = 1 day in 100-nanosecond units)
liDate.QuadPart += 86400000000000LL;
printf("Skipping holiday, trying next day\n");
} else {
printf("Error creating date item: 0x%08X\n", hr);
break;
}
}
DATE_ITEM_HANDLE hDate = NULL;
FILETIME ft = { 0 };
SystemTimeToFileTime(&st, &ft);
// Works with DATE_LOG_HANDLE
HRESULT hr1 = InitializeDateItem(hDateLog, &ft, NULL, &hDate);
// Works with DATE_RANGE_HANDLE
HRESULT hr2 = InitializeDateItem(hDateRange, &ft, NULL, &hDate);
// FAILS with HOLIDAYS_HANDLE - returns ERR_INVALID_DATELOG_HANDLE
HRESULT hr3 = InitializeDateItem(hHolidays, &ft, NULL, &hDate);
if (hr3 == ERR_INVALID_DATELOG_HANDLE) {
printf("Cannot use holidays handle to create date items\n");
}
InitializeDateItem is thread-safe. It acquires the holidays context mutex internally
via IsFiletimeMarketHoliday() to safely access the shared holidays list. Multiple threads
can simultaneously call this function on the same date log handle.
On successful return, the caller assumes ownership of the returned DATE_ITEM_HANDLE and
must call FreeDateItem(hDate) when done to release memory and resources.
When constructing LARGE_INTEGER dates, remember:
LARGE_INTEGER_TO_FILETIME macro to convert safelyThe function ignores all time components (hours, minutes, seconds, milliseconds) from the input. It extracts only the date portion for validation and storage. This ensures that all date items represent "day-level" granularity regardless of input time precision.
The function checks errors in this priority order:
The _Success_ SAL annotation documents the contract:
hDateItem != NULL && SUCCEEDED(return). This means:
*hDateItem is guaranteed to be non-NULL*hDateItem is guaranteed to be NULLNewDateItem() — Low-level date item creation (called internally)FreeDateItem() — Release date item resourcesIsFiletimeMarketHoliday() — Market day validationInitializeDateLogHandle() — Create date log contextIsValidDateLogHandle() — Validate handle integrity