Exemplo n.º 1
0
 def __init__(self, description, timeuuid, source, source_elapsed,
              thread_name):
     self.description = description
     self.datetime = datetime.utcfromtimestamp(
         unix_time_from_uuid1(timeuuid))
     self.source = source
     if source_elapsed is not None:
         self.source_elapsed = timedelta(microseconds=source_elapsed)
     else:
         self.source_elapsed = None
     self.thread_name = thread_name
    def test_times_from_uuid1(self):
        node = uuid.getnode()
        now = time.time()
        u = uuid.uuid1(node, 0)

        t = util.unix_time_from_uuid1(u)
        self.assertAlmostEqual(now, t, 2)

        dt = util.datetime_from_uuid1(u)
        t = calendar.timegm(dt.timetuple()) + dt.microsecond / 1e6
        self.assertAlmostEqual(now, t, 2)
    def test_uuid_from_time(self):
        t = time.time()
        seq = 0x2aa5
        node = uuid.getnode()
        u = util.uuid_from_time(t, node, seq)
        # using AlmostEqual because time precision is different for
        # some platforms
        self.assertAlmostEqual(util.unix_time_from_uuid1(u), t, 4)
        self.assertEqual(u.node, node)
        self.assertEqual(u.clock_seq, seq)

        # random node
        u1 = util.uuid_from_time(t, clock_seq=seq)
        u2 = util.uuid_from_time(t, clock_seq=seq)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u1), t, 4)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u2), t, 4)
        self.assertEqual(u.clock_seq, seq)
        # not impossible, but we shouldn't get the same value twice
        self.assertNotEqual(u1.node, u2.node)

        # random seq
        u1 = util.uuid_from_time(t, node=node)
        u2 = util.uuid_from_time(t, node=node)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u1), t, 4)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u2), t, 4)
        self.assertEqual(u.node, node)
        # not impossible, but we shouldn't get the same value twice
        self.assertNotEqual(u1.clock_seq, u2.clock_seq)

        # node too large
        with self.assertRaises(ValueError):
            u = util.uuid_from_time(t, node=2 ** 48)

        # clock_seq too large
        with self.assertRaises(ValueError):
            u = util.uuid_from_time(t, clock_seq=0x4000)

        # construct from datetime
        dt = util.datetime_from_timestamp(t)
        u = util.uuid_from_time(dt, node, seq)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u), t, 4)
        self.assertEqual(u.node, node)
        self.assertEqual(u.clock_seq, seq)
Exemplo n.º 4
0
 def my_timestamp(self):
     return util.unix_time_from_uuid1(self.val)