The basic clock function was too simple. Not including the LCD library, supplied by Adafruit (the company that makes the LCD Pi Plate), a total of four lines of Python code are required to write the local time zone date/time and the UTC date/time to the LCD. Like this:
now = datetime.datetime.now()
utc = datetime.datetime.utcnow()
lcd.message(now.strftime(‘%b %d %H:%M:%SE\n’))
lcd.message(utc.strftime(‘%b %d %H:%M:%SZ’))
The first two lines read the system time and store it using datetime format. The first line saves the local time (now), and the second line saves the UTC time (utc). The next two lines write the date/time info out to the display with the local time on the first line and UTC time on the second. It looks like this:
What took a bit of head scratching was avoiding a fairly ugly situation where the upper line might update first, showing something like “Dec 30 20:19:45E”, while the second line lagged behind at “Dec 30 01:19:44Z” for a fraction of a second. This lag was due to inefficiency of writing two entire lines to the LCD every second tick. After all, only once in every 60 updates does the minute field change. And only once in every 3600 updates does the hour field change. So I optimized the code to perform the minimum amount of updating required by each situation. The result was that the second fields on the upper and lower line now change at virtually the same time.
I embellished the project a bit by adding a sign-on screen that shows for 10 seconds after power is applied to the RPi. It shows the IP address of the Clock, which is useful for connecting to it remotely since it doesn’t have a traditional keyboard and display.
This clock is fantastic! I remember reading this about 4 years ago and set one up with local and Zulu time on my Pi, and now trying to build another as an insurance policy in case the first card corrupts. Reading through your forum again, I couldn’t seem to locate your wb4son python code package. Would you be willing to share the wb4son_ntp_clock/clock.py& package please? Thanks again for a great program!
Hi – I have a link on the tab for the clock that you can fetch the source, but here it is:
Click here for the python program (change the name from Clock.txt to Clock.py — WordPress doesn’t allow .py files): https://wb4son.com/wpblog/wp-content/uploads/2013/01/Clock1.txt