Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,14 @@ pub struct Duration {
pub fn Duration::abs(Self) -> Self
pub fn Duration::as_days(Self) -> Int64
pub fn Duration::as_hours(Self) -> Int64
pub fn Duration::as_hours_f64(Self) -> Double
pub fn Duration::as_microseconds(Self) -> Int64
pub fn Duration::as_milliseconds(Self) -> Int64
pub fn Duration::as_minutes(Self) -> Int64
pub fn Duration::as_minutes_f64(Self) -> Double
pub fn Duration::as_nanoseconds(Self) -> Int64
pub fn Duration::as_seconds(Self) -> Int64
pub fn Duration::as_seconds_f64(Self) -> Double
pub fn Duration::as_weeks(Self) -> Int64
pub fn Duration::checked_add(Self, Self) -> Self?
pub fn Duration::checked_multiply(Self, Int64) -> Self?
Expand Down
27 changes: 27 additions & 0 deletions src/tempo.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1916,18 +1916,45 @@ pub fn Duration::as_seconds(self : Duration) -> Int64 {
self.nanoseconds / ns_per_sec
}

///|
/// Total seconds in this duration as a `Double`.
///
/// `Double` has a ~53-bit mantissa, so magnitudes beyond ~2^53 nanoseconds
/// (~104 days) lose sub-unit precision.
pub fn Duration::as_seconds_f64(self : Duration) -> Double {
self.nanoseconds.to_double() / ns_per_sec.to_double()
}
Comment thread
brickfrog marked this conversation as resolved.

///|
/// Total whole minutes in this duration (truncated toward zero).
pub fn Duration::as_minutes(self : Duration) -> Int64 {
self.nanoseconds / ns_per_min
}

///|
/// Total minutes in this duration as a `Double`.
///
/// `Double` has a ~53-bit mantissa, so magnitudes beyond ~2^53 nanoseconds
/// (~104 days) lose sub-unit precision.
pub fn Duration::as_minutes_f64(self : Duration) -> Double {
self.nanoseconds.to_double() / ns_per_min.to_double()
}
Comment thread
brickfrog marked this conversation as resolved.

///|
/// Total whole hours in this duration (truncated toward zero).
pub fn Duration::as_hours(self : Duration) -> Int64 {
self.nanoseconds / ns_per_hour
}

///|
/// Total hours in this duration as a `Double`.
///
/// `Double` has a ~53-bit mantissa, so magnitudes beyond ~2^53 nanoseconds
/// (~104 days) lose sub-unit precision.
pub fn Duration::as_hours_f64(self : Duration) -> Double {
self.nanoseconds.to_double() / ns_per_hour.to_double()
}
Comment thread
brickfrog marked this conversation as resolved.

///|
/// Total whole days in this duration (truncated toward zero).
pub fn Duration::as_days(self : Duration) -> Int64 {
Expand Down
9 changes: 9 additions & 0 deletions src/tempo_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,15 @@ test "Duration::hours" {
assert_eq(d.as_minutes(), 60L)
}

///|
test "Duration fractional total accessors" {
assert_eq(@tempo.Duration::milliseconds(1500L).as_seconds_f64(), 1.5)
assert_eq(@tempo.Duration::minutes(90L).as_hours_f64(), 1.5)
assert_eq(@tempo.Duration::seconds(30L).as_minutes_f64(), 0.5)
assert_eq(@tempo.Duration::milliseconds(-1500L).as_seconds_f64(), -1.5)
assert_eq(@tempo.Duration::seconds(0L).as_hours_f64(), 0.0)
}

///|
test "Duration op_add" {
let a = @tempo.Duration::seconds(30L)
Expand Down