我可以用下面的代码从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: '-'
从已创建的
datetime
对象中提取秒数:另见:^{} - Python documentation
相关问题 更多 >
编程相关推荐