Skip to content

Commit

Permalink
Add shortcut constructors to Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Oct 26, 2022
1 parent 7facd96 commit bd781d1
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ type Date_Time
from Standard.Base import Date_Time
from Standard.Base.Data.Time import Duration, Period

example_plus = Date_Time.new 2020 + (Period.years 15) + (Duration.new hours=3)
example_plus = Date_Time.new 2020 + (Period.years 15) + (Duration.hours 3)
+ : (Duration | Period) -> Date_Time ! Time_Error
+ self amount =
case amount of
Expand Down Expand Up @@ -449,7 +449,7 @@ type Date_Time
from Standard.Base import Date_Time
import Standard.Base.Data.Time.Duration

example_minus = Date_Time.new 2020 - (Period.years 1) - (Period.months 9) - (Duration.new hours=5)
example_minus = Date_Time.new 2020 - (Period.years 1) - (Period.months 9) - (Duration.hours 5)
- : (Duration | Period) -> Date_Time ! Time_Error
- self amount =
case amount of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,33 @@ new : Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Duration
new hours=0 minutes=0 seconds=0 milliseconds=0 nanoseconds=0 =
Duration.new_builtin hours minutes seconds milliseconds nanoseconds

## Create a Duration from hours.
hours : Integer -> Duration
hours h = new hours=h

## Create a Duration from minutes.
minutes : Integer -> Duration
minutes m = new minutes=m

## Create a Duration from seconds.
seconds : Integer -> Duration
seconds s = new seconds=s

## Create a Duration from milliseconds.
milliseconds : Integer -> Duration
milliseconds ms = new milliseconds=ms

## Create a Duration from nanoseconds.
nanoseconds : Integer -> Duration
nanoseconds ns = new nanoseconds=ns

## Create a zero (empty) duration.
> Example
Folding a vector of durations.

import Standard.Base.Data.Time.Duration

durations = [(Duration.new seconds=1), (Duration.new seconds=2), (Duration.new seconds=3)]
durations = [(Duration.seconds 1), (Duration.seconds 2), (Duration.seconds 3)]
example_sum = durations.fold Duration.zero (+)
zero : Duration
zero = new
Expand Down Expand Up @@ -85,14 +104,14 @@ type Duration

import Standard.Base.Data.Time.Duration

example_add = (Duration.new minutes=3) + (Duration.new seconds=6)
example_add = (Duration.minutes 3) + (Duration.seconds 6)

> Example
Add 12 hours to a duration of 30 minutes.

import Standard.Base.Data.Time.Duration

example_add = (Duration.new minutes=30) + (Duration.new hours=12)
example_add = (Duration.minutes 30) + (Duration.hours 12)
+ : Duration -> Duration ! Time_Error
+ self that =
case that of
Expand All @@ -114,7 +133,7 @@ type Duration

import Standard.Base.Data.Time.Duration

example_subtract = (Duration.new hours=6) - (Duration.new minutes=30)
example_subtract = (Duration.hours 6) - (Duration.minutes 30)
- : Duration -> Duration ! Time_Error
- self that =
case that of
Expand All @@ -136,7 +155,7 @@ type Duration

import Standard.Base.Data.Time.Duration

example_eq = (Duration.new seconds=60).total_minutes == (Duration.new minutes=1).total_minutes
example_eq = (Duration.seconds 60).total_minutes == (Duration.minutes 1).total_minutes
== : Duration -> Boolean
== self that = self.equals_builtin that

Expand All @@ -152,7 +171,7 @@ type Duration

example_compare_to =
duration_1 = (Duration.new hour=1)
duration_2 = (Duration.new minutes=60) + (Duration.new minutes=5)
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)
Expand Down Expand Up @@ -251,7 +270,7 @@ type Duration

import Standard.Base.Data.Time.Duration

example_to_vec = (Duration.new nanoseconds=800)).to_vector
example_to_vec = (Duration.nanoseconds 800)).to_vector
to_vector : Vector.Vector Integer
to_vector self = [self.hours, self.minutes, self.seconds, self.milliseconds, self.nanoseconds]

Expand All @@ -262,7 +281,7 @@ type Duration

import Standard.Base.Data.Time.Duration

example_to_json = (Duration.new seconds=10).to_json
example_to_json = (Duration.seconds 10).to_json
to_json : Json.Object
to_json self =
b = Vector.new_builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ type Time_Of_Day
from Standard.Base import Time_Of_Day
import Standard.Base.Data.Time.Duration

example_plus = Time_Of_Day.new + (Duration.new seconds=3)
example_plus = Time_Of_Day.new + (Duration.seconds 3)
+ : Duration -> Time_Of_Day
+ self amount =
case amount of
Expand All @@ -236,7 +236,7 @@ type Time_Of_Day
from Standard.Base import Time_Of_Day
import Standard.Base.Data.Time.Duration

