mirror of
https://github.com/typst/typst
synced 2025-06-22 21:32:52 +08:00
Add Duration::decompose
This commit is contained in:
parent
fee6844045
commit
3b35f0cecf
@ -16,6 +16,21 @@ impl Duration {
|
|||||||
pub fn is_zero(&self) -> bool {
|
pub fn is_zero(&self) -> bool {
|
||||||
self.0.is_zero()
|
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]
|
#[scope]
|
||||||
@ -118,34 +133,25 @@ impl Debug for Duration {
|
|||||||
|
|
||||||
impl Repr for Duration {
|
impl Repr for Duration {
|
||||||
fn repr(&self) -> EcoString {
|
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 mut vec = Vec::with_capacity(5);
|
||||||
|
|
||||||
let weeks = tmp.whole_seconds() / 604_800.0 as i64;
|
|
||||||
if weeks != 0 {
|
if weeks != 0 {
|
||||||
vec.push(eco_format!("weeks: {}", weeks.repr()));
|
vec.push(eco_format!("weeks: {}", weeks.repr()));
|
||||||
}
|
}
|
||||||
tmp -= weeks.weeks();
|
|
||||||
|
|
||||||
let days = tmp.whole_days();
|
|
||||||
if days != 0 {
|
if days != 0 {
|
||||||
vec.push(eco_format!("days: {}", days.repr()));
|
vec.push(eco_format!("days: {}", days.repr()));
|
||||||
}
|
}
|
||||||
tmp -= days.days();
|
|
||||||
|
|
||||||
let hours = tmp.whole_hours();
|
|
||||||
if hours != 0 {
|
if hours != 0 {
|
||||||
vec.push(eco_format!("hours: {}", hours.repr()));
|
vec.push(eco_format!("hours: {}", hours.repr()));
|
||||||
}
|
}
|
||||||
tmp -= hours.hours();
|
|
||||||
|
|
||||||
let minutes = tmp.whole_minutes();
|
|
||||||
if minutes != 0 {
|
if minutes != 0 {
|
||||||
vec.push(eco_format!("minutes: {}", minutes.repr()));
|
vec.push(eco_format!("minutes: {}", minutes.repr()));
|
||||||
}
|
}
|
||||||
tmp -= minutes.minutes();
|
|
||||||
|
|
||||||
let seconds = tmp.whole_seconds();
|
|
||||||
if seconds != 0 {
|
if seconds != 0 {
|
||||||
vec.push(eco_format!("seconds: {}", seconds.repr()));
|
vec.push(eco_format!("seconds: {}", seconds.repr()));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user