Skip to content

Commit

Permalink
Update DateDuration::round
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Feb 15, 2024
1 parent b2fbf49 commit 44d0f25
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/components/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ impl Duration {
match unit {
TemporalUnit::Year | TemporalUnit::Month | TemporalUnit::Week | TemporalUnit::Day => {
let round_result = self.date().round(
Some(self.time), // TODO: Adjust to NormalizedTimeDuration.
Some(self.time.to_normalized()),
increment,
unit,
rounding_mode,
Expand Down
30 changes: 14 additions & 16 deletions src/components/duration/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::{
utils, TemporalError, TemporalResult, NS_PER_DAY,
};

use super::normalized::NormalizedTimeDuration;

/// `DateDuration` represents the [date duration record][spec] of the `Duration.`
///
/// These fields are laid out in the [Temporal Proposal][field spec] as 64-bit floating point numbers.
Expand Down Expand Up @@ -155,7 +157,7 @@ impl DateDuration {
#[allow(clippy::type_complexity, clippy::let_and_return)]
pub fn round<C: CalendarProtocol, Z: TzProtocol>(
&self,
additional_time: Option<TimeDuration>,
normalized_time: Option<NormalizedTimeDuration>,
increment: u64,
unit: TemporalUnit,
rounding_mode: TemporalRoundingMode,
Expand Down Expand Up @@ -184,23 +186,19 @@ impl DateDuration {
}
// 5. If unit is one of "year", "month", "week", or "day", then
TemporalUnit::Year | TemporalUnit::Month | TemporalUnit::Week | TemporalUnit::Day => {
// a. Let nanoseconds be TotalDurationNanoseconds(hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
let nanoseconds = additional_time.unwrap_or_default().as_nanos();

// b. If zonedRelativeTo is not undefined, then
// i. Let intermediate be ? MoveRelativeZonedDateTime(zonedRelativeTo, years, months, weeks, days, precalculatedPlainDateTime).
// ii. Let result be ? NanosecondsToDays(nanoseconds, intermediate).
// iii. Let fractionalDays be days + result.[[Days]] + result.[[Nanoseconds]] / result.[[DayLength]].
// c. Else,
// i. Let fractionalDays be days + nanoseconds / nsPerDay.
// d. Set days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds to 0.
// e. Assert: fractionalSeconds is not used below.
if zoned_relative_to.is_none() {
self.days + nanoseconds / NS_PER_DAY as f64
// a. If zonedRelativeTo is not undefined, then
if let Some(_zoned_relative) = zoned_relative_to {
// TODO:
// i. Let intermediate be ? MoveRelativeZonedDateTime(zonedRelativeTo, calendarRec, timeZoneRec, years, months, weeks, days, precalculatedPlainDateTime).
// ii. Let result be ? NormalizedTimeDurationToDays(norm, intermediate, timeZoneRec).
// iii. Let fractionalDays be days + result.[[Days]] + DivideNormalizedTimeDuration(result.[[Remainder]], result.[[DayLength]]).
return Err(TemporalError::general("Not yet implemented."));
// b. Else,
} else {
// implementation of b: i-iii needed.
return Err(TemporalError::range().with_message("Not yet implemented."));
// i. Let fractionalDays be days + DivideNormalizedTimeDuration(norm, nsPerDay).
self.days + normalized_time.unwrap_or_default().divide(NS_PER_DAY) as f64
}
// c. Set days to 0.
}
_ => {
return Err(TemporalError::range()
Expand Down
11 changes: 0 additions & 11 deletions src/components/duration/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@ impl TimeDuration {
nanoseconds,
}
}

/// Returns the current `TimeDuration` as nanoseconds.
#[inline]
pub(crate) fn as_nanos(&self) -> f64 {
self.hours
.mul_add(60_f64, self.minutes)
.mul_add(60_f64, self.seconds)
.mul_add(1_000_f64, self.milliseconds)
.mul_add(1_000_f64, self.microseconds)
.mul_add(1_000_f64, self.nanoseconds)
}
}

// ==== TimeDuration's public API ====
Expand Down

0 comments on commit 44d0f25

Please sign in to comment.