Skip to content

Instantly share code, notes, and snippets.

@dileepadev
Last active August 12, 2024 14:00
Show Gist options
  • Save dileepadev/77fb1e5841f884d11ad1b693fdfcdd1e to your computer and use it in GitHub Desktop.
Save dileepadev/77fb1e5841f884d11ad1b693fdfcdd1e to your computer and use it in GitHub Desktop.
WindowsOS-DateTimeSyncNow-Python

WindowsOS-DateTimeSyncNow-Python

A simple Python script with a batch file that helps synchronize the Windows OS local computer system date and time with the Windows Time Server.

@echo off
cd /d "%~dp0"
pythonw.exe "DateTimeSyncNow.py"
import subprocess
import ctypes
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def set_time_zone(timezone_name):
subprocess.run(['tzutil', '/s', timezone_name], capture_output=True, text=True)
def sync_time_with_server(server_name):
try:
subprocess.run(['w32tm', '/config', '/manualpeerlist:' + server_name, '/syncfromflags:manual', '/reliable:YES', '/update'], check=True, capture_output=True, text=True)
subprocess.run(['w32tm', '/resync'], check=True, capture_output=True, text=True)
print("Date and time successfully updated and synced with the time server.")
except subprocess.CalledProcessError as e:
print("Failed to sync with the time server. Error:", e.stderr)
if __name__ == "__main__":
if not is_admin():
print("Please run this script as an administrator.")
else:
# Set the time zone for Sri Lanka (Replace with your desired timezone).
timezone = "Sri Lanka Standard Time"
set_time_zone(timezone)
# Specify the time server you want to sync with.
time_server = "time.windows.com"
sync_time_with_server(time_server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment