Skip to content

Commit fa9b500

Browse files
Add impls for the Defmt logging crate
- Add impls for the main structs where it is obvious how to do so - Gate this behind a new cargo feature called "defmt"
1 parent 1c7567b commit fa9b500

File tree

10 files changed

+101
-21
lines changed

10 files changed

+101
-21
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ now = ["std"]
2828
oldtime = []
2929
wasmbind = ["wasm-bindgen", "js-sys"]
3030
unstable-locales = ["pure-rust-locales"]
31+
defmt = ["dep:defmt"]
3132
# Note that rkyv-16, rkyv-32, and rkyv-64 are mutually exclusive.
3233
rkyv = ["dep:rkyv", "rkyv/size_32"]
3334
rkyv-16 = ["dep:rkyv", "rkyv?/size_16"]
@@ -43,6 +44,7 @@ serde = { version = "1.0.99", default-features = false, optional = true }
4344
pure-rust-locales = { version = "0.8", optional = true }
4445
rkyv = { version = "0.7.43", optional = true, default-features = false }
4546
arbitrary = { version = "1.0.0", features = ["derive"], optional = true }
47+
defmt = { version = "0.3.8", optional = true }
4648

4749
[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
4850
wasm-bindgen = { version = "0.2", optional = true }

src/datetime/mod.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33

44
//! ISO 8601 date and time with time zone.
55
6-
#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
7-
use alloc::string::String;
8-
use core::borrow::Borrow;
9-
use core::cmp::Ordering;
10-
use core::fmt::Write;
11-
use core::ops::{Add, AddAssign, Sub, SubAssign};
12-
use core::time::Duration;
13-
use core::{fmt, hash, str};
14-
#[cfg(feature = "std")]
15-
use std::time::{SystemTime, UNIX_EPOCH};
16-
176
#[cfg(all(feature = "unstable-locales", feature = "alloc"))]
187
use crate::format::Locale;
198
use crate::format::{
@@ -30,6 +19,18 @@ use crate::offset::{FixedOffset, LocalResult, Offset, TimeZone, Utc};
3019
use crate::Date;
3120
use crate::{expect, try_opt};
3221
use crate::{Datelike, Months, TimeDelta, Timelike, Weekday};
22+
#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
23+
use alloc::string::String;
24+
use core::borrow::Borrow;
25+
use core::cmp::Ordering;
26+
use core::fmt::Write;
27+
use core::ops::{Add, AddAssign, Sub, SubAssign};
28+
use core::time::Duration;
29+
use core::{fmt, hash, str};
30+
#[cfg(feature = "defmt")]
31+
use defmt::{Format, Formatter};
32+
#[cfg(feature = "std")]
33+
use std::time::{SystemTime, UNIX_EPOCH};
3334

3435
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
3536
use rkyv::{Archive, Deserialize, Serialize};
@@ -1093,6 +1094,13 @@ impl DateTime<FixedOffset> {
10931094
}
10941095
}
10951096

1097+
#[cfg(feature = "defmt")]
1098+
impl<Tz: TimeZone> Format for DateTime<Tz> {
1099+
fn format(&self, fmt: Formatter) {
1100+
defmt::write!(fmt, "{=i32:04}-{=u32:02}-{=u32:02}", self.year(), self.month(), self.day());
1101+
}
1102+
}
1103+
10961104
impl<Tz: TimeZone> DateTime<Tz>
10971105
where
10981106
Tz::Offset: fmt::Display,

src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ pub use time_delta::TimeDelta;
521521
pub type Duration = TimeDelta;
522522

523523
use core::fmt;
524+
#[cfg(feature = "defmt")]
525+
use defmt::{Format, Formatter};
524526

525527
/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
526528
pub mod prelude {
@@ -678,6 +680,13 @@ impl fmt::Display for OutOfRange {
678680
}
679681
}
680682

683+
#[cfg(feature = "defmt")]
684+
impl Format for OutOfRange {
685+
fn format(&self, fmt: Formatter) {
686+
defmt::write!(fmt, "out of range");
687+
}
688+
}
689+
681690
impl fmt::Debug for OutOfRange {
682691
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
683692
write!(f, "out of range")

src/month.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::fmt;
2-
2+
#[cfg(feature = "defmt")]
3+
use defmt::{Format, Formatter};
34
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
45
use rkyv::{Archive, Deserialize, Serialize};
56

@@ -240,6 +241,13 @@ impl Months {
240241
}
241242
}
242243

244+
#[cfg(feature = "defmt")]
245+
impl Format for Months {
246+
fn format(&self, fmt: Formatter) {
247+
defmt::write!(fmt, "Months({=u32})", self.as_u32());
248+
}
249+
}
250+
243251
/// An error resulting from reading `<Month>` value with `FromStr`.
244252
#[derive(Clone, PartialEq, Eq)]
245253
pub struct ParseMonthError {

src/naive/date/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use core::iter::FusedIterator;
1919
use core::num::NonZeroI32;
2020
use core::ops::{Add, AddAssign, Sub, SubAssign};
2121
use core::{fmt, str};
22-
22+
#[cfg(feature = "defmt")]
23+
use defmt::{Format, Formatter};
2324
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
2425
use rkyv::{Archive, Deserialize, Serialize};
2526

@@ -2238,6 +2239,13 @@ impl fmt::Debug for NaiveDate {
22382239
}
22392240
}
22402241

2242+
#[cfg(feature = "defmt")]
2243+
impl Format for NaiveDate {
2244+
fn format(&self, fmt: Formatter) {
2245+
defmt::write!(fmt, "{=i32:04}-{=u32:02}-{=u32:02}", self.year(), self.month(), self.day());
2246+
}
2247+
}
2248+
22412249
/// The `Display` output of the naive date `d` is the same as
22422250
/// [`d.format("%Y-%m-%d")`](crate::format::strftime).
22432251
///

src/naive/datetime/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use core::fmt::Write;
99
use core::ops::{Add, AddAssign, Sub, SubAssign};
1010
use core::time::Duration;
1111
use core::{fmt, str};
12-
12+
#[cfg(feature = "defmt")]
13+
use defmt::{Format, Formatter};
1314
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
1415
use rkyv::{Archive, Deserialize, Serialize};
1516

@@ -2149,3 +2150,10 @@ impl Default for NaiveDateTime {
21492150
Self::UNIX_EPOCH
21502151
}
21512152
}
2153+
2154+
#[cfg(feature = "defmt")]
2155+
impl Format for NaiveDateTime {
2156+
fn format(&self, fmt: Formatter) {
2157+
defmt::write!(fmt, "{}T{}", self.date(), self.time());
2158+
}
2159+
}

