Files
blog/content/posts/2012-11-13-vag-klokke.md
T
James McDonald 96eedb5095 Import
2018-01-09 20:39:32 +01:00

100 lines
2.6 KiB
Markdown

---
title: Vag klokke
author: james
type: post
date: 2012-11-13T22:02:16+00:00
url: /2012/11/vag-klokke/
dsq_thread_id:
- 926740377
categories:
- Hacks
- Norsk
---
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 &#8220;x på/over halv&#8221; without too many horrible range conditions. I haven&#8217;t bothered to remove stale things like &#8216;tjuefem&#8217; from the minute list, because it ain&#8217;t broke. Now you, too, can have the power of a clock that isn&#8217;t very accurate. Now with added date!
<pre class="font:droid-sans-mono lang:python decode:true " title="vagklokke.py" >#!/usr/bin/env python
# vim: set fileencoding=utf8 :
# Norwegian port of fuzzyclock
from datetime import datetime, time
from time import sleep
import sys
hours = [
'tolv',
'ett',
'to',
'tre',
'fire',
'fem',
'seks',
'sju',
'åtte',
'ni',
'ti',
'elleve',
]
minutes = [
'null',
'fem',
'ti',
'kvart',
'tjue',
'tjuefem',
'halv'
]
def fuzzyclock(time=None):
if time is None:
time = datetime.now()
hour = time.hour
minute = int(round(time.minute/5.0))
if (minute &gt;= 4 and minute &lt; 6) or minute &gt;= 9:
past='på'
else:
past='over'
if minute &gt;= 4:
hour += 1
if minute &lt; 9:
past = '%s halv' % (past)
# Adjust the minute to be in the first half of the hour
if minute &gt; 6:
minute = 12 - minute
# Handle på/over halv in the same way
if minute &gt; 3 and minute &lt; 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) &gt; 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))
else:
# By default, print the time every 10 seconds
while True:
print "Datoen er %s" % datetime.now().date()
print "Klokka er %-23s" % (fuzzyclock()),
sys.stdout.flush()
sleep(10)
# Go up a line and CR, if the terminal lets you
print "\x1b[1F\r",
</pre>