From 2d88c1e6d6d4978ead986b804b9777e6b00fbdc0 Mon Sep 17 00:00:00 2001 From: Paul Bender Date: Wed, 15 Oct 2025 14:03:35 -0700 Subject: [PATCH] Fix conversion from AM/PM to 24H. --- src/ds323x/datetime.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ds323x/datetime.rs b/src/ds323x/datetime.rs index 41fd610..f03617c 100644 --- a/src/ds323x/datetime.rs +++ b/src/ds323x/datetime.rs @@ -271,11 +271,20 @@ fn is_am(hours_data: u8) -> bool { hours_data & BitFlags::AM_PM == 0 } +/// Returns the hours in 24H time. +/// +/// 12AM and 12PM are treated differently because 12AM is midnight (not noon) +/// and 12PM is noon (not midnight). So, 12AM must be converted 0, and 12PM must +/// be converted to 0 before adding 12. +/// +/// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 +/// |AM|AM|AM|AM|AM|AM|AM|AM|AM|AM|AM|AM|PM|PM|PM|PM|PM|PM|PM|PM|PM|PM|PM|PM| +/// 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 fn get_h24(hour: Hours) -> u8 { match hour { Hours::H24(h) => h, - Hours::AM(h) => h, - Hours::PM(h) => h + 12, + Hours::AM(h) => h % 12, + Hours::PM(h) => (h % 12) + 12, } } @@ -286,15 +295,14 @@ mod tests { #[test] fn can_convert_to_h24() { assert_eq!(0, get_h24(Hours::H24(0))); - assert_eq!(0, get_h24(Hours::AM(0))); - assert_eq!(12, get_h24(Hours::PM(0))); + assert_eq!(0, get_h24(Hours::AM(12))); + assert_eq!(12, get_h24(Hours::PM(12))); assert_eq!(1, get_h24(Hours::H24(1))); assert_eq!(1, get_h24(Hours::AM(1))); assert_eq!(13, get_h24(Hours::PM(1))); assert_eq!(23, get_h24(Hours::H24(23))); - assert_eq!(12, get_h24(Hours::AM(12))); assert_eq!(23, get_h24(Hours::PM(11))); } }