Change str of RelativeTime.

This commit is contained in:
Alexander Kryklia
2013-10-22 15:43:21 +03:00
committed by polesye
parent 14dc814e2a
commit 040b4da124
2 changed files with 22 additions and 0 deletions

View File

@@ -207,3 +207,15 @@ class RelativeTime(Field):
return str(value)
raise TypeError("RelativeTime: cannot convert {!r} to json".format(value))
def __str__(self):
"""
Makes first H in str representation non-optional.
str(timedelta) has [H]H:MM:SS format, which is not suitable
for front-end (and IsoTime standart), so we forse HH:MM:SS
"""
_str = super(RelativeTime, slef).__str__(self)
if len(_str) == 7:
_str = '0' + _str
return _str

View File

@@ -167,3 +167,13 @@ class RelativeTimeTest(unittest.TestCase):
with self.assertRaises(TypeError):
RelativeTimeTest.delta.to_json("123")
def test_str(self):
self.assertEqual(
"1:02:03",
RelativeTimeTest.delta.to_json(datetime.timedelta(seconds=3723))
)
self.assertEqual(
"11:02:03",
RelativeTimeTest.delta.to_json(datetime.timedelta(seconds=39723))
)