Compare commits
3 Commits
66648900dd
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 44b7d2ef0a | |||
| b8d3f3b943 | |||
| 62ce77fdf4 |
118
fuzzy_clock.py
118
fuzzy_clock.py
@@ -1,92 +1,48 @@
|
||||
#!/usr/bin/python3
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Name: Fuzzy Clock
|
||||
# Version: 0.2.1
|
||||
# Last Updated: 7/12/22
|
||||
# Author: Billie O'Dwyer
|
||||
# Email: billie@odwyer.xyz
|
||||
# Version: 1.0.0
|
||||
# Last Updated: 14/1/25
|
||||
# Author: Eibhlín O'Dwyer
|
||||
# Email: eibhlin@beod.co.uk
|
||||
# Description: A script that returns the time in a speech like manner, for example 'quarter past two'
|
||||
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
def return_hour(n):
|
||||
# Determine what the current hour to return is
|
||||
if n == 1 or n == 13:
|
||||
return "one"
|
||||
if n == 2 or n == 14:
|
||||
return "two"
|
||||
if n == 3 or n == 15:
|
||||
return "three"
|
||||
if n == 4 or n == 16:
|
||||
return "four"
|
||||
if n == 5 or n == 17:
|
||||
return "five"
|
||||
if n == 6 or n == 18:
|
||||
return "six"
|
||||
if n == 7 or n == 19:
|
||||
return "seven"
|
||||
if n == 8 or n == 20:
|
||||
return "eight"
|
||||
if n == 9 or n == 21:
|
||||
return "nine"
|
||||
if n == 10 or n == 22:
|
||||
return "ten"
|
||||
if n == 11 or n == 23:
|
||||
return "eleven"
|
||||
if n == 12 or n == 0 or n == 24:
|
||||
return "twelve"
|
||||
def get_fuzzy_time(hour, minute):
|
||||
""" Turn a 24-hour clock output into a human-speech like string.
|
||||
"""
|
||||
# Dictionary defining integers:strings
|
||||
hours = ["twelve", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven"]
|
||||
|
||||
def to_or_past(n):
|
||||
# Determine whether to return to, past, or other
|
||||
if 1 <= n <= 32:
|
||||
return "past"
|
||||
if 33 <= n <= 57:
|
||||
return "to"
|
||||
# Dictionary definining integer ranges:strings
|
||||
minutes = {
|
||||
(3, 7, "five"), (53, 57, "five"),
|
||||
(8, 12, "ten"), (48, 52, "ten"),
|
||||
(13, 17, "quarter"), (43, 47, "quarter"),
|
||||
(18, 22, "twenty"), (38, 42, "twenty"),
|
||||
(23, 27, "twenty-five"), (33, 37, "twenty-five"),
|
||||
(28, 32, "half")
|
||||
}
|
||||
|
||||
def return_minute(n):
|
||||
# Determine what the current minute to return is
|
||||
if 3 <= n <= 7 or 53 <= n <= 57:
|
||||
return "five"
|
||||
if 8 <= n <= 12 or 48 <= n <= 52:
|
||||
return "ten"
|
||||
if 13 <= n <= 17 or 43 <= n <= 47:
|
||||
return "quarter"
|
||||
if 18 <= n <= 22 or 38 <= n <= 42:
|
||||
return "twenty"
|
||||
if 23 <= n <= 27 or 33 <= n <= 37:
|
||||
return "twenty-five"
|
||||
if 28 <= n <= 32:
|
||||
return "half"
|
||||
# Iterate through minute dictionary to find the next entry which contains the present minute, otherwise return None
|
||||
minute_phrase = next((phrase for start, end, phrase in minutes if start <= minute <= end), None)
|
||||
|
||||
def cat(h,m):
|
||||
# Put it all together
|
||||
# HALF
|
||||
if 28 <= m <= 32:
|
||||
return return_minute(m) + " " + to_or_past(m) + " " + return_hour(h)
|
||||
# Put it all together and return the phrase we want
|
||||
if minute_phrase:
|
||||
if minute in {58, 59}:
|
||||
return f"{hours[(hour +1)] % 12} o'clock"
|
||||
if minute in {0, 1, 2}:
|
||||
return f"{hours[hour] % 12} o'clock"
|
||||
if 3 <= minute <= 32: # Past
|
||||
return f"{minute_phrase} past {hours[hour % 12]}"
|
||||
if 33 <= minute <= 57: # To
|
||||
return f"{minute_phrase} to {hours[(hour + 1) % 12]}"
|
||||
|
||||
# PAST
|
||||
if 3 <= m <= 27:
|
||||
return return_minute(m) + " " + to_or_past(m) + " " + return_hour(h)
|
||||
# Get current time
|
||||
|
||||
# TO
|
||||
if 33 <= m <= 57:
|
||||
return return_minute(m) + " " + to_or_past(m) + " " + return_hour(h+1)
|
||||
now = datetime.now()
|
||||
hour, minute = now.hour, now.minute
|
||||
|
||||
# OCLOCK
|
||||
if m in [58, 59]:
|
||||
return return_hour(h+1) + " o'clock"
|
||||
|
||||
if m in [0, 1, 2]:
|
||||
return return_hour(h) + " o'clock"
|
||||
|
||||
|
||||
h = time.gmtime()[3]
|
||||
m = time.gmtime()[4]
|
||||
|
||||
print(cat(h,m))
|
||||
|
||||
# Is it o'clock?
|
||||
# Is it 12?
|
||||
# Is it after or before?
|
||||
# What is it after or before?
|
||||
# https://github.com/SarthakU/fuzzy-clock/blob/master/fuzzy.py <- is a good resouce to take inspiration from
|
||||
# Output the fuzzy time
|
||||
print(get_fuzzy_time(hour, minute))
|
||||
|
||||
Reference in New Issue
Block a user