diff --git a/CHANGELOG.md b/CHANGELOG.md index 58c962df3..42aefb11c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +- [#893]: Add impls for structs in Chrono crate - [#889]: Add script for book hosting - [#887]: Fix interning example in the book - [#884]: Upgrade dependencies: notify is now at v7, thiserror is now at v2 diff --git a/defmt/Cargo.toml b/defmt/Cargo.toml index d24555db0..c15942768 100644 --- a/defmt/Cargo.toml +++ b/defmt/Cargo.toml @@ -20,6 +20,7 @@ version = "0.3.8" [features] alloc = [] avoid-default-panic = [] +chrono = [] ip_in_core = [] # Encoding feature flags. These should only be set by end-user crates, not by library crates. @@ -45,6 +46,7 @@ unstable-test = [ "defmt-macros/unstable-test" ] [dependencies] defmt-macros = { path = "../macros", version = "0.3.2" } bitflags = "1" +chrono = "0.4.38" [dev-dependencies] rustc_version = "0.4" diff --git a/defmt/src/impls/core_/chrono.rs b/defmt/src/impls/core_/chrono.rs new file mode 100644 index 000000000..cab07fec6 --- /dev/null +++ b/defmt/src/impls/core_/chrono.rs @@ -0,0 +1,77 @@ +use super::*; +extern crate chrono; +use chrono::{ + DateTime, Datelike, FixedOffset, Months, NaiveDate, NaiveDateTime, NaiveTime, OutOfRange, + TimeDelta, TimeZone, Timelike, Utc, +}; + +impl Format for DateTime { + fn format(&self, fmt: Formatter) { + crate::write!( + fmt, + "{:04}-{:02}-{:02}", + self.year(), + self.month(), + self.day() + ); + } +} + +impl Format for FixedOffset { + fn format(&self, fmt: Formatter) { + crate::write!(fmt, "FixedOffset({})", self.local_minus_utc()); + } +} + +impl Format for Months { + fn format(&self, fmt: Formatter) { + crate::write!(fmt, "Months({=u32})", self.as_u32()); + } +} + +impl Format for NaiveDate { + fn format(&self, fmt: Formatter) { + crate::write!( + fmt, + "{:04}-{:02}-{:02}", + self.year(), + self.month(), + self.day() + ); + } +} + +impl Format for NaiveDateTime { + fn format(&self, fmt: Formatter) { + crate::write!(fmt, "{}T{}", self.date(), self.time()); + } +} + +impl Format for NaiveTime { + fn format(&self, fmt: Formatter) { + crate::write!(fmt, "{}:{}:{}", self.hour(), self.minute(), self.second()); + } +} + +impl Format for OutOfRange { + fn format(&self, fmt: Formatter) { + crate::write!(fmt, "out of range"); + } +} + +impl Format for TimeDelta { + fn format(&self, fmt: Formatter) { + crate::write!( + fmt, + "TimeDelta {{ secs: {=i64}, nanos: {=i32} }}", + self.num_seconds(), + self.subsec_nanos() + ); + } +} + +impl Format for Utc { + fn format(&self, fmt: Formatter) { + crate::write!(fmt, "UTC"); + } +} diff --git a/defmt/src/impls/core_/mod.rs b/defmt/src/impls/core_/mod.rs index b005ca577..ab781119b 100644 --- a/defmt/src/impls/core_/mod.rs +++ b/defmt/src/impls/core_/mod.rs @@ -8,6 +8,8 @@ mod alloc_; mod array; mod cell; +#[cfg(feature = "chrono")] +mod chrono; #[cfg(feature = "ip_in_core")] mod net; mod num;