Ejemplo n.º 1
0
def default_time():
    """Generate a TimeUUID from the current local date and time.  uuid 
    generator is provided by DSE, to be stored in Cassandra
    
    :return: A uuid
    :rtype: uuid.UUID
    
    """
    return uuid_from_time(datetime.now())
Ejemplo n.º 2
0
    def test_conversion_specific_date(self):
        dt = datetime(1981, 7, 11, microsecond=555000)

        uuid = util.uuid_from_time(dt)

        from uuid import UUID
        assert isinstance(uuid, UUID)

        ts = (uuid.time - 0x01b21dd213814000) / 1e7  # back to a timestamp
        new_dt = datetime.utcfromtimestamp(ts)

        # checks that we created a UUID1 with the proper timestamp
        assert new_dt == dt
    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)
Ejemplo n.º 4
0
def default_time():
    """Generate a TimeUUID from the current local date and time"""
    return uuid_from_time(datetime.now())