Set the hardware clock in Python?

后端 未结 4 1100
星月不相逢
星月不相逢 2020-12-17 23:32

How do I set the hardware clock with Python on embedded Linux systems?

4条回答
  •  旧时难觅i
    2020-12-17 23:44

    An updated version on Ubuntu 16.04:

    import subprocess
    import shlex
    
    subprocess.call(shlex.split("timedatectl set-ntp false"))  # May be necessary
    subprocess.call(shlex.split("sudo date -s '2 OCT 2006 18:00:00'"))
    subprocess.call(shlex.split("sudo hwclock -w"))
    

    Important note: you may need to change the time/date settings to manually set (set-ntp false) or else it will immediately change it back to the current time.

    hwclock -w sets the hardware clock based on the current system time (set by date)

    It is required that date & hwclock are run as sudo as well.

提交回复
热议问题