如何从ntp服务器获取的时间中提取秒数

时间:2019-07-15 21:04:01

标签: python datetime time digits ntp

我可以使用以下代码从NTP服务器获取当前时间。现在,我尝试从时间上获取秒数,但是它没有用。有人可以解释一下我如何只从时间开始花费秒钟。

我试图将时间转换为带有整数的列表,然后采用18位和19位数字。

import ntplib
from datetime import datetime, timezone
c = ntplib.NTPClient()
        # Provide the respective ntp server ip in below function
response = c.request('uk.pool.ntp.org', version=3)
response.offset
        # UTC timezone used here, for working with different timezones you can use [pytz library][1]
currenttime =datetime.fromtimestamp(response.tx_time, timezone.utc)
print (currenttime)
d = map(int, str(currenttime))
print (list(d))

这就是我在控制台上看到的。

2019-07-15 20:56:19.952231+00:00

ValueError: invalid literal for int() with base 10: '-'

1 个答案:

答案 0 :(得分:1)

从您已经创建的datetime对象中提取秒数:

d = currenttime.second

另请参阅:datetime.second - Python documentation