Write Python script to get difference between OS and BIOS time in Windows
- User
Write Python script to get difference between OS and BIOS time in Windows
- Assistant
Write Python script to get difference between OS and BIOS time in Windows
To get the difference between the OS time and the BIOS (hardware) time on a Windows system using Python, you can follow these steps:
datetime module.Key point: The BIOS clock typically keeps UTC time; the OS time is usually local time or can also be UTC depending on settings.
We will use the pywin32 package or wmi Python package to access WMI and query the BIOS time.
wmi Python package to query Win32_UTCTime or Win32_TimeZone or the BIOS time.Win32_ComputerSystem or Win32_LocalTime or call native Windows API via ctypes.However, Windows does not expose BIOS hardware clock time directly through WMI in a straightforward way. But you can read the hardware clock (RTC) time by calling the Windows API GetSystemTime (which returns UTC OS time) or GetLocalTime.
Alternatively, one approach is to call Windows WmiMonitorBrightness but that is not relevant here.
Since Windows does not expose BIOS time directly, one approach is to call the Windows command line tool w32tm or use wmic (Windows Management Instrumentation Command-line) to get the BIOS (hardware) time.
But wmic class Win32_BIOS provides only BIOS version data, not time.
Another approach is to read the hardware clock time via Windows API:
GetSystemTime (UTC) or GetLocalTime (local time).Win32_LocalTime and compare with RTC time read via native APIs (can be complicated).Use w32tm /stripchart or w32tm /query /status to get the time difference with the hardware clock or network time.
But since request is for Python script, I will provide a script that obtains:
Since direct reading of RTC is complicated, a good proxy method for BIOS time is:
ctypes to call Windows API GetSystemTime (UTC) [which gets system time that should be synced with BIOS].ctypes to call GetLocalTime (local OS time).But that is still OS-level, not RTC time.
There is a Windows API named GetSystemTimeAdjustment and GetSystemTimePreciseAsFileTime, but they do not give access to BIOS time.
Therefore, I'll provide a script that:
w32tm /stripchart /computer:localhost /dataonly /samples:1 which pings the time source and shows the time difference between the local clock and time source, which could be the BIOS time or reference clock.Then parse that output to get the difference.
Alternatively, since this is complicated, the simplest way is to get the system time from OS and the hardware time from CMOS using ctypes to call GetSystemTime (which returns the OS time in UTC) and GetLocalTime (local time). This isn't difference with BIOS, but it's what is accessible.
GetSystemTime and compare with local OS time.import ctypes
from datetime import datetime, timedelta
class SYSTEMTIME(ctypes.Structure):
_fields_ = [
('wYear', ctypes.c_ushort),
('wMonth', ctypes.c_ushort),
('wDayOfWeek', ctypes.c_ushort),
('wDay', ctypes.c_ushort),
('wHour', ctypes.c_ushort),
('wMinute', ctypes.c_ushort),
('wSecond', ctypes.c_ushort),
('wMilliseconds', ctypes.c_ushort),
]
def get_system_time_utc():
st = SYSTEMTIME()
ctypes.windll.kernel32.GetSystemTime(ctypes.byref(st)) # UTC time
dt = datetime(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds*1000)
return dt
def get_local_time():
st = SYSTEMTIME()
ctypes.windll.kernel32.GetLocalTime(ctypes.byref(st)) # Local OS time
dt = datetime(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds*1000)
return dt
if __name__ == '__main__':
utc_time = get_system_time_utc()
local_time = get_local_time()
# The difference between local and UTC is the timezone offset + DST etc.
diff = local_time - utc_time
print(f"UTC time from GetSystemTime: {utc_time}")
print(f"Local time from GetLocalTime: {local_time}")
print(f"Difference (Local - UTC): {diff}")
GetSystemTime returns the system time in UTC.GetLocalTime returns the system time in local time.If you want the raw CMOS hardware clock (RTC) time, Windows does not provide an easy public API for user programs.
If you want me to provide a script that calls external commands or uses WMI to get time info, please let me know.
Not yet included in a published release.