sqlx_postgres/types/chrono/
time.rs1use crate::decode::Decode;
2use crate::encode::{Encode, IsNull};
3use crate::error::BoxDynError;
4use crate::types::Type;
5use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
6use chrono::{Duration, NaiveTime};
7use std::mem;
8
9impl Type<Postgres> for NaiveTime {
10 fn type_info() -> PgTypeInfo {
11 PgTypeInfo::TIME
12 }
13}
14
15impl PgHasArrayType for NaiveTime {
16 fn array_type_info() -> PgTypeInfo {
17 PgTypeInfo::TIME_ARRAY
18 }
19}
20
21impl Encode<'_, Postgres> for NaiveTime {
22 fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
23 let micros = (*self - NaiveTime::default())
25 .num_microseconds()
26 .ok_or_else(|| format!("Time out of range for PostgreSQL: {self}"))?;
27
28 Encode::<Postgres>::encode(micros, buf)
29 }
30
31 fn size_hint(&self) -> usize {
32 mem::size_of::<u64>()
33 }
34}
35
36impl<'r> Decode<'r, Postgres> for NaiveTime {
37 fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
38 Ok(match value.format() {
39 PgValueFormat::Binary => {
40 let us: i64 = Decode::<Postgres>::decode(value)?;
42 NaiveTime::default() + Duration::microseconds(us)
43 }
44
45 PgValueFormat::Text => NaiveTime::parse_from_str(value.as_str()?, "%H:%M:%S%.f")?,
46 })
47 }
48}
49
50#[test]
51fn check_naive_time_default_is_midnight() {
52 assert_eq!(
54 NaiveTime::from_hms_opt(0, 0, 0),
55 Some(NaiveTime::default()),
56 "implementation assumes `NaiveTime::default()` equals midnight"
57 );
58}