Skip to content

Commit

Permalink
Implement Duration::round and some general updates/fixes (#24)
Browse files Browse the repository at this point in the history
* General Duration updates and cleanup

* Initial impl of methods complete

* General work completed - minus test bug

* Finish work, add tests

* Add tests for ceil and expand

* cargo clippy

* Apply review and some API cleanup
  • Loading branch information
nekevss authored Feb 25, 2024
1 parent e3b1191 commit b1093da
Show file tree
Hide file tree
Showing 13 changed files with 1,418 additions and 927 deletions.
13 changes: 10 additions & 3 deletions src/components/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,16 +493,19 @@ impl<C: CalendarProtocol> CalendarSlot<C> {
) -> TemporalResult<Date<C>> {
match self {
CalendarSlot::Builtin(AnyCalendar::Iso(_)) => {
// 8. Let norm be NormalizeTimeDuration(duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]).
// 8. Let norm be NormalizeTimeDuration(duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]],
// duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]).
// 9. Let balanceResult be BalanceTimeDuration(norm, "day").
let (balance_days, _) = TimeDuration::from_normalized(
duration.time().to_normalized(),
TemporalUnit::Day,
)?;
// 10. Let result be ? AddISODate(date.[[ISOYear]], date.[[ISOMonth]], date.[[ISODay]], duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]] + balanceResult.[[Days]], overflow).

// 10. Let result be ? AddISODate(date.[[ISOYear]], date.[[ISOMonth]], date.[[ISODay]], duration.[[Years]],
// duration.[[Months]], duration.[[Weeks]], duration.[[Days]] + balanceResult.[[Days]], overflow).
let result = date.iso.add_iso_date(
&DateDuration::new_unchecked(
duration.days(),
duration.years(),
duration.months(),
duration.weeks(),
duration.days() + balance_days,
Expand Down Expand Up @@ -536,6 +539,10 @@ impl<C: CalendarProtocol> CalendarSlot<C> {
context: &mut C::Context,
) -> TemporalResult<Duration> {
match self {
CalendarSlot::Builtin(AnyCalendar::Iso(_)) => {
let date_duration = one.iso.diff_iso_date(&two.iso, largest_unit)?;
Ok(Duration::from_date_duration(&date_duration))
}
CalendarSlot::Builtin(_) => {
Err(TemporalError::range().with_message("Not yet implemented."))
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ impl<C: CalendarProtocol> Date<C> {
) -> TemporalResult<Self> {
// 1. If options is not present, set options to undefined.
// 2. If duration.[[Years]] ≠ 0, or duration.[[Months]] ≠ 0, or duration.[[Weeks]] ≠ 0, then
if duration.date().years() != 0.0
|| duration.date().months() != 0.0
|| duration.date().weeks() != 0.0
if duration.date().years != 0.0
|| duration.date().months != 0.0
|| duration.date().weeks != 0.0
{
// a. If dateAdd is not present, then
// i. Set dateAdd to unused.
Expand Down Expand Up @@ -382,7 +382,7 @@ impl<C: CalendarProtocol> Date<C> {

if largest_unit == TemporalUnit::Day {
let days = self.days_until(other);
return Ok(Duration::from_date_duration(DateDuration::new(
return Ok(Duration::from_date_duration(&DateDuration::new(
0f64,
0f64,
0f64,
Expand Down
Loading

0 comments on commit b1093da

Please sign in to comment.