summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-06-19 17:11:26 +0200
committerLaurenz <laurmaedje@gmail.com>2025-06-20 17:32:37 +0200
commit3b35f0cecf37c00d33334be6a596a105167875dd (patch)
treeb605d578757161c7cd47aebab701d30d868c863e
parentfee6844045e6a898bc65491a8e18cd520b24d08b (diff)
Add `Duration::decompose`
-rw-r--r--crates/typst-library/src/foundations/duration.rs26
1 files changed, 16 insertions, 10 deletions
diff --git a/crates/typst-library/src/foundations/duration.rs b/crates/typst-library/src/foundations/duration.rs
index 94d44fb2..90685fa3 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()));
}