Permalink
Browse files

Fix for correctly working with microseconds.

  • Loading branch information...
1 parent 2c7bdd7 commit 1d555d51110e558fe9749d41ca5cd0af69dd2107 @troygrosfield troygrosfield committed Feb 19, 2014
Showing with 21 additions and 1 deletion.
  1. +9 −1 durationfield/utils/timestring.py
  2. +12 −0 tests/tests.py
@@ -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])
+
+ 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:
time_groups[key] = 0
View
@@ -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 = (

0 comments on commit 1d555d5

Please sign in to comment.