use chrono::{DateTime, Utc, Local};
pub struct Date;
impl Date {
pub fn now() -> DateTime<Utc> { Utc::now() }
pub fn now_local() -> DateTime<Local> { Local::now() }
pub fn fmt(dt: &DateTime<Utc>, fmt: &str) -> String {
dt.format(fmt).to_string()
}
pub fn from_timestamp(ts: i64) -> DateTime<Utc> {
DateTime::from_timestamp(ts, 0).unwrap_or_default()
}
pub fn relative(ts: i64) -> String {
let then = DateTime::from_timestamp(ts, 0).unwrap_or_default();
let diff = Utc::now() - then;
if diff.num_seconds() < 60 { format!("{}秒前", diff.num_seconds()) }
else if diff.num_minutes() < 60 { format!("{}分钟前", diff.num_minutes()) }
else if diff.num_hours() < 24 { format!("{}小时前", diff.num_hours()) }
else if diff.num_days() < 30 { format!("{}天前", diff.num_days()) }
else if diff.num_days() < 365 { format!("{}个月前", diff.num_days() / 30) }
else { format!("{}年前", diff.num_days() / 365) }
}
pub fn begin_of_day(dt: &DateTime<Utc>) -> DateTime<Utc> {
dt.date_naive().and_hms_opt(0, 0, 0).unwrap()
.and_utc()
}
pub fn end_of_day(dt: &DateTime<Utc>) -> DateTime<Utc> {
dt.date_naive().and_hms_opt(23, 59, 59).unwrap()
.and_utc()
}
}