From cc83d741ff4b28b4b9fcb60d7e8e555ad66fa2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Mon, 11 Feb 2013 13:08:27 +0100 Subject: [PATCH] Create fuzzy_time segment --- powerline/segments/common.py | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/powerline/segments/common.py b/powerline/segments/common.py index 780d8264..2d8127f0 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -87,6 +87,58 @@ def date(format='%Y-%m-%d'): return datetime.now().strftime(format) +def fuzzy_time(): + '''Display the current time as fuzzy time, e.g. "quarter past six".''' + from datetime import datetime + + hour_str = ['twelve', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven'] + minute_str = { + 5: 'five past', + 10: 'ten past', + 15: 'quarter past', + 20: 'twenty past', + 25: 'twenty-five past', + 30: 'half past', + 35: 'twenty-five to', + 40: 'twenty to', + 45: 'quarter to', + 50: 'ten to', + 55: 'five to', + } + special_case_str = { + (23, 58): 'round about midnight', + (23, 59): 'round about midnight', + (0, 0): 'midnight', + (0, 1): 'round about midnight', + (0, 2): 'round about midnight', + (12, 0): 'noon', + } + + now = datetime.now() + + try: + return special_case_str[(now.hour, now.minute)] + except KeyError: + pass + + hour = now.hour + if now.minute > 32: + if hour == 23: + hour = 0 + else: + hour += 1 + if hour > 11: + hour = hour - 12 + hour = hour_str[hour] + + minute = int(round(now.minute / 5.0) * 5) + if minute == 60 or minute == 0: + return ' '.join([hour, 'o\'clock']) + else: + minute = minute_str[minute] + return ' '.join([minute, hour]) + + @memoize(600) def external_ip(query_url='http://ipv4.icanhazip.com/'): '''Return external IP address.