Skip to content

Commit

Permalink
Fix for correctly working with microseconds.
Browse files Browse the repository at this point in the history
  • Loading branch information
troygrosfield committed Feb 19, 2014
1 parent 2c7bdd7 commit 1d555d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion durationfield/utils/timestring.py
Expand Up @@ -46,7 +46,15 @@ def str_to_timedelta(td_str):
for key in time_groups.keys(): for key in time_groups.keys():
if time_groups[key] is not None: if time_groups[key] is not None:
is_valid = True is_valid = True
time_groups[key] = int(time_groups[key])
if key == 'microseconds':
val = time_groups[key]
leading_zeros = len(val) - len(val.lstrip('0'))
time_groups[key] = int(val.lstrip('0')
.ljust(6 - leading_zeros, '0'))
else:
time_groups[key] = int(time_groups[key])

else: else:
time_groups[key] = 0 time_groups[key] = 0


Expand Down
12 changes: 12 additions & 0 deletions tests/tests.py
Expand Up @@ -144,6 +144,18 @@ def testInputTimeSecondsMicroseconds(self):
self.assertEqual(seconds, delta.seconds) self.assertEqual(seconds, delta.seconds)
self.assertEqual(98, delta.microseconds) self.assertEqual(98, delta.microseconds)


def testInputTimeMicrosecondsRightPadZeros(self):
delta = timestring.str_to_timedelta("11:20:22.160")
self.assertEqual(160000, delta.microseconds)

def testInputTimeMicrosecondsLeftPadZeros(self):
delta = timestring.str_to_timedelta("11:20:22.016")
self.assertEqual(16000, delta.microseconds)

def testInputTimeMicrosecondsBothPadZeros(self):
delta = timestring.str_to_timedelta("11:20:22.0160")
self.assertEqual(16000, delta.microseconds)

def testInputAll(self): def testInputAll(self):
delta = timestring.str_to_timedelta("1 year, 10 months, 3 weeks, 2 days, 3:40:50") delta = timestring.str_to_timedelta("1 year, 10 months, 3 weeks, 2 days, 3:40:50")
days = ( days = (
Expand Down

0 comments on commit 1d555d5

Please sign in to comment.