From 2e516dac89b74854a3b9e7cf44c982da96f4555d Mon Sep 17 00:00:00 2001 From: Pavel Marek Date: Wed, 26 Oct 2022 16:31:59 +0200 Subject: [PATCH] Add ensure_duration --- .../0.0.0-dev/src/Data/Time/Duration.enso | 41 ++++++++++--------- .../Base/0.0.0-dev/src/Data/Time/Period.enso | 37 +++++++++-------- test/Tests/src/Data/Time/Duration_Spec.enso | 2 + 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Duration.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Duration.enso index 033f6db9421d..1d3911d196a7 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Duration.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Duration.enso @@ -90,6 +90,15 @@ time_execution ~function = duration = new nanoseconds=(end - start) Pair_Data duration result +## PRIVATE +ensure_duration : Any -> Suspend (Any -> Any) -> Any ! (Time_Error | Illegal_Argument_Error) +ensure_duration object ~action = + case object of + _ : Duration -> action + _ : Period.Period -> Error.throw (Time_Error_Data "Cannot use Period as a parameter") + x -> + Error.throw Illegal_Argument_Error_Data <| + "Expected Duration type, got: " + (Meta.get_qualified_type_name x) @Builtin_Type type Duration @@ -114,14 +123,9 @@ type Duration example_add = (Duration.minutes 30) + (Duration.hours 12) + : Duration -> Duration ! Time_Error + self that = - case that of - _ : Duration -> - Panic.catch ArithmeticException (self.plus_builtin that) err-> - Error.throw (Time_Error_Data err.getMessage) - _ : Period.Period -> - Error.throw (Time_Error_Data "Adding Period to Duration is not supported") - _ -> - Error.throw (Illegal_Argument_Error_Data "Illegal argument `that`") + ensure_duration that <| + Panic.catch ArithmeticException (self.plus_builtin that) err-> + Error.throw (Time_Error_Data err.getMessage) ## Subtract the specified amount of time from this duration. @@ -136,14 +140,9 @@ type Duration example_subtract = (Duration.hours 6) - (Duration.minutes 30) - : Duration -> Duration ! Time_Error - self that = - case that of - _ : Duration -> - Panic.catch ArithmeticException (self.minus_builtin that) err-> - Error.throw (Time_Error_Data err.getMessage) - _ : Period.Period -> - Error.throw Time_Error_Data "Subtracting Period from Duration is not supported" - _ -> - Error.throw Illegal_State_Error_Data "Illegal argument `that`" + ensure_duration that <| + Panic.catch ArithmeticException (self.minus_builtin that) err-> + Error.throw (Time_Error_Data err.getMessage) ## Check two durations for equality. @@ -156,8 +155,10 @@ type Duration import Standard.Base.Data.Time.Duration example_eq = (Duration.seconds 60).total_minutes == (Duration.minutes 1).total_minutes - == : Duration -> Boolean - == self that = self.equals_builtin that + == : Duration -> Boolean ! Time_Error + == self that = + ensure_duration that <| + self.equals_builtin that ## Compares `self` to `that` to produce an ordering. @@ -174,7 +175,9 @@ type Duration duration_2 = (Duration.minutes 60) + (Duration.minutes 5) duration_1.compare_to duration_2 compare_to : Duration -> Ordering - compare_to self that = Ordering.from_sign (self.compare_to_builtin that) + compare_to self that = + ensure_duration that <| + Ordering.from_sign (self.compare_to_builtin that) ## Get the portion of the duration expressed in nanoseconds. diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Period.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Period.enso index 88c56e557072..71ba7a923343 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Period.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Period.enso @@ -58,7 +58,7 @@ ensure_period object ~action error_msg="Cannot use Duration as a parameter" = case object of _ : Period -> action _ : Duration.Duration -> - Error.throw Time_Error_Data error_msg + Error.throw (Time_Error_Data error_msg) x -> Error.throw Illegal_Argument_Error_Data <| "Expected Period type, got: " + (Meta.get_qualified_type_name x) @@ -156,20 +156,21 @@ type Period ## Compare this period to another period to produce an ordering. compare_to : Period -> Ordering ! (Time_Error | Illegal_Argument_Error) compare_to self that = - case self of - Period.Period_With_Date _ start_date_self -> - case that of - Period.Period_With_Date _ start_date_that -> - end_date_self = start_date_self + self - duration_self = Duration.between start_date_self.to_date_time end_date_self.to_date_time - end_date_that = start_date_that + that - duration_that = Duration.between start_date_that.to_date_time end_date_that.to_date_time - duration_self.compare_to duration_that - Period.Period_Without_Date _ -> - (start_date_self + self).compare_to (start_date_self + that) - Period.Period_Without_Date _ -> - case that of - Period.Period_With_Date _ start_date_that -> - (start_date_that + self).compare_to (start_date_that + that) - Period.Period_Without_Date _ -> - Error.throw (Time_Error_Data "Cannot compare two Period_Without_Date, as there is no start date") + ensure_period that <| + case self of + Period.Period_With_Date _ start_date_self -> + case that of + Period.Period_With_Date _ start_date_that -> + end_date_self = start_date_self + self + duration_self = Duration.between start_date_self.to_date_time end_date_self.to_date_time + end_date_that = start_date_that + that + duration_that = Duration.between start_date_that.to_date_time end_date_that.to_date_time + duration_self.compare_to duration_that + Period.Period_Without_Date _ -> + (start_date_self + self).compare_to (start_date_self + that) + Period.Period_Without_Date _ -> + case that of + Period.Period_With_Date _ start_date_that -> + (start_date_that + self).compare_to (start_date_that + that) + Period.Period_Without_Date _ -> + Error.throw (Time_Error_Data "Cannot compare two Period_Without_Date, as there is no start date") diff --git a/test/Tests/src/Data/Time/Duration_Spec.enso b/test/Tests/src/Data/Time/Duration_Spec.enso index f7f5e0c9e8f1..b146cb0cc7a1 100644 --- a/test/Tests/src/Data/Time/Duration_Spec.enso +++ b/test/Tests/src/Data/Time/Duration_Spec.enso @@ -76,6 +76,8 @@ spec = (period - duration).should_fail_with Time_Error_Data (period > duration).should_fail_with Time_Error_Data (period < duration).should_fail_with Time_Error_Data + (duration > period).should_fail_with Time_Error_Data + (duration < period).should_fail_with Time_Error_Data Test.specify "Date_Time supports adding and subtracting Duration" <| ((Date_Time.new 2022 10 1 hour=10) + (Duration.hours 2)) . should_equal (Date_Time.new 2022 10 1 hour=12)