Add fuzzy_time(unicode_text) argument

This commit is contained in:
ZyX 2014-03-13 20:54:07 +04:00
parent 3cf3fb8f8e
commit e51d7a9dd3
2 changed files with 26 additions and 6 deletions

View File

@ -158,8 +158,19 @@ def date(pl, format='%Y-%m-%d', istime=False):
}] }]
def fuzzy_time(pl): UNICODE_TEXT_TRANSLATION = {
'''Display the current time as fuzzy time, e.g. "quarter past six".''' ord('\''): '',
ord('-'): '',
}
def fuzzy_time(pl, unicode_text=True):
'''Display the current time as fuzzy time, e.g. "quarter past six".
:param bool unicode_text:
If true then hyphenminuses (regular ASCII ``-``) and single quotes are
replaced with unicode dashes and apostrophes.
'''
hour_str = ['twelve', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven'] hour_str = ['twelve', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven']
minute_str = { minute_str = {
5: 'five past', 5: 'five past',
@ -202,10 +213,15 @@ def fuzzy_time(pl):
minute = int(round(now.minute / 5.0) * 5) minute = int(round(now.minute / 5.0) * 5)
if minute == 60 or minute == 0: if minute == 60 or minute == 0:
return ' '.join([hour, 'o\'clock']) result = ' '.join([hour, 'o\'clock'])
else: else:
minute = minute_str[minute] minute = minute_str[minute]
return ' '.join([minute, hour]) result = ' '.join([minute, hour])
if unicode_text:
result = result.translate(UNICODE_TEXT_TRANSLATION)
return result
def _external_ip(query_url='http://ipv4.icanhazip.com/'): def _external_ip(query_url='http://ipv4.icanhazip.com/'):

View File

@ -326,9 +326,13 @@ class TestCommon(TestCase):
time.minute = 59 time.minute = 59
self.assertEqual(common.fuzzy_time(pl=pl), 'round about midnight') self.assertEqual(common.fuzzy_time(pl=pl), 'round about midnight')
time.minute = 33 time.minute = 33
self.assertEqual(common.fuzzy_time(pl=pl), 'twenty-five to twelve') self.assertEqual(common.fuzzy_time(pl=pl), 'twentyfive to twelve')
time.minute = 60 time.minute = 60
self.assertEqual(common.fuzzy_time(pl=pl), 'twelve o\'clock') self.assertEqual(common.fuzzy_time(pl=pl), 'twelve oclock')
time.minute = 33
self.assertEqual(common.fuzzy_time(pl=pl, unicode_text=False), 'twenty-five to twelve')
time.minute = 60
self.assertEqual(common.fuzzy_time(pl=pl, unicode_text=False), 'twelve o\'clock')
def test_external_ip(self): def test_external_ip(self):
pl = Pl() pl = Pl()