use std::time::{SystemTime, UNIX_EPOCH};
use chrono::{DateTime, NaiveDateTime, TimeZone, Local};
use chrono_tz::Tz;
use anyhow::Result;
pub fn current_timestamp() -> u64 {
let start = SystemTime::now();
let duration = start.duration_since(UNIX_EPOCH)
.expect("时间错误");
duration.as_secs()
}
pub fn from_str(s: &str, fmt: &str) -> Result<DateTime<Local>>{
let datetime = NaiveDateTime::parse_from_str(s, fmt)?;
let tz = Local::now().timezone();
let dt = tz.from_local_datetime(&datetime).single().unwrap();
Ok(dt)
}
pub fn from_str_with_timezone(s: &str, fmt: &str, timezone: &str) -> Result<DateTime<Tz>>{
let datetime = NaiveDateTime::parse_from_str(s, fmt)?;
let tz: Tz = timezone.parse()?;
let dt = tz.from_local_datetime(&datetime).single().unwrap();
Ok(dt)
}
pub fn to_timestamp(s: &str, fmt: &str) -> Result<i64>{
let dt = from_str(s, fmt)?;
Ok(dt.timestamp())
}