Skip to content

Commit 37a5fe7

Browse files
committed
Use nicer Debug implementations
Same as Display for now
1 parent d0bab6a commit 37a5fe7

File tree

6 files changed

+39
-27
lines changed

6 files changed

+39
-27
lines changed

src/date.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ pub struct Date {
4343

4444
impl fmt::Debug for Date {
4545
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
46-
f.debug_struct("Date")
47-
.field("year", &self.year())
48-
.field("ordinal", &self.ordinal())
49-
.finish()
46+
fmt::Display::fmt(self, f)
5047
}
5148
}
5249

src/offset_date_time.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const UNIX_EPOCH_JULIAN_DAY: i32 = Date::__from_ordinal_date_unchecked(1970, 1).
2727
/// A [`PrimitiveDateTime`] with a [`UtcOffset`].
2828
///
2929
/// All comparisons are performed using the UTC time.
30-
#[derive(Debug, Clone, Copy, Eq)]
30+
#[derive(Clone, Copy, Eq)]
3131
pub struct OffsetDateTime {
3232
/// The [`PrimitiveDateTime`], which is _always_ in the stored offset.
3333
pub(crate) local_datetime: PrimitiveDateTime,
@@ -1132,6 +1132,12 @@ impl fmt::Display for OffsetDateTime {
11321132
write!(f, "{} {} {}", self.date(), self.time(), self.offset)
11331133
}
11341134
}
1135+
1136+
impl fmt::Debug for OffsetDateTime {
1137+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1138+
write!(f, "{:?} {:?} {:?}", self.date(), self.time(), self.offset)
1139+
}
1140+
}
11351141
// endregion formatting & parsing
11361142

11371143
// region: trait impls

src/primitive_date_time.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::parsing::Parsable;
1313
use crate::{error, util, Date, Duration, Month, OffsetDateTime, Time, UtcOffset, Weekday};
1414

1515
/// Combined date and time.
16-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
16+
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
1717
pub struct PrimitiveDateTime {
1818
#[allow(clippy::missing_docs_in_private_items)]
1919
pub(crate) date: Date,
@@ -821,6 +821,12 @@ impl fmt::Display for PrimitiveDateTime {
821821
write!(f, "{} {}", self.date, self.time)
822822
}
823823
}
824+
825+
impl fmt::Debug for PrimitiveDateTime {
826+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
827+
write!(f, "{:?} {:?}", self.date, self.time)
828+
}
829+
}
824830
// endregion formatting & parsing
825831

826832
// region: trait impls

src/time.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ pub struct Time {
4242
padding: Padding,
4343
}
4444

45-
impl fmt::Debug for Time {
46-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47-
f.debug_struct("Time")
48-
.field("hour", &self.hour)
49-
.field("minute", &self.minute)
50-
.field("second", &self.second)
51-
.field("nanosecond", &self.nanosecond)
52-
.finish()
53-
}
54-
}
55-
5645
impl Time {
5746
/// Create a `Time` that is exactly midnight.
5847
///
@@ -656,6 +645,12 @@ impl fmt::Display for Time {
656645
)
657646
}
658647
}
648+
649+
impl fmt::Debug for Time {
650+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
651+
fmt::Display::fmt(self, f)
652+
}
653+
}
659654
// endregion formatting & parsing
660655

661656
// region: trait impls

src/utc_offset.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::OffsetDateTime;
2020
/// This struct can store values up to ±23:59:59. If you need support outside this range, please
2121
/// file an issue with your use case.
2222
// All three components _must_ have the same sign.
23-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
23+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2424
pub struct UtcOffset {
2525
#[allow(clippy::missing_docs_in_private_items)]
2626
hours: i8,
@@ -326,6 +326,19 @@ impl fmt::Display for UtcOffset {
326326
)
327327
}
328328
}
329+
330+
impl fmt::Debug for UtcOffset {
331+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
332+
write!(
333+
f,
334+
"{}{:02}:{:02}:{:02}",
335+
if self.is_negative() { '-' } else { '+' },
336+
self.hours.abs(),
337+
self.minutes.abs(),
338+
self.seconds.abs()
339+
)
340+
}
341+
}
329342
// endregion formatting & parsing
330343

331344
impl Neg for UtcOffset {

tests/integration/date.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ use time::{util, Date, Duration, Month, Weekday};
88

99
#[test]
1010
fn debug() {
11-
assert_eq!(
12-
format!("{:?}", date!(2020 - 02 - 03)),
13-
"Date { year: 2020, ordinal: 34 }"
14-
);
11+
assert_eq!(format!("{:?}", date!(2020 - 02 - 03)), "2020-02-03");
1512
}
1613

1714
#[test]
@@ -969,11 +966,9 @@ fn replace_month() {
969966
date!(2022 - 02 - 18).replace_month(Month::January),
970967
Ok(date!(2022 - 01 - 18))
971968
);
972-
assert!(
973-
date!(2022 - 01 - 30)
974-
.replace_month(Month::February)
975-
.is_err()
976-
); // 30 isn't a valid day in February
969+
assert!(date!(2022 - 01 - 30)
970+
.replace_month(Month::February)
971+
.is_err()); // 30 isn't a valid day in February
977972
}
978973

979974
#[test]

0 commit comments

Comments
 (0)