Skip to content

Commit

Permalink
feat[rust]: more compact duration format (for table output) (#4638)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Aug 31, 2022
1 parent 7c6820f commit 434dd8e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 11 additions & 14 deletions polars/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,45 +513,45 @@ const SIZES_NS: [i64; 4] = [
60_000_000_000,
1_000_000_000,
];
const NAMES: [&str; 4] = ["day", "hour", "minute", "second"];
const NAMES: [&str; 4] = ["d", "h", "m", "s"];
const SIZES_US: [i64; 4] = [86_400_000_000, 3_600_000_000, 60_000_000, 1_000_000];
const SIZES_MS: [i64; 4] = [86_400_000, 3_600_000, 60_000, 1_000];

fn fmt_duration_ns(f: &mut Formatter<'_>, v: i64) -> fmt::Result {
if v == 0 {
return write!(f, "0 ns");
return write!(f, "0ns");
}
format_duration(f, v, SIZES_NS.as_slice(), NAMES.as_slice())?;
if v % 1000 != 0 {
write!(f, "{} ns", v % 1_000_000_000)?;
write!(f, "{}ns", v % 1_000_000_000)?;
} else if v % 1_000_000 != 0 {
write!(f, "{} µs", (v % 1_000_000_000) / 1000)?;
write!(f, "{}µs", (v % 1_000_000_000) / 1000)?;
} else if v % 1_000_000_000 != 0 {
write!(f, "{} ms", (v % 1_000_000_000) / 1_000_000)?;
write!(f, "{}ms", (v % 1_000_000_000) / 1_000_000)?;
}
Ok(())
}

fn fmt_duration_us(f: &mut Formatter<'_>, v: i64) -> fmt::Result {
if v == 0 {
return write!(f, "0 µs");
return write!(f, "0µs");
}
format_duration(f, v, SIZES_US.as_slice(), NAMES.as_slice())?;
if v % 1000 != 0 {
write!(f, "{} µs", (v % 1_000_000_000) / 1000)?;
write!(f, "{}µs", (v % 1_000_000_000) / 1000)?;
} else if v % 1_000_000 != 0 {
write!(f, "{} ms", (v % 1_000_000_000) / 1_000_000)?;
write!(f, "{}ms", (v % 1_000_000_000) / 1_000_000)?;
}
Ok(())
}

fn fmt_duration_ms(f: &mut Formatter<'_>, v: i64) -> fmt::Result {
if v == 0 {
return write!(f, "0 ms");
return write!(f, "0ms");
}
format_duration(f, v, SIZES_MS.as_slice(), NAMES.as_slice())?;
if v % 1_000 != 0 {
write!(f, "{} ms", (v % 1_000_000_000) / 1_000_000)?;
write!(f, "{}ms", (v % 1_000_000_000) / 1_000_000)?;
}
Ok(())
}
Expand All @@ -564,10 +564,7 @@ fn format_duration(f: &mut Formatter, v: i64, sizes: &[i64], names: &[&str]) ->
(v % sizes[i - 1]) / sizes[i]
};
if whole_num <= -1 || whole_num >= 1 {
write!(f, "{} {}", whole_num, names[i])?;
if whole_num != 1 {
write!(f, "s")?;
}
write!(f, "{}{}", whole_num, names[i])?;
if v % sizes[i] != 0 {
write!(f, " ")?;
}
Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/internals/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def date_range(
"""
if isinstance(interval, timedelta):
interval = _timedelta_to_pl_duration(interval)
elif " " in interval:
interval = interval.replace(" ", "")

low, low_is_date = _ensure_datetime(low)
high, high_is_date = _ensure_datetime(high)
Expand Down

0 comments on commit 434dd8e

Please sign in to comment.