polars_core/datatypes/
time_unit.rs1use super::*;
2
3#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Hash)]
4#[cfg_attr(
5 any(feature = "serde-lazy", feature = "serde"),
6 derive(Serialize, Deserialize)
7)]
8pub enum TimeUnit {
9 Nanoseconds,
10 Microseconds,
11 Milliseconds,
12}
13
14impl From<&ArrowTimeUnit> for TimeUnit {
15 fn from(tu: &ArrowTimeUnit) -> Self {
16 match tu {
17 ArrowTimeUnit::Nanosecond => TimeUnit::Nanoseconds,
18 ArrowTimeUnit::Microsecond => TimeUnit::Microseconds,
19 ArrowTimeUnit::Millisecond => TimeUnit::Milliseconds,
20 ArrowTimeUnit::Second => TimeUnit::Milliseconds,
22 }
23 }
24}
25
26impl Display for TimeUnit {
27 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28 match self {
29 TimeUnit::Nanoseconds => {
30 write!(f, "ns")
31 },
32 TimeUnit::Microseconds => {
33 write!(f, "μs")
34 },
35 TimeUnit::Milliseconds => {
36 write!(f, "ms")
37 },
38 }
39 }
40}
41
42impl TimeUnit {
43 pub fn to_ascii(self) -> &'static str {
44 use TimeUnit::*;
45 match self {
46 Nanoseconds => "ns",
47 Microseconds => "us",
48 Milliseconds => "ms",
49 }
50 }
51
52 pub fn to_arrow(self) -> ArrowTimeUnit {
53 match self {
54 TimeUnit::Nanoseconds => ArrowTimeUnit::Nanosecond,
55 TimeUnit::Microseconds => ArrowTimeUnit::Microsecond,
56 TimeUnit::Milliseconds => ArrowTimeUnit::Millisecond,
57 }
58 }
59}
60
61#[cfg(any(feature = "rows", feature = "object"))]
62#[cfg(any(feature = "dtype-datetime", feature = "dtype-duration"))]
63#[inline]
64pub(crate) fn convert_time_units(v: i64, tu_l: TimeUnit, tu_r: TimeUnit) -> i64 {
65 use TimeUnit::*;
66 match (tu_l, tu_r) {
67 (Nanoseconds, Microseconds) => v / 1_000,
68 (Nanoseconds, Milliseconds) => v / 1_000_000,
69 (Microseconds, Nanoseconds) => v * 1_000,
70 (Microseconds, Milliseconds) => v / 1_000,
71 (Milliseconds, Microseconds) => v * 1_000,
72 (Milliseconds, Nanoseconds) => v * 1_000_000,
73 _ => v,
74 }
75}