3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonで時刻同期(Windows)する

Last updated at Posted at 2018-11-15

#使用する技術

  • ctypes
  • PythonでC/C++の共有ライブラリを使用することができるライブラリ。

#必要条件

  • 管理者権限での実行

#サンプルソース

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

# 時刻同期関数に渡してあげるための構造体を作成する。
class SYSTEMTIME(Structure):
    _fields_ = [
      ('wYear',            WORD),
      ('wMonth',           WORD),
      ('wDayOfWeek',       WORD),
      ('wDay',             WORD),
      ('wHour',            WORD),
      ('wMinute',          WORD),
      ('wSecond',          WORD),
      ('wMilliseconds',    WORD),
    ]

# 設定する時刻の構造体にデータを入れる
setTimeData = SYSTEMTIME()
setTimeData.wYear = 2018
setTimeData.wMonth = 11
setTimeData.wDayOfWeek = 0  # 曜日は勝手に入る
setTimeData.wDay = 15
setTimeData.wHour = 17
setTimeData.wMinute = 0
setTimeData.wSecond = 0
setTimeData.wMilliseconds = 0

# 時刻同期する部分
SetLocalTime = windll.kernel32.SetLocalTime
ret = SetLocalTime(pointer(setTimeData))

# 結果判定
if ret == 0:
    print('設定に失敗しました。管理者権限で実行してください。')
else:
    print('成功しました。システムを終了します。')

#所感
Cの機能がPythonの手軽さで使えるので、
便利といえば便利。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?