68 lines
2.3 KiB
Rust
68 lines
2.3 KiB
Rust
use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs};
|
|
use chrono::NaiveDateTime;
|
|
//use chrono::{DateTime,Local, 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 datetime = NaiveDateTime::from_timestamp_opt(val, 0).unwrap_or_default(); // (Local::now());
|
|
/*
|
|
let naive_utc = dt.naive_utc();
|
|
let offset = dt.offset().clone();
|
|
// Serialize, pass through FFI... and recreate the `DateTime`:
|
|
let datetime = DateTime::<Local>::from_naive_utc_and_offset(naive_utc, offset);
|
|
|
|
// let datetime = DateTime::<Utc>::from_ut(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()
|
|
}
|