Pythonで日本標準時を取得・表示する

※細かいことは考慮していません。

# モジュール読み込み
import requests
import json
from datetime import datetime
from time import ctime

# NICTの日本標準時をJSONで取得
response = requests.get("http://ntp-b1.nict.go.jp/cgi-bin/json")
response.encoding = response.apparent_encoding
response_text = response.text

# JSONから日本標準時を取得し、日付に変換
response_json = json.loads(response_text)
st = response_json["st"]
st_ctime = ctime(st)
st_datetime = datetime.strptime(st_ctime, "%a %b %d %H:%M:%S %Y")

# 表示
print(st_datetime)

実行結果

2018-09-01 21:43:55

NICTからは下記形式のJSONが返却されます。

{
 "id": "ntp-b1.nict.go.jp",
 "it": 0.000,
 "st": 1535805835.215,
 "leap": 36,
 "next": 1483228800,
 "step": 1
}

また、ctimeの結果をprintすると下記のようになります。

Sat Sep  1 21:43:55 2018