1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-24 03:28:43 +09:00
misskey/src/misc/id/meidg.ts

29 lines
527 B
TypeScript
Raw Normal View History

2019-05-05 09:29:15 +09:00
const CHARS = '0123456789abcdef';
// 4bit Fixed hex value 'g'
// 44bit UNIX Time ms in Hex
// 48bit Random value in Hex
function getTime(time: number) {
if (time < 0) time = 0;
if (time === 0) {
return CHARS[0];
}
return time.toString(16).padStart(11, CHARS[0]);
}
function getRandom() {
let str = '';
for (let i = 0; i < 12; i++) {
str += CHARS[Math.floor(Math.random() * CHARS.length)];
}
return str;
}
export function genMeidg(date: Date): string {
return 'g' + getTime(date.getTime()) + getRandom();
}