example_minus = Time_Of_Day.now - (Duration.new hours=12)
example_minus = Time_Of_Day.now - (Duration.hours 12)
- : Duration -> Time_Of_Day
- self amount = self.minus_builtin amount

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ polyglot java import org.enso.base.Http_Utils

> Example
Create an HTTP client with extended timeout.
Http.new timeout=(Duration.new seconds=30)
Http.new timeout=(Duration.seconds 30)

> Example
Create an HTTP client with extended timeout and proxy settings.
Expand All @@ -46,9 +46,9 @@ polyglot java import org.enso.base.Http_Utils
import Standard.Base.Network.Proxy

example_new =
Http.new (timeout = (Duration.new seconds=30)) (proxy = Proxy.new "example.com" 8080)
Http.new (timeout = (Duration.seconds 30)) (proxy = Proxy.new "example.com" 8080)
new : Duration -> Boolean -> Proxy -> Http
new (timeout = (Duration.new seconds=10)) (follow_redirects = True) (proxy = Proxy.System) (version = Version.Http_1_1) =
new (timeout = (Duration.seconds 10)) (follow_redirects = True) (proxy = Proxy.System) (version = Version.Http_1_1) =
Http_Data timeout follow_redirects proxy version

## Send an Options request.
Expand Down Expand Up @@ -595,7 +595,7 @@ type Http
example_request =
form = [Form.text_field "name" "John Doe"]
req = Request.new Method.Post "http://httpbin.org/post" . with_form form
http = Http.new (timeout = (Duration.new seconds=30))
http = Http.new (timeout = (Duration.seconds 30))
http.request req
request : Request -> Response ! Request_Error
request self req =
Expand Down
4 changes: 2 additions & 2 deletions test/Tests/src/Data/Time/Date_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ spec_with name create_new_date parse_date =
date . day . should_equal 1

Test.specify "should throw error when adding time-based Duration" <|
case (create_new_date 1970 + (Duration.new hours=1)) . catch of
case (create_new_date 1970 + (Duration.hours 1)) . catch of
Time_Error_Data message ->
message . should_equal "Date does not support adding/subtracting Duration. Use Period instead."
result ->
Test.fail ("Unexpected result: " + result.to_text)

Test.specify "should throw error when subtracting time-based Duration" <|
case (create_new_date 1970 - (Duration.new minutes=1)) . catch of
case (create_new_date 1970 - (Duration.minutes 1)) . catch of
Time_Error_Data message ->
message . should_equal "Date does not support adding/subtracting Duration. Use Period instead."
result ->
Expand Down
16 changes: 8 additions & 8 deletions test/Tests/src/Data/Time/Date_Time_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ spec_with name create_new_datetime parse_datetime nanoseconds_loss_in_precision=
time . day . should_equal 1

Test.specify "should add time interval" <|
time = create_new_datetime 1970 (zone = Time_Zone.utc) + (Duration.new nanoseconds=1)
time = create_new_datetime 1970 (zone = Time_Zone.utc) + (Duration.nanoseconds 1)
time . year . should_equal 1970
time . month . should_equal 1
time . day . should_equal 1
Expand All @@ -250,7 +250,7 @@ spec_with name create_new_datetime parse_datetime nanoseconds_loss_in_precision=
time . zone . zone_id . should_equal Time_Zone.utc.zone_id

Test.specify "should add mixed date time interval" <|
time = create_new_datetime 1970 (zone = Time_Zone.utc) + (Period.months 1) + (Duration.new hours=3)
time = create_new_datetime 1970 (zone = Time_Zone.utc) + (Period.months 1) + (Duration.hours 3)
time . year . should_equal 1970
time . month . should_equal 2
time . day . should_equal 1
Expand All @@ -261,7 +261,7 @@ spec_with name create_new_datetime parse_datetime nanoseconds_loss_in_precision=
time . zone . zone_id . should_equal Time_Zone.utc.zone_id

Test.specify "should subtract time interval" <|
time = create_new_datetime 1970 (zone = Time_Zone.utc) - (Duration.new hours=1)
time = create_new_datetime 1970 (zone = Time_Zone.utc) - (Duration.hours 1)
time . year . should_equal 1969
time . month . should_equal 12
time . day . should_equal 31
Expand All @@ -283,7 +283,7 @@ spec_with name create_new_datetime parse_datetime nanoseconds_loss_in_precision=
time . zone . zone_id . should_equal Time_Zone.utc.zone_id

Test.specify "should subtract mixed date time interval" <|
time = create_new_datetime 1970 (zone = Time_Zone.utc) - (Period.months 1) - (Duration.new hours=3)
time = create_new_datetime 1970 (zone = Time_Zone.utc) - (Period.months 1) - (Duration.hours 3)
time . year . should_equal 1969
time . month . should_equal 11
time . day . should_equal 30
Expand All @@ -294,7 +294,7 @@ spec_with name create_new_datetime parse_datetime nanoseconds_loss_in_precision=
time . zone . zone_id . should_equal Time_Zone.utc.zone_id

