The WebGetSystemTime function returns the current date and
time into a SYSTEMTIME structure. Unlike the GetSystemTime
function, this function does not use the local system clock. Instead,
it obtains the current time from the SocketTools API server, which is
synchronized with official time servers using the Network Time
Protocol (NTP). This helps ensure that the returned value is accurate
and has not been altered by the user or affected by incorrect system
clock settings.
This function can be useful in scenarios where an application must
rely on a trusted and consistent time source. For example, it may be
used for validating software licenses, enforcing time-based
restrictions, generating timestamps for logging or auditing, or
coordinating events across multiple systems where the local system
time cannot be assumed to be accurate.
Because the time value is retrieved from a remote server, this
function requires an active network connection and may be subject to
network latency or temporary connectivity issues. To reduce network
overhead, the function internally caches the most recently retrieved
time value for a short period (typically up to 60 seconds). When the
function is called again within this interval, it does not immediately
query the server. Instead, it calculates the elapsed time since the
last successful request using a high-resolution system tick count and
adjusts the cached time value accordingly.
This approach allows the returned time to continue advancing
between server synchronizations, providing a smooth and consistent
progression of time values without requiring a network request for
each call. However, because this adjustment is based on the local
system timer, it should be considered an approximation of the actual
server time. The time value will be re-synchronized with the server
periodically to maintain overall accuracy.
In most cases, it is recommended that applications use the
WEB_TIME_UNIVERSAL option when storing or comparing date/time
values. Universal (UTC) time provides a consistent,
timezone-independent reference that avoids issues related to daylight
saving time changes or differences in local system settings. This is
particularly important when timestamps are shared between systems or
persisted for later comparison.
The WEB_TIME_LOCAL option can be used when the application
requires a date/time value adjusted for the user's current timezone,
such as when displaying timestamps in a user interface. However,
because the timezone adjustment is based on the local system
configuration, it may still be affected by incorrect timezone or
daylight saving settings on the client system.
Developers should consider using the local system time (for
example, via GetSystemTime or GetLocalTime) when
absolute accuracy is not critical or when network access is
unavailable. Retrieving the time locally avoids the overhead of a
network request and is typically sufficient for general-purpose tasks
such as UI updates, file timestamps, or short-lived timing operations.
This function is not intended to replace standard system time APIs
for high-frequency or real-time operations. Applications that require
precise time measurements or guaranteed monotonic behavior should use
dedicated high-resolution timing APIs instead of relying on this
function.
SYSTEMTIME stDateTime;
if (WebGetSystemTime(WEB_TIME_UNIVERSAL, &stDateTime))
{
_tprintf(_T("The current date and time is %04hu-%02hu-%02huT%02hu:%02hu:%02huZ\n"),
stDateTime.wYear,
stDateTime.wMonth,
stDateTime.wDay,
stDateTime.wHour,
stDateTime.wMinute,
stDateTime.wSecond);
}
else
{
DWORD dwError = WebGetLastError();
_tprintf(_T("Unable to determine the current date and time, error 0x%08lx\n"), dwError);
}