diesel/pg/types/date_and_time/
mod.rs1use std::ops::Add;
2
3use crate::deserialize::{self, Defaultable, FromSql, FromSqlRow};
4use crate::expression::AsExpression;
5use crate::pg::{Pg, PgValue};
6use crate::serialize::{self, IsNull, Output, ToSql};
7use crate::sql_types::{self, Date, Interval, Time, Timestamp, Timestamptz};
8
9#[cfg(feature = "chrono")]
10mod chrono;
11#[cfg(feature = "quickcheck")]
12mod quickcheck_impls;
13mod std_time;
14#[cfg(feature = "time")]
15mod time;
16
17#[cfg(feature = "postgres_backend")]
18#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, AsExpression, FromSqlRow)]
19#[diesel(sql_type = Timestamp)]
20#[diesel(sql_type = Timestamptz)]
21pub struct PgTimestamp(pub i64);
25
26impl Defaultable for PgTimestamp {
27 fn default_value() -> Self {
28 PgTimestamp(0)
29 }
30}
31
32#[cfg(feature = "postgres_backend")]
33#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, AsExpression, FromSqlRow)]
34#[diesel(sql_type = Date)]
35pub struct PgDate(pub i32);
39
40impl Defaultable for PgDate {
41 fn default_value() -> Self {
42 PgDate(0)
43 }
44}
45
46#[cfg(feature = "postgres_backend")]
50#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, AsExpression, FromSqlRow)]
51#[diesel(sql_type = Time)]
52pub struct PgTime(pub i64);
53
54impl Defaultable for PgTime {
55 fn default_value() -> Self {
56 PgTime(0)
57 }
58}
59
60#[cfg(feature = "postgres_backend")]
65#[derive(Debug, Clone, Copy, PartialEq, Eq, AsExpression, FromSqlRow)]
66#[diesel(sql_type = Interval)]
67pub struct PgInterval {
68 pub microseconds: i64,
70 pub days: i32,
72 pub months: i32,
74}
75
76impl PgInterval {
77 pub fn new(microseconds: i64, days: i32, months: i32) -> Self {
84 PgInterval {
85 microseconds,
86 days,
87 months,
88 }
89 }
90
91 pub fn from_microseconds(microseconds: i64) -> Self {
93 Self::new(microseconds, 0, 0)
94 }
95
96 pub fn from_days(days: i32) -> Self {
98 Self::new(0, days, 0)
99 }
100
101 pub fn from_months(months: i32) -> Self {
103 Self::new(0, 0, months)
104 }
105}
106
107#[cfg(feature = "postgres_backend")]
108impl ToSql<sql_types::Timestamp, Pg> for PgTimestamp {
109 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
110 ToSql::<sql_types::BigInt, Pg>::to_sql(&self.0, out)
111 }
112}
113
114#[cfg(feature = "postgres_backend")]
115impl FromSql<sql_types::Timestamp, Pg> for PgTimestamp {
116 fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
117 FromSql::<sql_types::BigInt, Pg>::from_sql(bytes).map(PgTimestamp)
118 }
119}
120
121#[cfg(feature = "postgres_backend")]
122impl ToSql<sql_types::Timestamptz, Pg> for PgTimestamp {
123 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
124 ToSql::<sql_types::Timestamp, Pg>::to_sql(self, out)
125 }
126}
127
128#[cfg(feature = "postgres_backend")]
129impl FromSql<sql_types::Timestamptz, Pg> for PgTimestamp {
130 fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
131 FromSql::<sql_types::Timestamp, Pg>::from_sql(bytes)
132 }
133}
134
135#[cfg(feature = "postgres_backend")]
136impl ToSql<sql_types::Date, Pg> for PgDate {
137 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
138 ToSql::<sql_types::Integer, Pg>::to_sql(&self.0, out)
139 }
140}
141
142#[cfg(feature = "postgres_backend")]
143impl FromSql<sql_types::Date, Pg> for PgDate {
144 fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
145 FromSql::<sql_types::Integer, Pg>::from_sql(bytes).map(PgDate)
146 }
147}
148
149#[cfg(feature = "postgres_backend")]
150impl ToSql<sql_types::Time, Pg> for PgTime {
151 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
152 ToSql::<sql_types::BigInt, Pg>::to_sql(&self.0, out)
153 }
154}
155
156#[cfg(feature = "postgres_backend")]
157impl FromSql<sql_types::Time, Pg> for PgTime {
158 fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
159 FromSql::<sql_types::BigInt, Pg>::from_sql(bytes).map(PgTime)
160 }
161}
162
163#[cfg(feature = "postgres_backend")]
164impl ToSql<sql_types::Interval, Pg> for PgInterval {
165 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
166 ToSql::<sql_types::BigInt, Pg>::to_sql(&self.microseconds, out)?;
167 ToSql::<sql_types::Integer, Pg>::to_sql(&self.days, out)?;
168 ToSql::<sql_types::Integer, Pg>::to_sql(&self.months, out)?;
169 Ok(IsNull::No)
170 }
171}
172
173#[cfg(feature = "postgres_backend")]
174impl FromSql<sql_types::Interval, Pg> for PgInterval {
175 fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
176 Ok(PgInterval {
177 microseconds: FromSql::<sql_types::BigInt, Pg>::from_sql(value.subslice(0..8))?,
178 days: FromSql::<sql_types::Integer, Pg>::from_sql(value.subslice(8..12))?,
179 months: FromSql::<sql_types::Integer, Pg>::from_sql(value.subslice(12..16))?,
180 })
181 }
182}
183
184impl Add<PgInterval> for PgInterval {
185 type Output = PgInterval;
186
187 fn add(self, other: PgInterval) -> Self::Output {
188 PgInterval {
189 microseconds: self.microseconds + other.microseconds,
190 days: self.days + other.days,
191 months: self.months + other.months,
192 }
193 }
194}