Skip to content

Instantly share code, notes, and snippets.

@K-atc
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save K-atc/aa25befcc04404fd1647 to your computer and use it in GitHub Desktop.

Select an option

Save K-atc/aa25befcc04404fd1647 to your computer and use it in GitHub Desktop.
Windows 時刻合わせバッチ

What is this?

NTPが使えない環境のための時刻合わせバッチファイル(スクリプト)

Requirements

  • 実行時に管理者権限
  • Python
  • requestsモジュール(pip install requests)
  • インターネット

Install

  1. Download Gist よりファイルを一括でダウンロード
  2. 少なくともpythonスクリプトは C:\net-time においてください
  • 内部で python C:\net-time\net-time.py を実行するため
  • 管理者権限でcmdを実行するとカレントディレクトリがSystem32になってしまう対策

Usage

  1. 管理者権限で net-time.BAT を実行
FOR /F "usebackq" %%t IN (`python C:\net-time\net-time.py`) DO @SET NOWTIME=%%t
time %NOWTIME%
import math
import requests
import json
j = json.loads(requests.get('http://ntp-a1.nict.go.jp/cgi-bin/json').text)
n = float(j['st'])
n += 0.5 # 遅延分修正
h = (int( n / 3600 % 24) + 9) % 24 # JST = GMT + 9H
m = int(n / 60 % 60)
s = int(n % 60)
ms = n % 1
time = str(h).rjust(2,'0') + ":" + str(m).rjust(2,'0') + ":" + str(s).rjust(2,'0') + "." + str(int(ms * 100))
print(time)
@K-atc
Copy link
Author

K-atc commented Apr 6, 2015

各言語で作って比較したら面白そう

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment