use chrono::{DateTime, FixedOffset, Utc};
#[derive(Clone, Debug)]
pub(crate) struct Time {
utc: DateTime<Utc>,
time_offset: FixedOffset,
pub(crate) detailed_display: bool,
}
impl Time {
pub fn new(utc: DateTime<Utc>, time_offset: FixedOffset) -> Self {
Self {
utc,
time_offset,
detailed_display: false,
}
}
pub fn now(time_zone: i32) -> Self {
Self::new(Utc::now(), FixedOffset::east_opt(time_zone * 3600).unwrap())
}
fn to_string(&self) -> String {
let format = "%Y-%m-%d %H:%M:%S%.3f".to_string();
if self.detailed_display {
format!("{} ({})", self.utc.format(&format), self.time_offset)
} else {
(self.utc.with_timezone(&self.time_offset))
.format(&format)
.to_string()
}
}
}
impl From<&Time> for String {
fn from(value: &Time) -> Self {
value.to_string()
}
}
impl std::fmt::Display for Time {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s: String = self.into();
write!(f, "{}", s)
}
}
unsafe impl Send for Time {}