How to create a wordclock with Python
How I built a word clock that tells time in three zones
Some projects are born out of necessity; others, out of sheer “I never did this before so I guess it will turn out great”-attitude. This project firmly falls into the latter category. Inspired by the concept of word clocks (thanks Waldek Mastykarz) and motivated by the challenge of tackling time zones, I wanted to create a Python-powered word clock that tells time in phrases across three zones: Pacific Time (PT), Greenwich Mean Time (GMT), and Indian Standard Time (IST).
How I approached it
When I’m tackling something new, it helps to divide and conquer. Here’s how I broke the problem down into smaller steps:
Displaying time in words
The first step was figuring out how to translate digital time into phrases like “It’s a quarter past six.” I created rules based on the minute ranges:
- If the minute is between
0
and5
, show “It’s [hour] o’clock.” - If the minute is between
15
and20
, show “It’s a quarter past [hour].” - If the minute is between
35
and40
, show “It’s twenty-five to [next hour].”
I stored these rules in a function called get_highlighted_phrases
, which dynamically selects phrases to highlight based on the current time.
Building the clock GUI
Once I had the logic for the words, I needed a way to display them. I used Tkinter to build a graphical interface where the phrases are arranged in a circle, mimicking the feel of a classic clock. Each time zone has its own circle with phrases like “It’s” and “half past” positioned radially.
Handling time zones
Here’s where things got tricky. Time zones aren’t just about adding or subtracting hours. Some, like Indian Standard Time (IST), involve half-hour offsets. To handle this, I used the pytz
library for PT and GMT but calculated IST manually using:
def get_ist_from_gmt(gmt_time):
return gmt_time + timedelta(hours=5, minutes=30)
AM/PM quirks
One of the most unexpected challenges was ensuring the AM/PM indicator displayed correctly for IST, especially during transitions like midnight. I ended up flipping AM/PM specifically for IST using a conditional flag:
def get_am_pm(hour_24, invert=False):
if invert:
return "AM" if hour_24 >= 12 else "PM"
else:
if hour_24 == 0:
return "AM"
elif hour_24 < 12:
return "AM"
elif hour_24 == 12:
return "PM"
else:
return "PM"
Debug tricks
Testing multiple time zones in real time meant countless print()
statements, which eventually morphed into a helpful debugging function:
def debug_current_times(pt_time, gmt_time, ist_time):
print("Debugging Current Times:")
print(f"PT: {format_time(pt_time)}")
print(f"GMT: {format_time(gmt_time)}")
print(f"IST: {format_time(ist_time, invert_am_pm=True)}")
print("-" * 30)
Where you can find the code
If you’re curious about how it all came together, or you want to try it yourself, check out the code at https://github.com/LuiseFreese/wordclock
You May Also Like
Introducing Bluesky Terminal Poster
Guide on how ton post from your terminal
Introducing the SVG to JSON for SharePoint List formatter
In my latest blog posts, I played a lot of SVGs in SharePoint lists. For everyone who isn’t aware - Unlike other image formats like .png or .jpg, .svg are vectors - which can be expressed as …
ProvisionGenie - an open-source provisioning engine for Microsoft Teams
Once upon a time I teamed up with my friend and partner in crime Carmen Ysewijn. We both work as Power Platform developers and Microsoft 365 consultants, and got both tired of doing the same things …