last around 2320
parent
af3da2e42a
commit
52cf992677
|
@ -0,0 +1,33 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Base62 characters
|
||||||
|
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||||
|
|
||||||
|
// Converts a number to Base-62
|
||||||
|
func toBase62(num int64) string {
|
||||||
|
result := ""
|
||||||
|
for num > 0 {
|
||||||
|
result = string(base62[num%62]) + result
|
||||||
|
num /= 62
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate an 8-character unique ID
|
||||||
|
func generateID() string {
|
||||||
|
EPOCH_START := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
now := time.Now().UTC().UnixMilli() - EPOCH_START.UnixMilli()
|
||||||
|
random := rand.Intn(1000) // 3-digit random number (000-999)
|
||||||
|
id := now*1000 + int64(random) // Combine timestamp + randomness
|
||||||
|
return fmt.Sprintf("%08s", toBase62(id))[:8] // Ensure fixed 8 chars
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(generateID()) // Example output: "5F3G8kL2"
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
function toBase62(num) {
|
||||||
|
let result = "";
|
||||||
|
while (num > 0) {
|
||||||
|
result = base62[num % 62] + result;
|
||||||
|
num = Math.floor(num / 62);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateID() {
|
||||||
|
const epochStart = new Date("2020-01-01").getTime();
|
||||||
|
const now = Date.now() - epochStart;
|
||||||
|
const random = Math.floor(Math.random() * 1000); // 3-digit random number
|
||||||
|
const id = now * 1000 + random; // Combine timestamp + randomness
|
||||||
|
return toBase62(id).padStart(8, "0").slice(0, 8); // Ensure 8 chars
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(generateID()); // Example output: "7zD2LmX1"
|
|
@ -0,0 +1,20 @@
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
|
BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||||
|
|
||||||
|
def to_base62(num):
|
||||||
|
result = ""
|
||||||
|
while num > 0:
|
||||||
|
result = BASE62[num % 62] + result
|
||||||
|
num //= 62
|
||||||
|
return result
|
||||||
|
|
||||||
|
def generate_id():
|
||||||
|
epoch_start = int(time.mktime((2020, 1, 1, 0, 0, 0, 0, 0, 0))) * 1000
|
||||||
|
now = int(time.time() * 1000) - epoch_start
|
||||||
|
random_num = random.randint(0, 999) # 3-digit random number
|
||||||
|
id_num = now * 1000 + random_num # Combine timestamp + randomness
|
||||||
|
return to_base62(id_num).zfill(8)[:8] # Ensure 8 chars
|
||||||
|
|
||||||
|
print(generate_id()) # Example output: "8F2T5MkQ"
|
|
@ -0,0 +1,29 @@
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH, Duration};
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
// Base-62 encoding characters
|
||||||
|
const BASE62: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
// Convert a number to Base-62
|
||||||
|
fn to_base62(mut num: u64) -> String {
|
||||||
|
let mut result = String::new();
|
||||||
|
while num > 0 {
|
||||||
|
result.insert(0, BASE62[(num % 62) as usize] as char);
|
||||||
|
num /= 62;
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate an 8-character unique ID
|
||||||
|
fn generate_id() -> String {
|
||||||
|
let epoch_start = UNIX_EPOCH + Duration::from_secs(1577836800); // 2020-01-01
|
||||||
|
let now = SystemTime::now().duration_since(epoch_start).unwrap().as_millis();
|
||||||
|
let random: u64 = rand::thread_rng().gen_range(0..1000); // 3-digit random number
|
||||||
|
let id = now * 1000 + random; // Combine timestamp + randomness
|
||||||
|
let base62_id = to_base62(id);
|
||||||
|
format!("{:0>8}", base62_id)[..8].to_string() // Ensure 8 chars
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("{}", generate_id()); // Example output: "9XcF7Bv2"
|
||||||
|
}
|
Loading…
Reference in New Issue