Generate a 10-char, alphanumeric base36 AIMS ID

PHOTO EMBED

Fri May 06 2022 06:47:52 GMT+0000 (Coordinated Universal Time)

Saved by @yusufalao #python #planetpress

# Python - Generate a 10-char, alphanumeric base36 aims id:

# Server ID (1)
# Year digit (1)
# Month digits (2)
# Date digits (2)
# Hour digits (2)
# Minute digits (2)
# Second digits (2)
# Millisecond digits (3)


from datetime import datetime
import re

# Get current date-time, removing all non numeric chars
current = re.sub(r'\D','',str(datetime.now()))

# remove 3 digits from either end and convert to int
current = int(current[3:17])


def convert_to_base36(int_str):
    # convert current integer 'string' to base36
    base36_alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    server_id = Watch.GetVariable("global.AIMS_Server_ID")

    b36_str = ""

    while int_str != 0:
        int_str, i = divmod(int_str, len(base36_alphabet))
        b36_str = base36_alphabet[i] + b36_str

    # ensure 9 characters in str
    b36_str = ("000000000" + b36_str)[-9:]

    return server_id + b36_str

# min & max values for current
min_test = int("00101000000000")
max_test = 91231235959999

aims_id = convert_to_base36(current)
content_copyCOPY