Fix time format Windows does not support. Resolve #1881. Resolve #1880

This commit is contained in:
Griatch 2019-08-29 00:10:46 +02:00
parent 71840a14d3
commit 18a09e341c
3 changed files with 25 additions and 6 deletions

View file

@ -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)

View file

@ -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")

View file

@ -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")