1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use chrono::prelude::*;
pub fn with_prefix(pfx: &str) -> String {
format!("{}-{}-{}", pfx, timestamp(6), random_manager::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);
}