src/naive/time/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use core::borrow::Borrow;
88
use core::ops::{Add, AddAssign, Sub, SubAssign};
99
use core::time::Duration;
1010
use core::{fmt, str};
11-
11+
#[cfg(feature = "defmt")]
12+
use defmt::{Format, Formatter};
1213
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
1314
use rkyv::{Archive, Deserialize, Serialize};
1415

@@ -1641,3 +1642,10 @@ impl Default for NaiveTime {
16411642
NaiveTime::from_hms_opt(0, 0, 0).unwrap()
16421643
}
16431644
}
1645+
1646+
#[cfg(feature = "defmt")]
1647+
impl Format for NaiveTime {
1648+
fn format(&self, fmt: Formatter) {
1649+
defmt::write!(fmt, "{=u32}:{=u32}:{=u32}", self.hour(), self.minute(), self.second());
1650+
}
1651+
}

src/offset/fixed.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
66
use core::fmt;
77
use core::str::FromStr;
8-
8+
#[cfg(feature = "defmt")]
9+
use defmt::{Format, Formatter};
910
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
1011
use rkyv::{Archive, Deserialize, Serialize};
1112

@@ -152,6 +153,13 @@ impl Offset for FixedOffset {
152153
}
153154
}
154155

156+
#[cfg(feature = "defmt")]
157+
impl Format for FixedOffset {
158+
fn format(&self, fmt: Formatter) {
159+
defmt::write!(fmt, "FixedOffset({=i32})", self.local_minus_utc());
160+
}
161+
}
162+
155163
impl fmt::Debug for FixedOffset {
156164
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
157165
let offset = self.local_minus_utc;

src/offset/utc.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
//! The UTC (Coordinated Universal Time) time zone.
55
66
use core::fmt;
7+
#[cfg(feature = "defmt")]
8+
use defmt::{Format, Formatter};
9+
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
10+
use rkyv::{Archive, Deserialize, Serialize};
711
#[cfg(all(
812
feature = "now",
913
not(all(
@@ -14,9 +18,6 @@ use core::fmt;
1418
))]
1519
use std::time::{SystemTime, UNIX_EPOCH};
1620

17-
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
18-
use rkyv::{Archive, Deserialize, Serialize};
19-
2021
use super::{FixedOffset, MappedLocalTime, Offset, TimeZone};
2122
use crate::naive::{NaiveDate, NaiveDateTime};
2223
#[cfg(feature = "now")]
@@ -150,3 +151,10 @@ impl fmt::Display for Utc {
150151
write!(f, "UTC")
151152
}
152153
}
154+
155+
#[cfg(feature = "defmt")]
156+
impl Format for Utc {
157+
fn format(&self, fmt: Formatter) {
158+
defmt::write!(fmt, "UTC");
159+
}
160+
}

src/time_delta.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
//! Temporal quantification
1212
13+
use crate::{expect, try_opt};
1314
use core::fmt;
1415
use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
1516
use core::time::Duration;
17+
#[cfg(feature = "defmt")]
18+
use defmt::{Format, Formatter};
1619
#[cfg(feature = "std")]
1720
use std::error::Error;
1821

19-
use crate::{expect, try_opt};
20-
2122
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
2223
use rkyv::{Archive, Deserialize, Serialize};
2324

@@ -593,6 +594,18 @@ impl fmt::Display for TimeDelta {
593594
}
594595
}
595596

597+
#[cfg(feature = "defmt")]
598+
impl Format for TimeDelta {
599+
fn format(&self, fmt: Formatter) {
600+
defmt::write!(
601+
fmt,
602+
"TimeDelta {{ secs: {=i64}, nanos: {=i32} }}",
603+
self.num_seconds(),
604+
self.subsec_nanos()
605+
);
606+
}
607+
}
608+
596609
/// Represents error when converting `TimeDelta` to/from a standard library
597610
/// implementation
598611
///

0 commit comments

Comments
 (0)