Windows API Reference

Kernel-User Base / System Info / System Time

System Time

This section provides information on functions and structures related to retrieving and manipulating system time in Windows.

Core Functions

The following functions are commonly used to work with system time:

GetSystemTime

Retrieves the current UTC date and time. The system time is Coordinated Universal Time (UTC).

GetSystemTime(LPSYSTEMTIME lpSystemTime);
GetLocalTime

Retrieves the current local date and time. The local date and time is based on the system's time zone.

GetLocalTime(LPSYSTEMTIME lpSystemTime);
SetSystemTime

Sets the current UTC date and time. This function is typically reserved for administrators.

SetSystemTime(const LPSYSTEMTIME lpSystemTime);
SetLocalTime

Sets the current local date and time. This function is typically reserved for administrators.

SetLocalTime(const LPSYSTEMTIME lpSystemTime);
GetTickCount

Retrieves the number of milliseconds that have elapsed since the system was started. This function is deprecated. Use GetTickCount64 instead.

DWORD GetTickCount();
GetTickCount64

Retrieves the number of milliseconds that have elapsed since the system was started. This function is recommended for new applications.

ULONGLONG GetTickCount64();

Related Structures

Key structures used for time representation:

SYSTEMTIME

Represents a date and time, with individual members for the year, month, day, hours, minutes, seconds, and milliseconds.

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *LPSYSTEMTIME;
TIME_ZONE_INFORMATION

Contains information about the current time zone, including standard and daylight saving time rules.

typedef struct _TIME_ZONE_INFORMATION {
    LONG      Bias;
    WCHAR     StandardName[32];
    SYSTEMTIME StandardDate;
    LONG      StandardBias;
    WCHAR     DaylightName[32];
    SYSTEMTIME DaylightDate;
    LONG      DaylightBias;
} TIME_ZONE_INFORMATION, *LPTIME_ZONE_INFORMATION;

Additional Information

Time Zones and Daylight Saving Time

Windows manages time zones and daylight saving time transitions. Functions like GetTimeZoneInformation and SetTimeZoneInformation allow you to query and configure these settings.

File Times

For file system operations, you'll often encounter the FILETIME structure, which represents time as a 64-bit value counting the number of 100-nanosecond intervals since January 1, 1601 (UTC).

FILETIME

Represents a date and time value.

typedef struct _FILETIME {
    DWORD dwLowDateTime;
    DWORD dwHighDateTime;
} FILETIME, *LPFILETIME;
FileTimeToSystemTime

Converts a file time to system time (local or UTC).

BOOL FileTimeToSystemTime(const FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime);
SystemTimeToFileTime

Converts system time (local or UTC) to a file time.

BOOL SystemTimeToFileTime(const LPSYSTEMTIME lpSystemTime, LPFILETIME lpFileTime);

System Performance Timers

For high-resolution timing, consider using multimedia timers or QueryPerformanceCounter for measuring elapsed time with high precision.