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
use chrono::prelude::Local;
use rand::distributions::Alphanumeric;
use rand::Rng;
use uuid::Uuid;

/// Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC
/// (aka "UNIX timestamp").
#[inline]
pub fn current_epoch_seconds() -> i64 {
    Local::now().timestamp()
}

#[inline]
pub fn new_hyphenated_uuid() -> String {
    Uuid::new_v4().to_hyphenated().to_string()
}

pub fn random_string(length: usize) -> String {
    rand::thread_rng()
        .sample_iter(&Alphanumeric)
        .take(length)
        .collect::<String>()
}

#[cfg(test)]
pub mod test {

    use super::*;

    #[test]
    fn should_return_a_random_string() {
        assert_eq!(10, random_string(10).len());
        assert_eq!(0, random_string(0).len());
    }
}