diff --git a/crates/typst-library/src/foundations/duration.rs b/crates/typst-library/src/foundations/duration.rs index 94d44fb2a..90685fa34 100644 --- a/crates/typst-library/src/foundations/duration.rs +++ b/crates/typst-library/src/foundations/duration.rs @@ -16,6 +16,21 @@ impl Duration { pub fn is_zero(&self) -> bool { self.0.is_zero() } + + /// Decomposes the time into whole weeks, days, hours, minutes, and seconds. + pub fn decompose(&self) -> [i64; 5] { + let mut tmp = self.0; + let weeks = tmp.whole_weeks(); + tmp -= weeks.weeks(); + let days = tmp.whole_days(); + tmp -= days.days(); + let hours = tmp.whole_hours(); + tmp -= hours.hours(); + let minutes = tmp.whole_minutes(); + tmp -= minutes.minutes(); + let seconds = tmp.whole_seconds(); + [weeks, days, hours, minutes, seconds] + } } #[scope] @@ -118,34 +133,25 @@ impl Debug for Duration { impl Repr for Duration { fn repr(&self) -> EcoString { - let mut tmp = self.0; + let [weeks, days, hours, minutes, seconds] = self.decompose(); let mut vec = Vec::with_capacity(5); - let weeks = tmp.whole_seconds() / 604_800.0 as i64; if weeks != 0 { vec.push(eco_format!("weeks: {}", weeks.repr())); } - tmp -= weeks.weeks(); - let days = tmp.whole_days(); if days != 0 { vec.push(eco_format!("days: {}", days.repr())); } - tmp -= days.days(); - let hours = tmp.whole_hours(); if hours != 0 { vec.push(eco_format!("hours: {}", hours.repr())); } - tmp -= hours.hours(); - let minutes = tmp.whole_minutes(); if minutes != 0 { vec.push(eco_format!("minutes: {}", minutes.repr())); } - tmp -= minutes.minutes(); - let seconds = tmp.whole_seconds(); if seconds != 0 { vec.push(eco_format!("seconds: {}", seconds.repr())); }