Debian Codegraph @

src/types/chrono/time.rs

raw symbols SCIP
1 use crate::decode::Decode;
2 use crate::encode::{Encode, IsNull};
3 use crate::error::BoxDynError;
4 use crate::types::Type;
5 use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
6 use chrono::{Duration, NaiveTime};
7 use std::mem;
8
9 impl Type<Postgres> for NaiveTime {
10 fn type_info() -> PgTypeInfo {
11 PgTypeInfo::TIME
12 }
13 }
14
15 impl PgHasArrayType for NaiveTime {
16 fn array_type_info() -> PgTypeInfo {
17 PgTypeInfo::TIME_ARRAY
18 }
19 }
20
21 impl Encode<'_, Postgres> for NaiveTime {
22 fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
23 // TIME is encoded as the microseconds since midnight
24 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
36 impl<'r> Decode<'r, Postgres> for NaiveTime {
37 fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
38 Ok(match value.format() {
39 PgValueFormat::Binary => {
40 // TIME is encoded as the microseconds since midnight
41 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]
51 fn check_naive_time_default_is_midnight() {
52 // Just a canary in case this changes.
53 assert_eq!(
54 NaiveTime::from_hms_opt(0, 0, 0),
55 Some(NaiveTime::default()),
56 "implementation assumes `NaiveTime::default()` equals midnight"
57 );
58 }