Skip to content

Instantly share code, notes, and snippets.

@mtimkovich
Last active January 17, 2017 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtimkovich/d38d480bcfc510338255ff0da8188844 to your computer and use it in GitHub Desktop.
Save mtimkovich/d38d480bcfc510338255ff0da8188844 to your computer and use it in GitHub Desktop.
fuzzy clock
#!/usr/bin/env python3
from datetime import datetime
word = {
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
10: 'ten',
11: 'eleven',
12: 'twelve',
15: 'quarter',
20: 'twenty',
30: 'half'
}
increments = {
0: 0,
3: 5,
8: 10,
13: 15,
18: 20,
25: 30,
35: 40,
43: 45,
48: 50,
53: 55,
58: 0
}
def hour_word(h):
if h > 12:
h -= 12
return word[h]
def fuzzy(hours, minutes):
rounded = [r for n, r in sorted(increments.items()) if minutes >= n][-1]
if rounded == 0:
if minutes > 5:
hours += 1
return hour_word(hours) + " o'clock"
elif rounded > 30:
rounded = 60 - rounded
glue = 'til'
hours += 1
elif rounded <= 30:
glue = 'past'
return '{} {} {}'.format(word[rounded], glue, hour_word(hours))
now = datetime.now()
hours = now.hour
minutes = now.minute
print(fuzzy(hours, minutes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment