Skip to content

Commit

Permalink
Add ensure_duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Oct 27, 2022
1 parent 151b01a commit 2e516da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand Down
37 changes: 19 additions & 18 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Period.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
2 changes: 2 additions & 0 deletions test/Tests/src/Data/Time/Duration_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2e516da

Please sign in to comment.