Skip to content

Commit

Permalink
Rebase, cargo fmt, and review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Jul 21, 2024
1 parent 908acb0 commit 3d7face
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
43 changes: 29 additions & 14 deletions src/components/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,10 @@ mod tests {
use crate::{
components::{calendar::Calendar, duration::DateDuration, Duration},
iso::{IsoDate, IsoTime},
options::{DifferenceSettings, RoundingIncrement, RoundingOptions, TemporalRoundingMode, TemporalUnit},
options::{
DifferenceSettings, RoundingIncrement, RoundingOptions, TemporalRoundingMode,
TemporalUnit,
},
primitive::FiniteF64,
};

Expand Down Expand Up @@ -744,33 +747,45 @@ mod tests {
assert_eq!(dt.nanosecond(), expected.8);
};

let gen_rounding_options = | smallest: TemporalUnit, increment: u32 | -> RoundingOptions {
let gen_rounding_options = |smallest: TemporalUnit, increment: u32| -> RoundingOptions {
RoundingOptions {
largest_unit: None,
smallest_unit: Some(smallest),
increment: Some(RoundingIncrement::try_new(increment).unwrap()),
rounding_mode: None,
}

};
let dt = DateTime::new(1976, 11, 18, 14, 23, 30, 123, 456, 789, Calendar::default()).unwrap();
let dt =
DateTime::new(1976, 11, 18, 14, 23, 30, 123, 456, 789, Calendar::default()).unwrap();

let result = dt.round(gen_rounding_options(TemporalUnit::Hour, 4)).unwrap();
let result = dt
.round(gen_rounding_options(TemporalUnit::Hour, 4))
.unwrap();
assert_datetime(result, (1976, 11, 18, 16, 0, 0, 0, 0, 0));

let result = dt.round(gen_rounding_options(TemporalUnit::Minute, 15)).unwrap();
let result = dt
.round(gen_rounding_options(TemporalUnit::Minute, 15))
.unwrap();
assert_datetime(result, (1976, 11, 18, 14, 30, 0, 0, 0, 0));

let result = dt.round(gen_rounding_options(TemporalUnit::Second, 30)).unwrap();

let result = dt
.round(gen_rounding_options(TemporalUnit::Second, 30))
.unwrap();
assert_datetime(result, (1976, 11, 18, 14, 23, 30, 0, 0, 0));

let result = dt.round(gen_rounding_options(TemporalUnit::Millisecond, 10)).unwrap();

let result = dt
.round(gen_rounding_options(TemporalUnit::Millisecond, 10))
.unwrap();
assert_datetime(result, (1976, 11, 18, 14, 23, 30, 120, 0, 0));

let result = dt.round(gen_rounding_options(TemporalUnit::Microsecond, 10)).unwrap();

let result = dt
.round(gen_rounding_options(TemporalUnit::Microsecond, 10))
.unwrap();
assert_datetime(result, (1976, 11, 18, 14, 23, 30, 123, 460, 0));

let result = dt.round(gen_rounding_options(TemporalUnit::Nanosecond, 10)).unwrap();

let result = dt
.round(gen_rounding_options(TemporalUnit::Nanosecond, 10))
.unwrap();
assert_datetime(result, (1976, 11, 18, 14, 23, 30, 123, 456, 790));
}
}
8 changes: 6 additions & 2 deletions src/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ impl IsoDateTime {

pub(crate) fn round(&self, resolved_options: ResolvedRoundingOptions) -> TemporalResult<Self> {
let (rounded_days, rounded_time) = self.time.round(resolved_options)?;
let balance_result = IsoDate::balance(self.date.year, self.date.month.into(), i32::from(self.date.day) + rounded_days);
let balance_result = IsoDate::balance(
self.date.year,
self.date.month.into(),
i32::from(self.date.day) + rounded_days,
);
Self::new(balance_result, rounded_time)
}

Expand Down Expand Up @@ -724,7 +728,7 @@ impl IsoTime {
}
_ => {
return Err(TemporalError::range()
.with_message("Invalid smallestUNit value for time rounding."))
.with_message("Invalid smallestUnit value for time rounding."))
}
};
// 7. Let unitLength be the value in the "Length in Nanoseconds" column of the row of Table 22 whose "Singular" column contains unit.
Expand Down

0 comments on commit 3d7face

Please sign in to comment.