如何从ntp中提取秒

2024-04-20 04:41:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以用下面的代码从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: '-'

Tags: 代码import服务器fordatetimeresponsewith时间