Alright, taking a slight detour from the usual Hive development topics today! Sometimes I tinker with little Python scripts just for fun or to solve a small, everyday annoyance. This is one I find myself using occasionally when I want a different way to see the time.
Instead of just seeing 10:15
or 14:48
, I thought it would be neat to have a script that approximates the time in words, like those old-school word clocks – you know, "quarter past ten" or "ten 'til three".
The Concept
The idea is pretty simple: take the current time, round the minutes to the nearest 5-minute interval, and then translate that into a common English phrase relative to the hour. It needs to handle "o'clock", "past" the hour (like five past, ten past, quarter past, half past), and "til" the next hour (like twenty-five 'til, quarter 'til, five 'til).
The Python Script
It's a fairly straightforward script with no external dependencies beyond Python itself. It uses a couple of lists/dictionaries to store the word representations for hours and the minute phrases, does a bit of rounding math, and then figures out whether to use "past" or "'til".
Here's the code if you want to try it out or see how it works:
#!/usr/bin/env -S uv run --quiet --script
# /// script
# requires-python = ">=3.13"
# dependencies = [
# ]
# ///
import datetime
# Hour names for 12-hour clock, note index 0 -> 12, 1->1, ..., 11->11
hours = [
"twelve", # 12 AM / PM
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
]
# Mapping minutes to words mapped by 5-min intervals from 0 to 55
# A tuple format (phrase, direction), direction = 'past' or 'til' or None for exact o'clock
minute_phrases = {
0: ("o'clock", None),
5: ("five", "past"),
10: ("ten", "past"),
15: ("quarter", "past"),
20: ("twenty", "past"),
25: ("twenty-five", "past"),
30: ("half", "past"),
35: ("twenty-five", "til"),
40: ("twenty", "til"),
45: ("quarter", "til"),
50: ("ten", "til"),
55: ("five", "til"),
}
def print_time(now: datetime.datetime):
# Round minutes to nearest multiple of 5
minute = now.minute
remainder = minute % 5
if remainder >= 3:
# Round up if 3 or 4 minutes past the multiple of 5
minute_rounded = minute + (5 - remainder)
else:
# Round down if 0, 1, or 2 minutes past
minute_rounded = minute - remainder
# If minute_rounded is 60 (due to rounding up from 58/59), advance hour
if minute_rounded == 60:
# Use modulo 24 for hour arithmetic, then modulo 12 for display
hour = (now.hour + 1) % 24
minute_rounded = 0
else:
hour = now.hour
# Determine if hour should be "til" the next hour or "past" current hour
phrase, direction = minute_phrases[minute_rounded]
# Get the correct hour index for the 12-hour 'hours' list
# hour % 12 gives 0 for 12 AM/PM, 1 for 1 AM/PM, etc. which matches our list index
current_hour_index = hour % 12
next_hour_index = (hour + 1) % 12 # Index for the next hour
if direction == "til":
# Use the word for the *next* hour
display_hour_word = hours[next_hour_index]
print(f"{phrase} 'til {display_hour_word}")
elif direction == "past":
# Use the word for the *current* hour
display_hour_word = hours[current_hour_index]
# Handle special cases like "quarter past" vs "fifteen past" (using the phrase directly)
print(f"{phrase} past {display_hour_word}")
else: # direction is None, meaning o'clock
# Use the word for the *current* hour
display_hour_word = hours[current_hour_index]
print(f"{display_hour_word} {phrase}")
if __name__ == "__main__":
now = datetime.datetime.now()
print(f"Current numeric time: {now.strftime('%H:%M:%S')}") # Added this for comparison
print("Approximate time in words:")
print_time(now)
Why? Just Because!
No grand purpose here, really. It's just a slightly more human-readable way to glance at the time, maybe useful if you run it in a terminal window or as part of a status bar script (which was my original intent). Sometimes coding little things like this is just fun.
Hope you enjoyed this little detour from the usual Hive stuff!
As always,
Michael Garcia a.k.a. TheCrazyGM