Change str of RelativeTime.
This commit is contained in:
committed by
polesye
parent
14dc814e2a
commit
040b4da124
@@ -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
|
||||
|
||||
@@ -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))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user