Skip to content

Instantly share code, notes, and snippets.

@alexsleat
Created November 13, 2011 14:25
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 alexsleat/1362167 to your computer and use it in GitHub Desktop.
Save alexsleat/1362167 to your computer and use it in GitHub Desktop.
Python Fuzzy Clock, more info at alexsleat.co.uk
#!/usr/bin/python
from time import gmtime, strftime
#Get minutes
time_min = strftime("%M", gmtime())
#Get hour, as int
time_hour = int(strftime("%H", gmtime()))
#round minutes to nearest 5, return as int
time_min = int(round(float(time_min)*2,-1)/2)
#Make it 12 hour, rather than 24
if time_hour >= 13:
time_hour = time_hour - 12
#Get the O'clock for if it's around 0mins past
if time_min == 0:
print "It's " + str(time_hour) + " o'clock"
elif time_min == 60:
print "It's " + str(time_hour+1) + " o'clock"
#If it's less than half past, it'll be past
elif time_min <= 30:
print "It's " + str(time_min) + " past " + str(time_hour)
#otherwise it'll be to, you need to invert the minutes and add on an hour
# so 35 past 12 becomes: 25 to 1
else:
print "It's " + str(60-time_min) + " to " + str(time_hour+1)
#!/usr/bin/python
from time import gmtime, strftime
import bisect
minLookup=[
' ',
'five',
'ten',
'quater',
'twenty',
'twenty-five'
]
hourLookup=[
'midnight',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'noon'
]
#Get minutes
time_min = strftime("%M", gmtime())
#Get hour, as int
time_hour = int(strftime("%H", gmtime()))
#round minutes to nearest 5, return as int
time_min = int(round(float(time_min)*2,-1)/2)
#Make it 12 hour, rather than 24
if time_hour >= 13:
time_hour = time_hour - 12
#Get the O'clock for if it's around 0mins past
if time_min == 0:
print "It's " + hourLookup[time_hour] + " o'clock"
elif time_min == 60:
print "It's " + hourLookup[time_hour+1] + " o'clock"
#If it's less than half past, it'll be past
elif time_min <= 30:
print "It's " + minLookup[time_min/5] + " past " + hourLookup[time_hour]
#otherwise it'll be to, you need to invert the minutes and add on an hour
# so 35 past 12 becomes: 25 to 1
else:
print "It's " + minLookup[(60-time_min)/5] + " to " + hourLookup[time_hour+1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment