如何从 ntp 服务器获取的时间中提取秒数
How to extract seconds from time taken from ntp server
我能够使用以下代码从 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
对象中提取秒数:
d = currenttime.second
我能够使用以下代码从 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
对象中提取秒数:
d = currenttime.second