Skip to content

Commit

Permalink
Merge remote-tracking branch 'troygrosfield/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpaulett committed Feb 25, 2014
2 parents 2c7bdd7 + c95278d commit 4674bd5
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():
if time_groups[key] is not None:
is_valid = True
time_groups[key] = int(time_groups[key])
value = time_groups[key]
if key == 'microseconds':
# When parsing time regex, make sure the microseconds value
# uses the correct number of digits. This must be correctly
# padded so 3.14 == 3.140 == 3.1400 == 3.14000 == 3.140000
# 3.14 == 3 seconds 140,000 microseconds
value = value.ljust(6, '0')
time_groups[key] = int(value)

else:
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(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):
delta = timestring.str_to_timedelta("1 year, 10 months, 3 weeks, 2 days, 3:40:50")
days = (
Expand Down

0 comments on commit 4674bd5

Please sign in to comment.