Time synchronization (Windows) with Python

Technology to use

Requirements

--Execute with administrator privileges

Sample source

Sample.py


# coding: Shift_JIS
from ctypes import Structure, windll, pointer
from ctypes.wintypes import WORD

#Create a structure to pass to the time synchronization function.
class SYSTEMTIME(Structure):
    _fields_ = [
      ('wYear',            WORD),
      ('wMonth',           WORD),
      ('wDayOfWeek',       WORD),
      ('wDay',             WORD),
      ('wHour',            WORD),
      ('wMinute',          WORD),
      ('wSecond',          WORD),
      ('wMilliseconds',    WORD),
    ]

#Put data in the structure of the set time
setTimeData = SYSTEMTIME()
setTimeData.wYear = 2018
setTimeData.wMonth = 11
setTimeData.wDayOfWeek = 0  #The day of the week is selfish
setTimeData.wDay = 15
setTimeData.wHour = 17
setTimeData.wMinute = 0
setTimeData.wSecond = 0
setTimeData.wMilliseconds = 0

#Time synchronization part
SetLocalTime = windll.kernel32.SetLocalTime
ret = SetLocalTime(pointer(setTimeData))

#Result judgment
if ret == 0:
    print('Setting failed. Execute with administrator privileges.')
else:
    print('Succeeded. Shut down the system.')

Impressions

Because C functions can be used with the convenience of Python Speaking of convenience, it is convenient.

Recommended Posts

Time synchronization (Windows) with Python
Python starting with Windows 7
Python with VS Code (Windows 10)
Run python with PyCharm (Windows)
Easy folder synchronization with Python
Execution time measurement with Python With
Face detection with YOLO Face (Windows10, Python3.6)
Build mlpy with python3.3 (64bit) (windows 64bit)
Use Windows 10 speech synthesis with Python
[Linux, Windows] Time synchronization setting / confirmation
Getting started with Python 3.8 on Windows
[Python] Creating multiple windows with Tkinter
[Python Windows] pip install with Python version
FizzBuzz with Python3
First time python
Scraping with Python
Run servo with Python on ESP32 (Windows)
Statistics with python
Python on Windows
Scraping with Python
Python with Go
python windows environment
Twilio with Python
Building a Python 3.6 environment with Windows + PowerShell
Integrate with Python
Python installation (Windows)
python time measurement
Play with 2016-Python
AES256 with python
Tested with Python
Develop Windows apps with Python 3 + Tkinter (Preparation)
python starts with ()
First time python
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Project cannot be created with Python3.5 (Windows) + django1.7.1
[Python3] A story stuck with time zone conversion
Python (Windows 10) Virtual Environment / Package with VS Code
Use logger with Python for the time being
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Convert Windows epoch values to date with python
Develop Windows apps with Python 3 + Tkinter (exe file)
Image Processing with Python Environment Setup for Windows
How to measure execution time with Python Part 1
Use Python / Django with Windows Azure Cloud Service!
How to measure execution time with Python Part 2
✨ Easy with Python ☆ Estimated elapsed time after death ✨
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Socket communication with Python
Data analysis with python 2
Python: Time Series Analysis
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python