Update id generation methods
This commit is contained in:
parent
b247be80cc
commit
e64912545a
6 changed files with 30 additions and 72 deletions
23
src/misc/id/aid.ts
Normal file
23
src/misc/id/aid.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// AID
|
||||
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列]
|
||||
|
||||
import * as cluster from 'cluster';
|
||||
|
||||
const TIME2000 = 946684800000;
|
||||
let counter = process.pid + (cluster.isMaster ? 0 : cluster.worker.id);
|
||||
|
||||
function getTime(time: number) {
|
||||
time = time - TIME2000;
|
||||
if (time < 0) time = 0;
|
||||
|
||||
return time.toString(36).padStart(8, '0');
|
||||
}
|
||||
|
||||
function getRandom() {
|
||||
return counter.toString(36).padStart(length, '0').substr(2);
|
||||
}
|
||||
|
||||
export function genAid(date: Date): string {
|
||||
counter++;
|
||||
return getTime(date.getTime()) + getRandom();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue