use arrow::datatypes::TimeUnit as ArrowTimeUnit;
use core::fmt;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy)]
pub struct Time {
pub timestamp: i64,
pub unit: PoSQLTimeUnit,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Deserialize, Serialize, Hash)]
pub enum PoSQLTimeUnit {
Second,
Millisecond,
Microsecond,
Nanosecond,
}
impl From<PoSQLTimeUnit> for ArrowTimeUnit {
fn from(unit: PoSQLTimeUnit) -> Self {
match unit {
PoSQLTimeUnit::Second => ArrowTimeUnit::Second,
PoSQLTimeUnit::Millisecond => ArrowTimeUnit::Millisecond,
PoSQLTimeUnit::Microsecond => ArrowTimeUnit::Microsecond,
PoSQLTimeUnit::Nanosecond => ArrowTimeUnit::Nanosecond,
}
}
}
impl fmt::Display for PoSQLTimeUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PoSQLTimeUnit::Second => write!(f, "Second"),
PoSQLTimeUnit::Millisecond => write!(f, "Millisecond"),
PoSQLTimeUnit::Microsecond => write!(f, "Microsecond"),
PoSQLTimeUnit::Nanosecond => write!(f, "Nanosecond"),
}
}
}
impl From<ArrowTimeUnit> for PoSQLTimeUnit {
fn from(unit: ArrowTimeUnit) -> Self {
match unit {
ArrowTimeUnit::Second => PoSQLTimeUnit::Second,
ArrowTimeUnit::Millisecond => PoSQLTimeUnit::Millisecond,
ArrowTimeUnit::Microsecond => PoSQLTimeUnit::Microsecond,
ArrowTimeUnit::Nanosecond => PoSQLTimeUnit::Nanosecond,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn we_can_convert_from_arrow_time_units() {
assert_eq!(
PoSQLTimeUnit::from(ArrowTimeUnit::Second),
PoSQLTimeUnit::Second
);
assert_eq!(
PoSQLTimeUnit::from(ArrowTimeUnit::Millisecond),
PoSQLTimeUnit::Millisecond
);
assert_eq!(
PoSQLTimeUnit::from(ArrowTimeUnit::Microsecond),
PoSQLTimeUnit::Microsecond
);
assert_eq!(
PoSQLTimeUnit::from(ArrowTimeUnit::Nanosecond),
PoSQLTimeUnit::Nanosecond
);
}
#[test]
fn we_can_convert_to_arrow_time_units() {
assert_eq!(
ArrowTimeUnit::from(PoSQLTimeUnit::Second),
ArrowTimeUnit::Second
);
assert_eq!(
ArrowTimeUnit::from(PoSQLTimeUnit::Millisecond),
ArrowTimeUnit::Millisecond
);
assert_eq!(
ArrowTimeUnit::from(PoSQLTimeUnit::Microsecond),
ArrowTimeUnit::Microsecond
);
assert_eq!(
ArrowTimeUnit::from(PoSQLTimeUnit::Nanosecond),
ArrowTimeUnit::Nanosecond
);
}
}