use crate::fmt::format::Writer;
use crate::fmt::time::FormatTime;
use alloc::{format, string::String, sync::Arc};
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub struct ChronoLocal {
format: Arc<ChronoFmtType>,
}
impl ChronoLocal {
pub fn rfc_3339() -> Self {
Self {
format: Arc::new(ChronoFmtType::Rfc3339),
}
}
pub fn new(format_string: String) -> Self {
Self {
format: Arc::new(ChronoFmtType::Custom(format_string)),
}
}
}
impl FormatTime for ChronoLocal {
fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result {
let t = chrono::Local::now();
match self.format.as_ref() {
ChronoFmtType::Rfc3339 => {
use chrono::format::{Fixed, Item};
write!(
w,
"{}",
t.format_with_items(core::iter::once(Item::Fixed(Fixed::RFC3339)))
)
}
ChronoFmtType::Custom(fmt) => {
write!(w, "{}", t.format(fmt))
}
}
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub struct ChronoUtc {
format: Arc<ChronoFmtType>,
}
impl ChronoUtc {
pub fn rfc_3339() -> Self {
Self {
format: Arc::new(ChronoFmtType::Rfc3339),
}
}
pub fn new(format_string: String) -> Self {
Self {
format: Arc::new(ChronoFmtType::Custom(format_string)),
}
}
}
impl FormatTime for ChronoUtc {
fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result {
let t = chrono::Utc::now();
match self.format.as_ref() {
ChronoFmtType::Rfc3339 => w.write_str(&t.to_rfc3339()),
ChronoFmtType::Custom(fmt) => w.write_str(&format!("{}", t.format(fmt))),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Default)]
enum ChronoFmtType {
#[default]
Rfc3339,
Custom(String),
}
#[cfg(test)]
mod tests {
use crate::fmt::format::Writer;
use crate::fmt::time::FormatTime;
use alloc::{borrow::ToOwned, string::String, sync::Arc};
use super::ChronoFmtType;
use super::ChronoLocal;
use super::ChronoUtc;
#[test]
fn test_chrono_format_time_utc_default() {
let mut buf = String::new();
let mut dst: Writer<'_> = Writer::new(&mut buf);
assert!(FormatTime::format_time(&ChronoUtc::default(), &mut dst).is_ok());
assert!(chrono::DateTime::parse_from_str(&buf, "%FT%H:%M:%S%.6f%z").is_ok());
}
#[test]
fn test_chrono_format_time_utc_custom() {
let fmt = ChronoUtc {
format: Arc::new(ChronoFmtType::Custom("%a %b %e %T %Y".to_owned())),
};
let mut buf = String::new();
let mut dst: Writer<'_> = Writer::new(&mut buf);
assert!(FormatTime::format_time(&fmt, &mut dst).is_ok());
assert!(chrono::NaiveDateTime::parse_from_str(&buf, "%a %b %e %T %Y").is_ok());
}
#[test]
fn test_chrono_format_time_local_default() {
let mut buf = String::new();
let mut dst: Writer<'_> = Writer::new(&mut buf);
assert!(FormatTime::format_time(&ChronoLocal::default(), &mut dst).is_ok());
assert!(chrono::DateTime::parse_from_str(&buf, "%FT%H:%M:%S%.6f%z").is_ok());
}
#[test]
fn test_chrono_format_time_local_custom() {
let fmt = ChronoLocal {
format: Arc::new(ChronoFmtType::Custom("%a %b %e %T %Y".to_owned())),
};
let mut buf = String::new();
let mut dst: Writer<'_> = Writer::new(&mut buf);
assert!(FormatTime::format_time(&fmt, &mut dst).is_ok());
assert!(chrono::NaiveDateTime::parse_from_str(&buf, "%a %b %e %T %Y").is_ok());
}
}