import time
import os
import psutil
import ntplib
from datetime import datetime
def get_network_time():
ntp_client = ntplib.NTPClient()
response = ntp_client.request('pool.ntp.org')
return datetime.fromtimestamp(response.tx_time)
def get_battery_life():
battery = psutil.sensors_battery()
return battery.percent
def countdown(days):
target_time = datetime.now() + timedelta(days=days)
while datetime.now() < target_time:
network_time = get_network_time()
battery_life = get_battery_life()
print(f"网络时间:{network_time},电量:{battery_life}%")
remaining_time = (target_time - datetime.now()).total_seconds()
time.sleep(remaining_time)
countdown(10)
这段代码实现了倒计时功能,倒计时天数为10天。在倒计时过程中,会显示网络时间和电量百分比。请注意,这段代码需要在支持psutil
库的Python环境中运行。