Exemplo n.º 1
0
    def from_datetime(self, dt):
        """
        generates a UUID for a given datetime

        :param dt: datetime
        :type dt: datetime
        :return:
        """
        global _last_timestamp

        epoch = datetime(1970, 1, 1, tzinfo=dt.tzinfo)
        offset = epoch.tzinfo.utcoffset(epoch).total_seconds() if epoch.tzinfo else 0
        timestamp = (dt  - epoch).total_seconds() - offset

        node = None
        clock_seq = None

        nanoseconds = int(timestamp * 1e9)
        timestamp = int(nanoseconds // 100) + 0x01b21dd213814000L

        if clock_seq is None:
            import random
            clock_seq = random.randrange(1 << 14L)  # instead of stable storage
        time_low = timestamp & 0xffffffffL
        time_mid = (timestamp >> 32L) & 0xffffL
        time_hi_version = (timestamp >> 48L) & 0x0fffL
        clock_seq_low = clock_seq & 0xffL
        clock_seq_hi_variant = (clock_seq >> 8L) & 0x3fL
        if node is None:
            node = getnode()
        return pyUUID(fields=(time_low, time_mid, time_hi_version,
                            clock_seq_hi_variant, clock_seq_low, node), version=1)
Exemplo n.º 2
0
    def from_datetime(self, dt):
        """
        generates a UUID for a given datetime

        :param dt: datetime
        :type dt: datetime
        :return:
        """
        global _last_timestamp

        epoch = datetime(1970, 1, 1, tzinfo=dt.tzinfo)
        offset = epoch.tzinfo.utcoffset(
            epoch).total_seconds() if epoch.tzinfo else 0
        timestamp = (dt - epoch).total_seconds() - offset

        node = None
        clock_seq = None

        nanoseconds = int(timestamp * 1e9)
        timestamp = int(nanoseconds // 100) + 0x01b21dd213814000

        if clock_seq is None:
            import random
            clock_seq = random.randrange(1 << 14)  # instead of stable storage
        time_low = timestamp & 0xffffffff
        time_mid = (timestamp >> 32) & 0xffff
        time_hi_version = (timestamp >> 48) & 0x0fff
        clock_seq_low = clock_seq & 0xff
        clock_seq_hi_variant = (clock_seq >> 8) & 0x3f
        if node is None:
            node = getnode()
        return pyUUID(fields=(time_low, time_mid, time_hi_version,
                              clock_seq_hi_variant, clock_seq_low, node),
                      version=1)
def generate_timeuuid():
    """
    code modified from: http://goo.gl/czeA4P
    """
    dt = datetime.datetime.now()

    epoch = datetime.datetime(1970, 1, 1, tzinfo=dt.tzinfo)
    offset = epoch.tzinfo.utcoffset(epoch).total_seconds() \
        if epoch.tzinfo else 0
    timestamp = (dt - epoch).total_seconds() - offset

    node = None
    clock_seq = None

    nanoseconds = int(timestamp * 1e9)
    timestamp = int(nanoseconds // 100) + 0x01b21dd213814000

    if clock_seq is None:
        import random

        clock_seq = random.randrange(1 << 14)  # instead of stable storage
    time_low = timestamp & 0xffffffff
    time_mid = (timestamp >> 32) & 0xffff
    time_hi_version = (timestamp >> 48) & 0x0fff
    clock_seq_low = clock_seq & 0xff
    clock_seq_hi_variant = (clock_seq >> 8) & 0x3f
    if node is None:
        node = getnode()
    return pyUUID(fields=(time_low, time_mid, time_hi_version,
                          clock_seq_hi_variant, clock_seq_low, node),
                  version=1)
def generate_timeuuid():
    """
    code modified from: http://goo.gl/czeA4P
    """
    dt = datetime.datetime.now()

    epoch = datetime.datetime(1970, 1, 1, tzinfo=dt.tzinfo)
    offset = epoch.tzinfo.utcoffset(epoch).total_seconds() \
        if epoch.tzinfo else 0
    timestamp = (dt - epoch).total_seconds() - offset

    node = None
    clock_seq = None

    nanoseconds = int(timestamp * 1e9)
    timestamp = int(nanoseconds // 100) + 0x01b21dd213814000

    if clock_seq is None:
        import random

        clock_seq = random.randrange(1 << 14)  # instead of stable storage
    time_low = timestamp & 0xffffffff
    time_mid = (timestamp >> 32) & 0xffff
    time_hi_version = (timestamp >> 48) & 0x0fff
    clock_seq_low = clock_seq & 0xff
    clock_seq_hi_variant = (clock_seq >> 8) & 0x3f
    if node is None:
        node = getnode()
    return pyUUID(fields=(time_low, time_mid, time_hi_version,
                          clock_seq_hi_variant, clock_seq_low, node),
                  version=1)
Exemplo n.º 5
0
    def __assert_uuid(self, data, attribs):
        if data is None:
            return self.validation_success(data)

        try:
            return self.validation_success(pyUUID(data))
        except Exception:
            return self.validation_error(data, attribs['error'])