Revision 45bc5e4a

b/lib/utils.py
1168 1168
  return None
1169 1169

  
1170 1170

  
1171
def SplitTime(seconds):
1171
def SplitTime(value):
1172 1172
  """Splits time as floating point number into a tuple.
1173 1173

  
1174
  @param seconds: Time in seconds
1175
  @type seconds: int or float
1176
  @return: Tuple containing (seconds, milliseconds)
1174
  @param value: Time in seconds
1175
  @type value: int or float
1176
  @return: Tuple containing (seconds, microseconds)
1177 1177

  
1178 1178
  """
1179
  seconds = round(seconds, 3)
1180
  (seconds, fraction) = divmod(seconds, 1.0)
1181
  return (int(seconds), int(round(fraction * 1000, 0)))
1179
  (seconds, microseconds) = divmod(int(value * 1000000), 1000000)
1180

  
1181
  assert 0 <= seconds, \
1182
    "Seconds must be larger than or equal to 0, but are %s" % seconds
1183
  assert 0 <= microseconds <= 999999, \
1184
    "Microseconds must be 0-999999, but are %s" % microseconds
1185

  
1186
  return (int(seconds), int(microseconds))
1182 1187

  
1183 1188

  
1184 1189
def MergeTime(timetuple):
1185 1190
  """Merges a tuple into time as a floating point number.
1186 1191

  
1187
  @param timetuple: Time as tuple, (seconds, milliseconds)
1192
  @param timetuple: Time as tuple, (seconds, microseconds)
1188 1193
  @type timetuple: tuple
1189 1194
  @return: Time as a floating point number expressed in seconds
1190 1195

  
1191 1196
  """
1192
  (seconds, milliseconds) = timetuple
1197
  (seconds, microseconds) = timetuple
1193 1198

  
1194
  assert 0 <= seconds, "Seconds must be larger than 0"
1195
  assert 0 <= milliseconds <= 999, "Milliseconds must be 0-999"
1199
  assert 0 <= seconds, \
1200
    "Seconds must be larger than or equal to 0, but are %s" % seconds
1201
  assert 0 <= microseconds <= 999999, \
1202
    "Microseconds must be 0-999999, but are %s" % microseconds
1196 1203

  
1197
  return float(seconds) + (float(1) / 1000 * milliseconds)
1204
  return float(seconds) + (float(microseconds) * 0.000001)
1198 1205

  
1199 1206

  
1200 1207
def LockedMethod(fn):
b/test/ganeti.utils_unittest.py
783 783

  
784 784
  def runTest(self):
785 785
    self.assertEqual(utils.SplitTime(1), (1, 0))
786
    self.assertEqual(utils.SplitTime(1.5), (1, 500))
787
    self.assertEqual(utils.SplitTime(1218448917.4809151), (1218448917, 481))
788
    self.assertEqual(utils.SplitTime(123.48012), (123, 480))
789
    self.assertEqual(utils.SplitTime(123.9995), (124, 0))
790
    self.assertEqual(utils.SplitTime(123.999999999), (124, 0))
786
    self.assertEqual(utils.SplitTime(1.5), (1, 500000))
787
    self.assertEqual(utils.SplitTime(1218448917.4809151), (1218448917, 480915))
788
    self.assertEqual(utils.SplitTime(123.48012), (123, 480120))
789
    self.assertEqual(utils.SplitTime(123.9996), (123, 999600))
790
    self.assertEqual(utils.SplitTime(123.9995), (123, 999500))
791
    self.assertEqual(utils.SplitTime(123.9994), (123, 999400))
792
    self.assertEqual(utils.SplitTime(123.999999999), (123, 999999))
793

  
794
    self.assertRaises(AssertionError, utils.SplitTime, -1)
791 795

  
792 796
    self.assertEqual(utils.MergeTime((1, 0)), 1.0)
793
    self.assertEqual(utils.MergeTime((1, 500)), 1.5)
794
    self.assertEqual(utils.MergeTime((1218448917, 500)), 1218448917.5)
797
    self.assertEqual(utils.MergeTime((1, 500000)), 1.5)
798
    self.assertEqual(utils.MergeTime((1218448917, 500000)), 1218448917.5)
795 799

  
796
    self.assertEqual(round(utils.MergeTime((1218448917, 481)), 3), 1218448917.481)
797
    self.assertEqual(round(utils.MergeTime((1, 801)), 3), 1.801)
800
    self.assertEqual(round(utils.MergeTime((1218448917, 481000)), 3), 1218448917.481)
801
    self.assertEqual(round(utils.MergeTime((1, 801000)), 3), 1.801)
798 802

  
799 803
    self.assertRaises(AssertionError, utils.MergeTime, (0, -1))
800
    self.assertRaises(AssertionError, utils.MergeTime, (0, 1000))
801
    self.assertRaises(AssertionError, utils.MergeTime, (0, 9999))
804
    self.assertRaises(AssertionError, utils.MergeTime, (0, 1000000))
805
    self.assertRaises(AssertionError, utils.MergeTime, (0, 9999999))
802 806
    self.assertRaises(AssertionError, utils.MergeTime, (-1, 0))
803 807
    self.assertRaises(AssertionError, utils.MergeTime, (-9999, 0))
804 808

  

Also available in: Unified diff