quackdb_internal/conversion/to_duckdb/
chrono.rs

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
use crate::{ffi, type_id::TypeId};
use chrono::prelude::*;

use super::{FromDuckDb, IntoDuckDb, ToDuckDbType};

impl ToDuckDbType for NaiveDate {
    const DUCKDB_TYPE_ID: TypeId = TypeId::Date;

    type DuckDbRepresentation = ffi::duckdb_date;
}
impl IntoDuckDb for NaiveDate {
    fn into_duckdb(self) -> Self::DuckDbRepresentation {
        let s = ffi::duckdb_date_struct {
            year: self.year(),
            month: self.month() as i8,
            day: self.day() as i8,
        };
        unsafe { ffi::duckdb_to_date(s) }
    }
}
impl FromDuckDb for NaiveDate {
    fn from_duckdb(value: Self::DuckDbRepresentation) -> Self {
        let date = unsafe { ffi::duckdb_from_date(value) };
        Self::from_ymd_opt(date.year, date.month as u32, date.day as u32).expect("from duckdb_date")
    }
}

impl ToDuckDbType for NaiveTime {
    const DUCKDB_TYPE_ID: TypeId = TypeId::Time;

    type DuckDbRepresentation = ffi::duckdb_time;
}
impl IntoDuckDb for NaiveTime {
    fn into_duckdb(self) -> Self::DuckDbRepresentation {
        let s = ffi::duckdb_time_struct {
            hour: self.hour() as i8,
            min: self.minute() as i8,
            sec: self.second() as i8,
            micros: (self.nanosecond() / 1000) as i32,
        };
        unsafe { ffi::duckdb_to_time(s) }
    }
}
impl FromDuckDb for NaiveTime {
    fn from_duckdb(value: Self::DuckDbRepresentation) -> Self {
        Self::from_num_seconds_from_midnight_opt(
            (value.micros / 1000000) as u32,
            (value.micros % 1000000 * 1000) as u32,
        )
        .expect("from duckdb_time")
    }
}

impl ToDuckDbType for NaiveDateTime {
    const DUCKDB_TYPE_ID: TypeId = TypeId::Timestamp;

    type DuckDbRepresentation = ffi::duckdb_timestamp;
}
impl IntoDuckDb for NaiveDateTime {
    fn into_duckdb(self) -> Self::DuckDbRepresentation {
        ffi::duckdb_timestamp {
            micros: self.timestamp_micros(),
        }
    }
}
impl FromDuckDb for NaiveDateTime {
    fn from_duckdb(value: Self::DuckDbRepresentation) -> Self {
        Self::from_timestamp_micros(value.micros).expect("from duckdb_timestamp")
    }
}

impl<Tz: TimeZone> ToDuckDbType for DateTime<Tz> {
    const DUCKDB_TYPE_ID: TypeId = TypeId::Timestamp;
    type DuckDbRepresentation = ffi::duckdb_timestamp;
}
impl<Tz: TimeZone> IntoDuckDb for DateTime<Tz> {
    fn into_duckdb(self) -> Self::DuckDbRepresentation {
        ffi::duckdb_timestamp {
            micros: self.timestamp_micros(),
        }
    }
}
impl FromDuckDb for DateTime<Utc> {
    fn from_duckdb(value: Self::DuckDbRepresentation) -> Self {
        Self::from_timestamp(
            value.micros / 1000000,
            (value.micros % 1000000 * 1000) as u32,
        )
        .expect("from duckdb_timestamp")
    }
}