import ntptime,time
#本地话时间 东八区
def datetime():
date=time.localtime(time.time()+28800)
#(2024, 4, 29, 9, 58, 14, 0, 120) 年 月 日 时 分 秒 周 全年的第几天
return date
# 定义NTP服务器列表
ntp_servers = [
'ntp.ntsc.ac.cn',
'ntp1.aliyun.com',
'ntp.tencent.com',
'pool.ntp.org'
]
#NTP同步
def ntp(ntp_servers):
for server in ntp_servers:
try:
# 尝试从当前服务器同步时间
ntptime.host = server
ntptime.settime()
print("时间已从 {} 同步".format(server))
return
except Exception as e:
print("从 {} 同步时间失败: {}".format(server, e))
continue
print("无法从任何服务器同步时间")
ntp(ntp_servers)
NTP校时间及本地化时间 micropython
发表评论