Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions defmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"
Expand Down
77 changes: 77 additions & 0 deletions defmt/src/impls/core_/chrono.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use super::*;
extern crate chrono;
use chrono::{
DateTime, Datelike, FixedOffset, Months, NaiveDate, NaiveDateTime, NaiveTime, OutOfRange,
TimeDelta, TimeZone, Timelike, Utc,
};

impl<Tz: TimeZone> Format for DateTime<Tz> {
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");
}
}
2 changes: 2 additions & 0 deletions defmt/src/impls/core_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
mod alloc_;
mod array;
mod cell;
#[cfg(feature = "chrono")]
mod chrono;
#[cfg(feature = "ip_in_core")]
mod net;
mod num;
Expand Down
Loading