use chrono::prelude::*;
pub fn with_prefix(pfx: &str) -> String {
format!(
"{}-{}-{}",
pfx,
timestamp(6),
random_manager::secure_string(6)
)
}
#[test]
fn test_with_prefix() {
use log::info;
use std::{thread, time};
let _ = env_logger::builder()
.filter_level(log::LevelFilter::Info)
.is_test(true)
.try_init();
let ts1 = with_prefix("hello");
thread::sleep(time::Duration::from_millis(1001));
let ts2 = with_prefix("hello");
assert_ne!(ts1, ts2);
info!("ts1: {:?}", ts1);
info!("ts2: {:?}", ts2);
}
pub fn timestamp(n: usize) -> String {
let local: DateTime<Local> = Local::now();
let mut d = format!(
"{}{:02}{:02}{:02}{:02}",
local.year(),
local.month(),
local.day(),
local.hour(),
local.second(),
);
if d.len() > n {
d.truncate(n);
}
d
}
#[test]
fn test_timestamp() {
use log::info;
use std::{thread, time};
let _ = env_logger::builder()
.filter_level(log::LevelFilter::Info)
.is_test(true)
.try_init();
let ts1 = timestamp(12);
thread::sleep(time::Duration::from_millis(1001));
let ts2 = timestamp(12);
assert_ne!(ts1, ts2);
info!("ts1: {:?}", ts1);
info!("ts2: {:?}", ts2);
}