def utc_time(self): libc = CDLL(self.__libc) ts = self._TimeSpec() status = libc.clock_gettime(0, byref(ts)) if status == 0: return ClockTime(ts.seconds, ts.nanoseconds) else: raise RuntimeError("clock_gettime failed with status %d" % status)
def test_repr(self): ct = ClockTime(123456.789) self.assertTrue(repr(ct).startswith("ClockTime"))
def test_sub_object(self): with self.assertRaises(TypeError): _ = ClockTime(123456.789) - object()
def test_sub_duration_with_months(self): with self.assertRaises(ValueError): _ = ClockTime(123456.789) - Duration(months=1)
def test_sub_duration(self): ct = ClockTime(123456.789) - Duration(seconds=1) self.assertEqual(ct.seconds, 123455) self.assertEqual(ct.nanoseconds, 789000000)
def test_sub_float(self): ct = ClockTime(123456.789) - 0.1 self.assertEqual(ct.seconds, 123456) self.assertEqual(ct.nanoseconds, 689000000)
def test_add_float(self): ct = ClockTime(123456.789) + 0.1 self.assertEqual(ct.seconds, 123456) self.assertEqual(ct.nanoseconds, 889000000)
def test_negative_nanoseconds(self): ct = ClockTime(1, -1) self.assertEqual(ct.seconds, 0) self.assertEqual(ct.nanoseconds, 999999999)
def test_positive_nanoseconds(self): ct = ClockTime(1, 1) self.assertEqual(ct.seconds, 1) self.assertEqual(ct.nanoseconds, 1)
def test_nanoseconds_overflow(self): ct = ClockTime(0, 2123456789) self.assertEqual(ct.seconds, 2) self.assertEqual(ct.nanoseconds, 123456789)
def test_only_nanoseconds(self): ct = ClockTime(0, 123456789) self.assertEqual(ct.seconds, 0) self.assertEqual(ct.nanoseconds, 123456789)
def test_zero_(self): ct = ClockTime() self.assertEqual(ct.seconds, 0) self.assertEqual(ct.nanoseconds, 0)
def utc_time(self): from time import time_ns t = time_ns() seconds, nanoseconds = divmod(t, 1000000000) return ClockTime(seconds, nanoseconds)
def utc_time(self): from time import time seconds, nanoseconds = nano_divmod(int(time() * 1000000), 1000000) return ClockTime(seconds, nanoseconds * 1000)