Skip to content

Commit

Permalink
return to the faster-performing version
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Nov 8, 2024
1 parent 9d99e1e commit d9afa20
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions crates/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,13 +1019,12 @@ pub fn fmt_duration_string(mut v: i64, unit: TimeUnit, iso: bool) -> String {
if v < 0 {
// negative sign before "P" indicates that the entire ISO duration is negative.
// the Polars version applies a negative sign to each *individual* part.
write!(s, "-P").unwrap();
s.push_str("-P");
v = v.abs()
} else {
write!(s, "P").unwrap();
s.push('P');
}
};

// iterate over dtype-specific sizes to appropriately scale
// and extract 'days', 'hours', 'minutes', and 'seconds' parts.
for (i, &size) in sizes.iter().enumerate() {
Expand All @@ -1035,63 +1034,70 @@ pub fn fmt_duration_string(mut v: i64, unit: TimeUnit, iso: bool) -> String {
(v % sizes[i - 1]) / size
};
if whole_num != 0 || (iso && i == 3) {
write!(s, "{}", buffer.format(whole_num)).unwrap();
s.push_str(buffer.format(whole_num));
if iso {
// (index 3 => 'seconds' part): the ISO version writes
// fractional seconds, not nano/micro/milliseconds
// fractional seconds, not integer nano/micro/milliseconds.
if i == 3 {
let secs = match unit {
TimeUnit::Nanoseconds => format!(".{:09}", v % size),
TimeUnit::Microseconds => format!(".{:06}", v % size),
TimeUnit::Milliseconds => format!(".{:03}", v % size),
};
write!(s, "{}", secs.trim_end_matches('0')).unwrap();
s.push_str(secs.trim_end_matches('0'));
}
write!(s, "{}", ISO_DURATION_PARTS[i]).unwrap();
s.push_str(ISO_DURATION_PARTS[i]);

// (index 0 => 'days' part): after writing days above (if non-zero)
// the ISO duration string requires a `T` before the time part
// the ISO duration string requires a `T` before the time part.
if i == 0 {
write!(s, "T").unwrap();
s.push('T');
}
} else {
write!(s, "{}", DURATION_PARTS[i]).unwrap();
s.push_str(DURATION_PARTS[i]);
if v % size != 0 {
write!(s, " ").unwrap();
s.push(' ');
}
}
} else if iso && i == 0 {
write!(s, "T").unwrap();
// always need to write the `T` separator for ISO
// durations, even if there is no 'days' part.
s.push('T');
}
}
if iso {
// ISO version has already written fractional seconds (if non-zero)
// ISO version has already written out the fractional seconds (if non-zero).
if s.ends_with('T') {
s.pop();
}
} else {
// Polars version writes out fractional seconds separately as
// integer nano/micro/millseconds
// Polars version writes out fractional seconds as integer nano/micro/milliseconds.
match unit {
TimeUnit::Nanoseconds => {
if v % 1000 != 0 {
write!(s, "{}ns", buffer.format(v % 1_000_000_000)).unwrap();
s.push_str(buffer.format(v % 1_000_000_000));
s.push_str("ns");
} else if v % 1_000_000 != 0 {
write!(s, "{}µs", buffer.format((v % 1_000_000_000) / 1000)).unwrap();
s.push_str(buffer.format((v % 1_000_000_000) / 1000));
s.push_str("µs");
} else if v % 1_000_000_000 != 0 {
write!(s, "{}ms", buffer.format((v % 1_000_000_000) / 1_000_000)).unwrap();
s.push_str(buffer.format((v % 1_000_000_000) / 1_000_000));
s.push_str("ms");
}
},
TimeUnit::Microseconds => {
if v % 1000 != 0 {
write!(s, "{}µs", buffer.format(v % 1_000_000)).unwrap();
s.push_str(buffer.format(v % 1_000_000));
s.push_str("µs");
} else if v % 1_000_000 != 0 {
write!(s, "{}ms", buffer.format((v % 1_000_000) / 1_000)).unwrap();
s.push_str(buffer.format((v % 1_000_000) / 1_000));
s.push_str("ms");
}
},
TimeUnit::Milliseconds => {
if v % 1000 != 0 {
write!(s, "{}ms", buffer.format(v % 1_000)).unwrap();
s.push_str(buffer.format(v % 1_000));
s.push_str("ms");
}
},
}
Expand Down

0 comments on commit d9afa20

Please sign in to comment.