Commit 01882078 by Alexander Kryklia Committed by polesye

Fix to_json of RelativeTime to support Isotime format.

parent 040b4da1
......@@ -196,7 +196,7 @@ class RelativeTime(Field):
if isinstance(value, float): # backward compatibility
if value > 86400:
value = 86400
return str(datetime.timedelta(seconds=value))
return self._str(datetime.timedelta(seconds=value))
if isinstance(value, datetime.timedelta):
if value.total_seconds() > 86400: # sanity check
......@@ -204,18 +204,18 @@ class RelativeTime(Field):
"RelativeTime max value is 23:59:59=86400.0 seconds, "
"but {} seconds is passed".format(value.total_seconds())
)
return str(value)
return self._str(value)
raise TypeError("RelativeTime: cannot convert {!r} to json".format(value))
def __str__(self):
def _str(self, value):
"""
Makes first H in str representation non-optional.
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
for front-end (and IsoTime standart), so we forse HH:MM:SS format.
"""
_str = super(RelativeTime, slef).__str__(self)
if len(_str) == 7:
_str = '0' + _str
return _str
string = str(value)
if len(string) == 7:
string = '0' + string
return string
......@@ -145,7 +145,7 @@ class RelativeTimeTest(unittest.TestCase):
def test_to_json(self):
self.assertEqual(
"1:02:03",
"01:02:03",
RelativeTimeTest.delta.to_json(datetime.timedelta(seconds=3723))
)
self.assertEqual(
......@@ -153,7 +153,7 @@ class RelativeTimeTest(unittest.TestCase):
RelativeTimeTest.delta.to_json(None)
)
self.assertEqual(
"0:01:40",
"00:01:40",
RelativeTimeTest.delta.to_json(100.0)
)
......@@ -170,7 +170,7 @@ class RelativeTimeTest(unittest.TestCase):
def test_str(self):
self.assertEqual(
"1:02:03",
"01:02:03",
RelativeTimeTest.delta.to_json(datetime.timedelta(seconds=3723))
)
self.assertEqual(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment