From 18a09e341c291b0a27c86916e7990efef49e7f91 Mon Sep 17 00:00:00 2001 From: Griatch Date: Thu, 29 Aug 2019 00:10:46 +0200 Subject: [PATCH] Fix time format Windows does not support. Resolve #1881. Resolve #1880 --- evennia/contrib/mail.py | 10 ++++++---- evennia/utils/tests/test_utils.py | 17 +++++++++++++++++ evennia/utils/utils.py | 4 ++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/evennia/contrib/mail.py b/evennia/contrib/mail.py index 9c458b8419..7e9b8fafb5 100644 --- a/evennia/contrib/mail.py +++ b/evennia/contrib/mail.py @@ -87,7 +87,7 @@ class CmdMail(default_cmds.MuxAccountCommand): def parse(self): """ - Add convenience check to know if caller is an Account or not since this cmd + Add convenience check to know if caller is an Account or not since this cmd will be able to add to either Object- or Account level. """ @@ -276,7 +276,9 @@ class CmdMail(default_cmds.MuxAccountCommand): if message: messageForm.append(_HEAD_CHAR * _WIDTH) messageForm.append("|wFrom:|n %s" % (message.senders[0].get_display_name(self.caller))) - messageForm.append("|wSent:|n %s" % message.db_date_created.strftime("%b %-d, %Y - %H:%M:%S")) + # note that we cannot use %-d format here since Windows does not support it + day = message.db_date_created.day + messageForm.append("|wSent:|n %s" % message.db_date_created.strftime(f"%b {day}, %Y - %H:%M:%S")) messageForm.append("|wSubject:|n %s" % message.header) messageForm.append(_SUB_HEAD_CHAR * _WIDTH) messageForm.append(message.message) @@ -286,7 +288,7 @@ class CmdMail(default_cmds.MuxAccountCommand): message.tags.add("-", category="mail") else: - # list messages + # list messages messages = self.get_all_mail() if messages: @@ -300,7 +302,7 @@ class CmdMail(default_cmds.MuxAccountCommand): if status == "NEW": status = "|gNEW|n" - table.add_row(index, message.senders[0].get_display_name(self.caller), + table.add_row(index, message.senders[0].get_display_name(self.caller), message.header, datetime_format(message.db_date_created), status) diff --git a/evennia/utils/tests/test_utils.py b/evennia/utils/tests/test_utils.py index d2e42c4169..36b32318b3 100644 --- a/evennia/utils/tests/test_utils.py +++ b/evennia/utils/tests/test_utils.py @@ -5,7 +5,9 @@ TODO: Not nearly all utilities are covered yet. """ +import mock from django.test import TestCase +from datetime import datetime from evennia.utils.ansi import ANSIString from evennia.utils import utils @@ -186,3 +188,18 @@ class TestTimeformat(TestCase): """Test that unknown formats raise exceptions.""" self.assertRaises(ValueError, utils.time_format, 0, 5) self.assertRaises(ValueError, utils.time_format, 0, "u") + + +@mock.patch("evennia.utils.utils.timezone.now", + new=mock.MagicMock(return_value=datetime(2019, 8, 28, 21, 56))) +class TestDateTimeFormat(TestCase): + + def test_datetimes(self): + dtobj = datetime(2017, 7, 26, 22, 54) + self.assertEqual(utils.datetime_format(dtobj), "Jul 26, 2017") + dtobj = datetime(2019, 7, 26, 22, 54) + self.assertEqual(utils.datetime_format(dtobj), "Jul 26") + dtobj = datetime(2019, 8, 28, 19, 54) + self.assertEqual(utils.datetime_format(dtobj), "19:54") + dtobj = datetime(2019, 8, 28, 21, 32) + self.assertEqual(utils.datetime_format(dtobj), "21:32:00") diff --git a/evennia/utils/utils.py b/evennia/utils/utils.py index 6f0685a3ab..6c45bd8d18 100644 --- a/evennia/utils/utils.py +++ b/evennia/utils/utils.py @@ -577,10 +577,10 @@ def datetime_format(dtobj): if dtobj.year < now.year: # another year (Apr 5, 2019) - timestring = dtobj.strftime("%b %-d, %Y") + timestring = dtobj.strftime(f"%b {dtobj.day}, %Y") elif dtobj.date() < now.date(): # another date, same year (Apr 5) - timestring = dtobj.strftime("%b %-d") + timestring = dtobj.strftime(f"%b {dtobj.day}") elif dtobj.hour < now.hour - 1: # same day, more than 1 hour ago (10:45) timestring = dtobj.strftime("%H:%M")