Setting time and date using Python / Linux

I am developing an inventory management system that will hopefully be deployed across several of our factories. I am going to use Linux SBC platform (Pi, Beaglebone ...... etc.). I want my user interface to be non-Linux so the end user doesn't have to know the Linux OS or how to navigate the command line. My application will automatically launch on boot and provide the end user with all the required interfaces using PMI / Tkinter HMI. My prototype is based on a Raspberry Pi B. Since many of the installations may not have an available network, I need a way to set the system time via the UI via python. I am using I2C Dallas 3231 RTC chip on GPIO pins with i2C interface. Everything is designed exceptthat there is no easy way for Python to set the system time, write it to the RTC, and ignore NPT timing if network is available. It may be very simple, but I'm stumped.

+3


source to share


1 answer


Someone might think of something better, but this is how I have done it in the past.



import subprocess
import datetime

try:
    subprocess.check_call("ntpdate") #Non-zero exit code means it was unable to get network time
except subprocess.CalledProcessError:
    dt = getRTCTime() # Get time from RTC as a datetime object
    subprocess.call(['sudo', 'date', '-s', '{:}'.format(dt.strftime('%Y/%m/%d %H:%M:%S'))], shell=True) #Sets system time (Requires root, obviously)

#Rest of code

      

0


source







All Articles