From ae313ff83075474d1eed8de1ac840a2eafd59710 Mon Sep 17 00:00:00 2001 From: Nate Butler Date: Tue, 19 Dec 2023 11:16:59 -0500 Subject: [PATCH] Allow format_distance to take a DateTimeType --- crates/ui2/src/utils/format_distance.rs | 65 +++++++++++++++++-------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/crates/ui2/src/utils/format_distance.rs b/crates/ui2/src/utils/format_distance.rs index d157d30bbe..f13e92bbe9 100644 --- a/crates/ui2/src/utils/format_distance.rs +++ b/crates/ui2/src/utils/format_distance.rs @@ -1,4 +1,23 @@ -use chrono::NaiveDateTime; +use chrono::{DateTime, Local, NaiveDateTime}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum DateTimeType { + Naive(NaiveDateTime), + Local(DateTime), +} + +impl DateTimeType { + /// Converts the DateTimeType to a NaiveDateTime. + /// + /// If the DateTimeType is already a NaiveDateTime, it will be returned as is. + /// If the DateTimeType is a DateTime, it will be converted to a NaiveDateTime. + pub fn to_naive(&self) -> NaiveDateTime { + match self { + DateTimeType::Naive(naive) => *naive, + DateTimeType::Local(local) => local.naive_local(), + } + } +} /// Calculates the distance in seconds between two NaiveDateTime objects. /// It returns a signed integer denoting the difference. If `date` is earlier than `base_date`, the returned value will be negative. @@ -108,13 +127,13 @@ fn distance_string(distance: i64, include_seconds: bool, add_suffix: bool) -> St /// ``` /// /// Output: `"There was about 3 years between the first and last crewed moon landings."` -pub fn naive_format_distance( - date: NaiveDateTime, +pub fn format_distance( + date: DateTimeType, base_date: NaiveDateTime, include_seconds: bool, add_suffix: bool, ) -> String { - let distance = distance_in_seconds(date, base_date); + let distance = distance_in_seconds(date.to_naive(), base_date); distance_string(distance, include_seconds, add_suffix) } @@ -142,14 +161,14 @@ pub fn naive_format_distance( /// ``` /// /// Output: `It's been over 54 years since Apollo 11 first landed on the moon.` -pub fn naive_format_distance_from_now( - datetime: NaiveDateTime, +pub fn format_distance_from_now( + datetime: DateTimeType, include_seconds: bool, add_suffix: bool, ) -> String { let now = chrono::offset::Local::now().naive_local(); - naive_format_distance(datetime, now, include_seconds, add_suffix) + format_distance(datetime, now, include_seconds, add_suffix) } #[cfg(test)] @@ -159,38 +178,44 @@ mod tests { #[test] fn test_naive_format_distance() { - let date = - NaiveDateTime::from_timestamp_opt(9600, 0).expect("Invalid NaiveDateTime for date"); - let base_date = - NaiveDateTime::from_timestamp_opt(0, 0).expect("Invalid NaiveDateTime for base_date"); + let date = DateTimeType::Naive( + NaiveDateTime::from_timestamp_opt(9600, 0).expect("Invalid NaiveDateTime for date"), + ); + let base_date = DateTimeType::Naive( + NaiveDateTime::from_timestamp_opt(0, 0).expect("Invalid NaiveDateTime for base_date"), + ); assert_eq!( "about 2 hours", - naive_format_distance(date, base_date, false, false) + format_distance(date, base_date.to_naive(), false, false) ); } #[test] fn test_naive_format_distance_with_suffix() { - let date = - NaiveDateTime::from_timestamp_opt(9600, 0).expect("Invalid NaiveDateTime for date"); - let base_date = - NaiveDateTime::from_timestamp_opt(0, 0).expect("Invalid NaiveDateTime for base_date"); + let date = DateTimeType::Naive( + NaiveDateTime::from_timestamp_opt(9600, 0).expect("Invalid NaiveDateTime for date"), + ); + let base_date = DateTimeType::Naive( + NaiveDateTime::from_timestamp_opt(0, 0).expect("Invalid NaiveDateTime for base_date"), + ); assert_eq!( "about 2 hours from now", - naive_format_distance(date, base_date, false, true) + format_distance(date, base_date.to_naive(), false, true) ); } #[test] fn test_naive_format_distance_from_now() { - let date = NaiveDateTime::parse_from_str("1969-07-20T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ") - .expect("Invalid NaiveDateTime for date"); + let date = DateTimeType::Naive( + NaiveDateTime::parse_from_str("1969-07-20T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ") + .expect("Invalid NaiveDateTime for date"), + ); assert_eq!( "over 54 years ago", - naive_format_distance_from_now(date, false, true) + format_distance_from_now(date, false, true) ); }