Python TimeUUID
Temperature: 0 °C
參考這一篇:
什麼時候讓用 time UUID,Url link
StackOverflow 有人提供了一段 code:
Url link
public static String reorderTimeUUId(String originalTimeUUID)
{
StringTokenizer tokens = new StringTokenizer(originalTimeUUID, "-");
if (tokens.countTokens() == 5)
{
String time_low = tokens.nextToken();
String time_mid = tokens.nextToken();
String time_high_and_version = tokens.nextToken();
String variant_and_sequence = tokens.nextToken();
String node = tokens.nextToken();
return time_high_and_version + '-' + time_mid + '-' + time_low + '-' + variant_and_sequence + '-' + node;
}
return originalTimeUUID;
}
把它翻成 python :
def getTimeUUID(uuidVar)
# ref: http://stackoverflow.com/questions/8713873/is-python-uuid1-sequential-as-timestamps
import uuid
if not isinstance(uuidVar, uuid.UUID):
uuidVar = uuid.uuid1()
tmp = str(uuidVar)
tokens = tmp.split("-")
if len(tokens) == 5:
time_low, time_mid, time_high_and_version, variant_and_sequence, node = tokens
return time_high_and_version + '-' + time_mid + '-' + time_low + '-' + variant_and_sequence + '-' + node
return tmp
原理是原本的 UUID (uuid1) 組合是:
time_low – time_mid – time_high_and_version – variant_and_sequence – node
轉成
time_high_and_version – time_mid – time_low – variant_and_sequence – node
這樣產生出來的 UUID 就可以按時間排序了.
三重湯城園區 Go