更新系统时间
例如:数据传输过程中数据不时出现丢失的情况,偶尔会丢失一部分数据
APP 中接收数据代码:
@Override
import subprocess
# 设置时间同步为网络时间服务器
def set_time_sync(server):
try:
subprocess.run(['w32tm', '/config', '/syncfromflags:manual', '/manualpeerlist:' + server, '/update'])
subprocess.run(['w32tm', '/resync'])
print("成功将时间同步设置为网络同步!")
except Exception as e:
print("设置时间同步失败:", e)
# 获取当前时间同步状态
def get_time_sync_status():
try:
result = subprocess.run(['w32tm', '/query', '/status'], capture_output=True, text=True)
status = result.stdout
print(status)
except Exception as e:
print("获取时间同步状态失败:", e)
# 示例:将时间同步设置为 pool.ntp.org 时间服务器
set_time_sync('pool.ntp.org')
# 示例:获取当前时间同步状态
get_time_sync_status()
# import subprocess
#
# # 使用管理员权限运行命令
# def run_command(command):
# subprocess.run(command, shell=True, capture_output=True, text=True)
#
# # 主动更新系统时间
# def update_system_time():
# command = "w32tm /stripchart /computer:time.windows.com /samples:1 /dataonly"
# run_command(command)
#
# # 主程序入口
# if __name__ == "__main__":
# update_system_time()
"""
w32tm /config /syncfromflags:domhier /update
net stop w32time
net start w32time
"""