vortex_array/extension/datetime/
rounding.rs1use std::fmt;
11
12use vortex_error::VortexResult;
13use vortex_error::vortex_ensure_eq;
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
17pub enum TemporalRounding {
18 Exact,
20 TowardZero,
22 Floor,
24 NearestTiesAwayFromZero,
26}
27
28impl TemporalRounding {
29 pub fn divide(self, value: i64, divisor: i64) -> VortexResult<i64> {
31 let quotient = value / divisor;
32 let remainder = value % divisor;
33
34 Ok(match self {
35 Self::Exact => {
36 vortex_ensure_eq!(
37 remainder,
38 0,
39 "Expected an exact temporal conversion, got {value}"
40 );
41 quotient
42 }
43 Self::TowardZero => quotient,
44 Self::Floor => value.div_euclid(divisor),
45 Self::NearestTiesAwayFromZero if remainder.abs() * 2 >= divisor => {
46 quotient + remainder.signum()
47 }
48 Self::NearestTiesAwayFromZero => quotient,
49 })
50 }
51}
52
53impl fmt::Display for TemporalRounding {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 f.write_str(match self {
56 Self::Exact => "exact",
57 Self::TowardZero => "toward_zero",
58 Self::Floor => "floor",
59 Self::NearestTiesAwayFromZero => "nearest_ties_away_from_zero",
60 })
61 }
62}