Test.specify "should support mixed interval operators" <|
time = create_new_datetime 1970 (zone = Time_Zone.utc) - (Period.months 1) + (Duration.new hours=12)
time = create_new_datetime 1970 (zone = Time_Zone.utc) - (Period.months 1) + (Duration.hours 12)
time . year . should_equal 1969
time . month . should_equal 12
time . day . should_equal 1
Expand Down Expand Up @@ -426,7 +426,7 @@ spec_with name create_new_datetime parse_datetime nanoseconds_loss_in_precision=
Test.specify "should find start/end of a Date_Period or Time_Period containing the current datetime correctly near the spring DST switch" pending=js_dst_pending <|
d1 = create_new_datetime 2022 3 27 1 34 15 0 tz
d2 = create_new_datetime 2022 3 27 3 34 15 0 tz
d1_plus = d1 + (Duration.new hours=1)
d1_plus = d1 + (Duration.hours 1)
d1_plus . should_equal d2

check_dates_spring date =
Expand Down Expand Up @@ -458,7 +458,7 @@ spec_with name create_new_datetime parse_datetime nanoseconds_loss_in_precision=
dst_overlap_message = "We cannot correctly migrate the datetime inside of the timeline overlap through the polyglot boundar - as due to polyglot conversion limitation, always the earlier one is chosen. See the bug report: https://github.com/oracle/graal/issues/4918"
Test.specify "should find start/end of a Date_Period or Time_Period containing the current datetime correctly near the autumn DST switch" pending=dst_overlap_message <|
d3 = create_new_datetime 2022 10 30 2 30 15 0 tz
d4 = d3 + (Duration.new hours=1)
d4 = d3 + (Duration.hours 1)

d3.hour . should_equal 2
d4.hour . should_equal 2
Expand Down Expand Up @@ -532,7 +532,7 @@ spec_with name create_new_datetime parse_datetime nanoseconds_loss_in_precision=

Test.specify "should handle shifting dates around autumn DST edge cases" pending=dst_overlap_message <|
d3 = create_new_datetime 2022 10 30 2 30 15 0 tz
d4 = d3 + (Duration.new hours=1)
d4 = d3 + (Duration.hours 1)

# TODO we need to check and document the actual behaviour once it is expressible, it may be equally acceptable to shift to 3:30 instead of 2:30.
d4 . add_work_days 0 . should_equal (Date_Time.new 2022 10 31 2 30 15 0 tz)
Expand Down
32 changes: 16 additions & 16 deletions test/Tests/src/Data/Time/Duration_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec =
Test.group "Duration" <|

Test.specify "should create interval seconds" <|
duration = (Duration.new seconds=5)
duration = (Duration.seconds 5)
duration.seconds . should_equal 5
duration.milliseconds . should_equal 0

Expand All @@ -39,26 +39,26 @@ spec =
interval.is_empty . should_be_true

Test.specify "should normalize periods" <|
(Duration.new seconds=60).total_minutes . should_equal 1
(Duration.new milliseconds=1000).total_seconds . should_equal 1
(Duration.seconds 60).total_minutes . should_equal 1
(Duration.milliseconds 1000).total_seconds . should_equal 1

Test.specify "should normalize addition" <|
duration = (Duration.new hours=11) + (Duration.new hours=1)
duration = (Duration.hours 11) + (Duration.hours 1)
duration.hours . should_equal 12

Test.specify "should normalize subtraction" <|
duration = (Duration.new hours=13) - (Duration.new hours=1)
duration = (Duration.hours 13) - (Duration.hours 1)
duration.hours . should_equal 12

Test.specify "should convert to Json" <|
interval = (Duration.new nanoseconds=120) + (Duration.new seconds=30) + (Duration.new hours=14)
interval = (Duration.nanoseconds 120) + (Duration.seconds 30) + (Duration.hours 14)
interval.to_json.should_equal <|
duration_pairs = [["nanoseconds", interval.nanoseconds], ["seconds", interval.seconds], ["hours", interval.hours]]
Json.from_pairs ([["type", "Duration"]] + duration_pairs)

Test.specify "should be comparable" <|
duration_1 = (Duration.new hours=5)
duration_2 = (Duration.new minutes=1)
duration_1 = (Duration.hours 5)
duration_2 = (Duration.minutes 1)
duration_1.compare_to duration_1 . should_equal Ordering.Equal
duration_1==duration_1 . should_be_true
duration_1!=duration_2 . should_be_true
Expand All @@ -67,7 +67,7 @@ spec =

