    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

    use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs};
    use chrono::{DateTime,Utc,NaiveDateTime};
    //use std::time::{UNIX_EPOCH, Duration};

    pub fn get_socket_addr(bind: &str, port: u16) -> SocketAddr {
      let url = format!("{}:{}",&bind,&port);
      match url.to_socket_addrs() {
        Ok(addrs_op) => if let Some(addr) = addrs_op.to_owned().next() {
             addr
           } else {
             SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port.to_owned())
           }
        Err(e) => {
           eprintln!("Evironment load error: {} {}", e, url);
           SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port.to_owned())
        }
      }
    }
    pub fn generate_uuid(alphabet: String) -> String {
        if alphabet.is_empty() {
            return String::from("");
        }
        let mut code = String::new();
        for _ in 0..32 {
            // 16 is the length of the above Hex alphabet
            let number = rand::random::<f32>() * (16 as f32);
            let number = number.round() as usize;
            if let Some(character) = alphabet.chars().nth(number) {
                code.push(character)
            }
        }
        code.insert(20, '-');
        code.insert(16, '-');
        code.insert(12, '-');
        code.insert(8, '-');

        code
    }
    #[allow(dead_code)]
    pub fn path_timestamp(filepath: &str) -> u32 {
        let arr_path = filepath.split("-").collect::<Vec<&str>>();
        if let Some(timestamp)  = arr_path.last() {
            timestamp.split(".").collect::<Vec<&str>>()[0].parse().unwrap_or_default()
        } else {
            0
        }
    }

    pub fn str_date_from_timestamp(timestamp: &str) -> String {
      if timestamp.is_empty() { return String::from(""); }
      let val: i64 = timestamp.parse().unwrap_or_default();
      let dt = NaiveDateTime::from_timestamp_opt(val, 0).unwrap_or_default();
      let datetime = DateTime::<Utc>::from_utc(dt, Utc);
      //  let val = u64::try_from(timestamp.to_owned()).unwrap_or_default();
      //  let str_timestamp = UNIX_EPOCH + Duration::from_millis(val);
      //  let datetime = DateTime::<Utc>::from(str_timestamp);
       datetime.format("%Y-%m-%d %H:%M:%S").to_string()
    }