1
0

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 5 years have passed since last update.

Python で ntp server から時刻を取得する (Python 2 版)

Posted at

Python で ntp server から時刻を取得する (Python 2 版)

そういえば、何のために書いたのか思い出せないけど、昔書いたなあと思い出したので.
単純な UDP パケットが飛んでいるだけなんですよね.
まあ、それを基に高度な計算をしているはずなんですが...

これ、Python 2 でしか動かないから Python 3 に移植しよう(笑).

テスト

>>> ntp_now('ntp.jst.mfeed.ad.jp')
datetime.datetime(2019, 4, 28, 21, 18, 22)

コード

def ntp_now(server, port = 123):
    from socket import socket, AF_INET, SOCK_DGRAM
    from struct import unpack
    from datetime import datetime
    s = socket(AF_INET, SOCK_DGRAM)
    try:
        s.sendto('\x1b' + 47 * '\0', (server, port))
        result = s.recvfrom(1024)[0]
    finally:
        s.close()
    if result:
        return datetime.fromtimestamp(unpack('!12I', result)[10] - 2208988800L)
    else:
        None
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?