James McDonald

Vag klokke

I localised my awesome fuzzyclock.py script to Norwegian language and, uh, regional time-reading standards. Behold the glory of vagklokke.py! A little terminal in the top right of my screen now proudly proclaims:

<br /> Datoen er 2012-11-13<br /> Klokka er fem på elleve<br />

It was fun to work out how to handle “x på/over halv” without too many horrible range conditions. I haven’t bothered to remove stale things like ‘tjuefem’ from the minute list, because it ain’t broke. Now you, too, can have the power of a clock that isn’t very accurate. Now with added date!

#!/usr/bin/env python
# vim: set fileencoding=utf8 :
# Norwegian port of fuzzyclock
from datetime import datetime, time
from time import sleep
import sys

hours = [

minutes = [

def fuzzyclock(time=None):
    if time is None:
        time = datetime.now()
    hour = time.hour
    minute = int(round(time.minute/5.0))
    if (minute >= 4 and minute < 6) or minute >= 9:

    if minute >= 4:
        hour += 1
        if minute < 9:
            past = '%s halv' % (past)

    # Adjust the minute to be in the first half of the hour
    if minute > 6:
        minute = 12 - minute

    # Handle på/over halv in the same way
    if minute > 3 and minute < 6:
        minute = 6-minute

    minute = minutes[minute]
    hour = hours[hour%12]

    if minute=='null':
        return "%s" % (hour)
    if minute=='halv':
        return "halv %s" % (hour)
    return "%s %s %s" % (minute, past, hour)

if __name__=='__main__':
    if len(sys.argv) > 1 and sys.argv[1] == '-t':
        for x in xrange(0,60,5):
            print "Klokka er %s" % fuzzyclock(datetime(2012,01,01,9,x,0))
        # By default, print the time every 10 seconds
        while True:
            print "Datoen er %s" % datetime.now().date()
            print "Klokka er %-23s" % (fuzzyclock()),
            # Go up a line and CR, if the terminal lets you
            print "\x1b[1F\r",


comments powered by Disqus