Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blank and negated + small adjustments #17

Merged
merged 3 commits into from
Feb 8, 2024
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
41 changes: 30 additions & 11 deletions src/components/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ impl Duration {

// 4. Let sign be ! DurationSign(years, months, weeks, days, 0, 0, 0, 0, 0, 0).
// 5. Assert: sign ≠ 0.
let sign = f64::from(self.duration_sign());
let sign = f64::from(self.sign());

// 6. Let oneYear be ! CreateTemporalDuration(sign, 0, 0, 0, 0, 0, 0, 0, 0, 0).
let one_year = Self::one_year(sign);
Expand Down Expand Up @@ -604,7 +604,7 @@ impl Duration {

// 5. Let sign be ! DurationSign(years, months, weeks, days, 0, 0, 0, 0, 0, 0).
// 6. Assert: sign ≠ 0.
let sign = f64::from(self.duration_sign());
let sign = f64::from(self.sign());

// 7. Let oneYear be ! CreateTemporalDuration(sign, 0, 0, 0, 0, 0, 0, 0, 0, 0).
let one_year = Self::one_year(sign);
Expand Down Expand Up @@ -857,23 +857,40 @@ impl Duration {
// ==== Public Duration methods ====

impl Duration {
/// Returns the absolute value of `Duration`.
/// Determines the sign for the current self.
#[inline]
#[must_use]
pub fn abs(&self) -> Self {
pub fn sign(&self) -> i32 {
duration_sign(&self.iter().collect())
}

/// Returns whether the current `Duration` is zero.
///
/// Equivalant to `Temporal.Duration.blank()`.
#[inline]
#[must_use]
pub fn is_zero(&self) -> bool {
duration_sign(&self.iter().collect()) == 0
}

/// Returns a negated `Duration`
#[inline]
#[must_use]
pub fn negated(&self) -> Self {
Self {
date: self.date().abs(),
time: self.time().abs(),
date: self.date().negated(),
time: self.time().negated(),
}
}

/// 7.5.10 `DurationSign ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds )`
///
/// Determines the sign for the current self.
/// Returns the absolute value of `Duration`.
#[inline]
#[must_use]
pub fn duration_sign(&self) -> i32 {
duration_sign(&self.into_iter().collect())
pub fn abs(&self) -> Self {
Self {
date: self.date().abs(),
time: self.time().abs(),
}
}

/// 7.5.12 `DefaultTemporalLargestUnit ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds )`
Expand Down Expand Up @@ -943,6 +960,8 @@ pub(crate) fn is_valid_duration(set: &Vec<f64>) -> bool {
}

/// Utility function for determining the sign for the current set of `Duration` fields.
///
/// Equivalent: 7.5.10 `DurationSign ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds )`
#[inline]
#[must_use]
fn duration_sign(set: &Vec<f64>) -> i32 {
Expand Down
12 changes: 12 additions & 0 deletions src/components/duration/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ impl DateDuration {
}
}

/// Returns a negated `DateDuration`.
#[inline]
#[must_use]
pub fn negated(&self) -> Self {
Self {
years: self.years * -1.0,
months: self.months * -1.0,
weeks: self.weeks * -1.0,
days: self.days * -1.0,
}
}

/// Returns a new `DateDuration` representing the absolute value of the current.
#[inline]
#[must_use]
Expand Down
2 changes: 1 addition & 1 deletion src/components/duration/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl TimeDuration {
/// Returns a negated `TimeDuration`.
#[inline]
#[must_use]
pub fn neg(&self) -> Self {
pub fn negated(&self) -> Self {
Self {
hours: self.hours * -1f64,
minutes: self.minutes * -1f64,
Expand Down
2 changes: 1 addition & 1 deletion src/components/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Instant {
/// Subtracts a `TimeDuration` to `Instant`.
#[inline]
pub fn subtract_time_duration(&self, duration: &TimeDuration) -> TemporalResult<Self> {
self.add_to_instant(&duration.neg())
self.add_to_instant(&duration.negated())
}

/// Returns a `TimeDuration` representing the duration since provided `Instant`
Expand Down
2 changes: 1 addition & 1 deletion src/components/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Time {
#[inline]
#[must_use]
pub fn subtract_time_duration(&self, duration: &TimeDuration) -> Self {
self.add_to_time(&duration.neg())
self.add_to_time(&duration.negated())
}

// TODO (nekevss): optimize and test rounding_increment type (f64 vs. u64).
Expand Down