use chrono::prelude::*;
pub fn datetime(utc: bool) -> String {
let fmt = "%Y-%m-%d %H:%M:%S";
return {
if utc {
Utc::now().format(fmt).to_string()
} else {
Local::now().format(fmt).to_string()
}
};
}
pub fn to_date(datetime: &str) -> String {
let fmt = "%Y-%m-%d %H:%M:%S";
let datetime = NaiveDateTime::parse_from_str(datetime, fmt).unwrap();
let fmt = "%Y-%m-%d";
datetime.format(fmt).to_string()
}
pub fn to_time(datetime: &str) -> String {
let fmt = "%Y-%m-%d %H:%M:%S";
let datetime = NaiveDateTime::parse_from_str(datetime, fmt).unwrap();
let fmt = "%H:%M:%S";
datetime.format(fmt).to_string()
}
pub fn to_timestamp(datetime: &str, unit: &str) -> i64 {
let fmt = "%Y-%m-%d %H:%M:%S";
let timestamp = NaiveDateTime::parse_from_str(datetime, fmt).unwrap();
let local = Local.timestamp(0, 0).offset().utc_minus_local() as i64;
match unit {
"ms" => {
timestamp.timestamp_millis() + local * 1000
}
_ => {
timestamp.timestamp() + local
}
}
}
pub fn to_sn(utc: bool) -> String {
let fmt = "%Y%m%d%H%M%S";
return {
if utc {
Utc::now().format(fmt).to_string()
} else {
Local::now().format(fmt).to_string()
}
};
}
pub fn to_gmt() -> String {
let fmt = "%a %d %b %Y %H:%M:%S GMT";
Utc::now().format(fmt).to_string()
}