This commit is contained in:
Philip Wellnitz [10] 2024-05-12 04:45:27 +09:00
parent a34abe325a
commit 6b36ba8ae9
2 changed files with 19 additions and 21 deletions

View File

@ -1,7 +1,5 @@
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
from datetime import datetime
from datetime import datetime, timezone
def date(pl, format='%Y-%m-%d', istime=False, timezone=None):
@ -45,11 +43,11 @@ UNICODE_TEXT_TRANSLATION = {
}
def fuzzy_time(pl, format='{minute_str} {hour_str}', unicode_text=False, timezone=None, hour_str=['twelve', 'one', 'two', 'three', 'four',
def fuzzy_time(pl, format=None, unicode_text=False, timezone=None, hour_str=['twelve', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven'], minute_str = {
'0': 'o\'clock', '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'
'0': '{hour_str} o\'clock', '5': 'five past {hour_str}', '10': 'ten past {hour_str}','15': 'quarter past {hour_str}',
'20': 'twenty past {hour_str}', '25': 'twenty-five past {hour_str}', '30': 'half past {hour_str}', '35': 'twenty-five to {hour_str}',
'40': 'twenty to {hour_str}', '45': 'quarter to {hour_str}', '50': 'ten to {hour_str}', '55': 'five to {hour_str}'
}, special_case_str = {
'(23, 58)': 'round about midnight',
'(23, 59)': 'round about midnight',
@ -62,8 +60,7 @@ def fuzzy_time(pl, format='{minute_str} {hour_str}', unicode_text=False, timezon
'''Display the current time as fuzzy time, e.g. "quarter past six".
:param string format:
Format used to display the fuzzy time. (Ignored when a special time
is displayed.)
(unused)
:param bool unicode_text:
If true then hyphenminuses (regular ASCII ``-``) and single quotes are
replaced with unicode dashes and apostrophes.
@ -74,7 +71,9 @@ def fuzzy_time(pl, format='{minute_str} {hour_str}', unicode_text=False, timezon
Strings to be used to display the hour, starting with midnight.
(This list may contain 12 or 24 entries.)
:param dict minute_str:
Dictionary mapping minutes to strings to be used to display them.
Dictionary mapping minutes to strings to be used to display them. Each entry may
optionally come with a format field "{hour_str}" to indicate the position of the
string used for the current hour.
:param dict special_case_str:
Special strings for special times.
@ -100,7 +99,7 @@ def fuzzy_time(pl, format='{minute_str} {hour_str}', unicode_text=False, timezon
pass
hour = now.hour
if now.minute >= 30:
if now.minute >= 32:
hour = hour + 1
hour = hour % len(hour_str)
@ -115,7 +114,7 @@ def fuzzy_time(pl, format='{minute_str} {hour_str}', unicode_text=False, timezon
elif now.minute < mn and mn - now.minute < min_dis:
min_dis = mn - now.minute
min_pos = mn
result = format.format(minute_str=minute_str[str(min_pos)], hour_str=hour_str[hour])
result = minute_str[str(min_pos)].format(hour_str=hour_str[hour])
if unicode_text:
result = result.translate(UNICODE_TEXT_TRANSLATION)

View File

@ -843,17 +843,16 @@ class TestTime(TestCommon):
time = Args(hour=0, minute=45)
pl = Pl()
hour_str = ['12', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven']
minute_str = {'0': 'o\'clock', '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'}
minute_str = {'0': '{hour_str} o\'clock', '5': 'five past {hour_str}', '10': 'ten past {hour_str}',
'15': 'quarter past {hour_str}', '20': 'twenty past {hour_str}', '25': 'twenty-five past {hour_str}',
'30': 'half past {hour_str}', '35': 'twenty-five to {hour_str}', '40': 'twenty to {hour_str}',
'45': 'quarter to {hour_str}', '50': 'ten to {hour_str}', '55': 'five to {hour_str}'}
special_case_str = {
'(23, 58)': '~ midnight',
'(23, 59)': '~ midnight',
'(0, 0)': 'midnight',
'(0, 1)': '~ midnight',
'(0, 2)': '~ midnight',
'(12, 0)': 'twelve o\'clock'}
'(0, 2)': '~ midnight'}
with replace_attr(self.module, 'datetime', Args(strptime=lambda timezone, fmt: Args(tzinfo=timezone), now=lambda tz: time)):
self.assertEqual(self.module.fuzzy_time(pl=pl, hour_str=hour_str, minute_str=minute_str, special_case_str=special_case_str), 'quarter to one')
self.assertEqual(self.module.fuzzy_time(pl=pl), 'quarter to one')
@ -867,7 +866,7 @@ class TestTime(TestCommon):
self.assertEqual(self.module.fuzzy_time(pl=pl), 'twenty-five to twelve')
time.hour = 12
time.minute = 0
self.assertEqual(self.module.fuzzy_time(pl=pl, hour_str=hour_str, minute_str=minute_str, special_case_str=special_case_str), 'twelve o\'clock')
self.assertEqual(self.module.fuzzy_time(pl=pl, hour_str=hour_str, minute_str=minute_str, special_case_str=special_case_str), '12 o\'clock')
self.assertEqual(self.module.fuzzy_time(pl=pl), 'noon')
time.hour = 11
time.minute = 33
@ -875,7 +874,7 @@ class TestTime(TestCommon):
self.assertEqual(self.module.fuzzy_time(pl=pl, unicode_text=False), 'twenty-five to twelve')
time.hour = 12
time.minute = 0
self.assertEqual(self.module.fuzzy_time(pl=pl, unicode_text=False, hour_str=hour_str, minute_str=minute_str, special_case_str=special_case_str), 'twelve o\'clock')
self.assertEqual(self.module.fuzzy_time(pl=pl, unicode_text=False, hour_str=hour_str, minute_str=minute_str, special_case_str=special_case_str), '12 o\'clock')
self.assertEqual(self.module.fuzzy_time(pl=pl, unicode_text=False), 'noon')
time.hour = 11
time.minute = 33
@ -883,7 +882,7 @@ class TestTime(TestCommon):
self.assertEqual(self.module.fuzzy_time(pl=pl, unicode_text=True), 'twentyfive to twelve')
time.hour = 12
time.minute = 0
self.assertEqual(self.module.fuzzy_time(pl=pl, unicode_text=True, hour_str=hour_str, minute_str=minute_str, special_case_str=special_case_str), 'twelve oclock')
self.assertEqual(self.module.fuzzy_time(pl=pl, unicode_text=True, hour_str=hour_str, minute_str=minute_str, special_case_str=special_case_str), '12 oclock')
self.assertEqual(self.module.fuzzy_time(pl=pl, unicode_text=True),'noon')