1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use chrono::{Duration, Utc};
use chrono_tz::{Tz, UTC};

use crate::{
    convert::{unexpected_type, FromSql, ToSql},
    types::Type,
    Value,
};
use anyhow::*;

/// Wrapper type for Clickhouse `Date` type.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Debug, Default)]
pub struct Date(pub u16);

impl ToSql for Date {
    fn to_sql(self) -> Result<Value> {
        Ok(Value::Date(self))
    }
}

impl FromSql for Date {
    fn from_sql(type_: &Type, value: Value) -> Result<Self> {
        if !matches!(type_, Type::Date) {
            return Err(unexpected_type(type_));
        }
        match value {
            Value::Date(x) => Ok(x),
            _ => unimplemented!(),
        }
    }
}

impl Into<chrono::Date<Utc>> for Date {
    fn into(self) -> chrono::Date<Utc> {
        chrono::MIN_DATE + Duration::days(self.0 as i64)
    }
}

impl From<chrono::Date<Utc>> for Date {
    fn from(other: chrono::Date<Utc>) -> Self {
        Self(other.signed_duration_since(chrono::MIN_DATE).num_days() as u16)
    }
}

/// Wrapper type for Clickhouse `DateTime` type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DateTime(pub Tz, pub u32);

impl ToSql for DateTime {
    fn to_sql(self) -> Result<Value> {
        Ok(Value::DateTime(self))
    }
}

impl FromSql for DateTime {
    fn from_sql(type_: &Type, value: Value) -> Result<Self> {
        if !matches!(type_, Type::DateTime(_)) {
            return Err(unexpected_type(type_));
        }
        match value {
            Value::DateTime(x) => Ok(x),
            _ => unimplemented!(),
        }
    }
}

impl Default for DateTime {
    fn default() -> Self {
        Self(UTC, 0)
    }
}

impl Into<chrono::DateTime<Tz>> for DateTime {
    fn into(self) -> chrono::DateTime<Tz> {
        chrono::MIN_DATETIME.with_timezone(&self.0) + Duration::seconds(self.1 as i64)
    }
}

impl From<chrono::DateTime<Tz>> for DateTime {
    fn from(other: chrono::DateTime<Tz>) -> Self {
        Self(
            other.timezone(),
            other
                .signed_duration_since(chrono::MIN_DATETIME)
                .num_seconds() as u32,
        )
    }
}

impl From<chrono::DateTime<chrono::Utc>> for DateTime {
    fn from(other: chrono::DateTime<Utc>) -> Self {
        Self(
            chrono_tz::UTC,
            other
                .signed_duration_since(chrono::MIN_DATETIME)
                .num_seconds() as u32,
        )
    }
}

/// Wrapper type for Clickhouse `DateTime64` type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DateTime64<const PRECISION: usize>(pub Tz, pub u64);

impl<const PRECISION: usize> ToSql for DateTime64<PRECISION> {
    fn to_sql(self) -> Result<Value> {
        Ok(Value::DateTime64(self.0, PRECISION, self.1))
    }
}

impl<const PRECISION: usize> FromSql for DateTime64<PRECISION> {
    fn from_sql(type_: &Type, value: Value) -> Result<Self> {
        if !matches!(type_, Type::DateTime64(x, _) if *x == PRECISION) {
            return Err(unexpected_type(type_));
        }
        match value {
            Value::DateTime64(tz, _, value) => Ok(Self(tz, value)),
            _ => unimplemented!(),
        }
    }
}

impl<const PRECISION: usize> Default for DateTime64<PRECISION> {
    fn default() -> Self {
        Self(UTC, 0)
    }
}

impl<const PRECISION: usize> Into<chrono::DateTime<Tz>> for DateTime64<PRECISION> {
    fn into(self) -> chrono::DateTime<Tz> {
        chrono::MIN_DATETIME.with_timezone(&self.0) + Duration::seconds(self.1 as i64)
    }
}

impl<const PRECISION: usize> From<chrono::DateTime<Tz>> for DateTime64<PRECISION> {
    fn from(other: chrono::DateTime<Tz>) -> Self {
        Self(
            other.timezone(),
            other
                .signed_duration_since(chrono::MIN_DATETIME)
                .num_seconds() as u64,
        )
    }
}

#[cfg(test)]
mod chrono_tests {
    use super::*;
    use chrono_tz::UTC;

    #[test]
    fn test_date() {
        for i in 0..30000u16 {
            let date = Date(i);
            let chrono_date: chrono::Date<Utc> = date.into();
            let new_date = Date::from(chrono_date);
            assert_eq!(new_date, date);
        }
    }

    #[test]
    fn test_datetime() {
        for i in (0..30000u32).map(|x| x * 10000) {
            let date = DateTime(UTC, i);
            let chrono_date: chrono::DateTime<Tz> = date.into();
            let new_date = DateTime::from(chrono_date);
            assert_eq!(new_date, date);
        }
    }
}