Test.specify "should not mix Duration and Period" <|
handler err = err.is_error . should_be_true
durations = [(Duration.new hours=1), (Duration.zero), (Duration.new hours=1 seconds=30)]
durations = [(Duration.hours 1), (Duration.zero), (Duration.new hours=1 seconds=30)]
periods = [(Period.days 1), (Period.new 0), (Period.years 30), (Period.new years=3 months=2)]
durations.each duration->
periods.each period->
Expand All @@ -79,15 +79,15 @@ spec =
(period < duration).should_fail_with Time_Error_Data

Test.specify "Date_Time supports adding and subtracting Duration" <|
((Date_Time.new 2022 10 1 hour=10) + (Duration.new hours=2)) . should_equal (Date_Time.new 2022 10 1 hour=12)
((Date_Time.new 2022 10 1 hour=10) - (Duration.new hours=2)) . should_equal (Date_Time.new 2022 10 1 hour=8)
((Date_Time.new 2022 10 2) - (Duration.new hours=24)) . should_equal (Date_Time.new 2022 10 1)
((Date_Time.new 2022 10 1 hour=2) - (Duration.new minutes=3)) . should_equal (Date_Time.new 2022 10 1 hour=1 minute=57)
((Date_Time.new 2022 10 1 hour=10) + (Duration.hours 2)) . should_equal (Date_Time.new 2022 10 1 hour=12)
((Date_Time.new 2022 10 1 hour=10) - (Duration.hours 2)) . should_equal (Date_Time.new 2022 10 1 hour=8)
((Date_Time.new 2022 10 2) - (Duration.hours 24)) . should_equal (Date_Time.new 2022 10 1)
((Date_Time.new 2022 10 1 hour=2) - (Duration.minutes 3)) . should_equal (Date_Time.new 2022 10 1 hour=1 minute=57)

Test.specify "Java Duration is equal to Enso Duration" <|
(Duration.new hours=1) . should_equal (Java_Duration.ofHours 1)
(Duration.new minutes=80) . should_equal (Java_Duration.ofMinutes 80)
(Java_Duration.ofSeconds 30) . should_equal (Duration.new seconds=30)
(Duration.hours 1) . should_equal (Java_Duration.ofHours 1)
(Duration.minutes 80) . should_equal (Java_Duration.ofMinutes 80)
(Java_Duration.ofSeconds 30) . should_equal (Duration.seconds 30)

Test.specify "Difference of Java Date and Enso date should be an Enso Duration" <|
(Duration.between (java_datetime 2022 01 01) (Date_Time.new 2022 01 02) timezone_aware=False).total_hours . should_equal 24
Expand Down
8 changes: 4 additions & 4 deletions test/Tests/src/Data/Time/Time_Of_Day_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ specWith name create_new_time parse_time =
datetime . zone . zone_id . should_equal Time_Zone.utc.zone_id

Test.specify "should add time-based interval" <|
time = create_new_time 0 + (Duration.new minutes=1)
time = create_new_time 0 + (Duration.minutes 1)
time . to_seconds . should_equal 60

Test.specify "should subtract time-based interval" <|
time = create_new_time 0 - (Duration.new minutes=1)
time = create_new_time 0 - (Duration.minutes 1)
time . to_seconds . should_equal 86340

Test.specify "should support mixed interval operators" <|
time = create_new_time 0 + (Duration.new hours=1) - (Duration.new seconds=1)
time = create_new_time 0 + (Duration.hours 1) - (Duration.seconds 1)
time . to_seconds . should_equal 3599

Test.specify "should throw error when adding date-based interval" <|
Expand All @@ -101,7 +101,7 @@ specWith name create_new_time parse_time =
Test.fail ("Unexpected result: " + result.to_text)

Test.specify "should throw error when subtracting date-based interval" <|
case (create_new_time 0 - ((Period.days 1) - (Duration.new minutes=1))) . catch of
case (create_new_time 0 - ((Period.days 1) - (Duration.minutes 1))) . catch of
Time_Error_Data message ->
message . should_equal "Time_Of_Day does not support date intervals (periods)"
result ->
Expand Down
4 changes: 2 additions & 2 deletions test/Tests/src/Network/Http_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ spec =
url_get = base_url_with_slash + "get"
url_post = base_url_with_slash + "post"
Test.specify "should create HTTP client with timeout setting" <|
http = Http.new (timeout = (Duration.new seconds=30))
http.timeout.should_equal (Duration.new seconds=30)
http = Http.new (timeout = (Duration.seconds 30))
http.timeout.should_equal (Duration.seconds 30)
Test.specify "should create HTTP client with follow_redirects setting" <|
http = Http.new (follow_redirects = False)
http.follow_redirects.should_equal False
Expand Down

0 comments on commit bd781d1

Please sign in to comment.