@@ -6,6 +6,7 @@ use crate::{
66 } ,
77} ;
88use anyhow:: { bail, Context , Result } ;
9+ use chrono:: format:: { Fixed , Item , Numeric , Pad } ;
910use chrono:: { DateTime , NaiveDateTime , TimeZone , Utc } ;
1011use indexmap:: { Equivalent , IndexMap } ;
1112use itertools:: multizip;
@@ -1164,7 +1165,22 @@ pub enum DBResponsePrimitive {
11641165 Uncommon ( Value ) ,
11651166}
11661167
1167- const TIMESTAMP_FORMAT : & str = "%Y-%m-%dT%H:%M:%S%.3f" ;
1168+ /// `%Y-%m-%dT%H:%M:%S%.3f`
1169+ const TIMESTAMP_ITEMS : & [ Item < ' static > ] = & [
1170+ Item :: Numeric ( Numeric :: Year , Pad :: Zero ) ,
1171+ Item :: Literal ( "-" ) ,
1172+ Item :: Numeric ( Numeric :: Month , Pad :: Zero ) ,
1173+ Item :: Literal ( "-" ) ,
1174+ Item :: Numeric ( Numeric :: Day , Pad :: Zero ) ,
1175+ Item :: Literal ( "T" ) ,
1176+ Item :: Numeric ( Numeric :: Hour , Pad :: Zero ) ,
1177+ Item :: Literal ( ":" ) ,
1178+ Item :: Numeric ( Numeric :: Minute , Pad :: Zero ) ,
1179+ Item :: Literal ( ":" ) ,
1180+ Item :: Numeric ( Numeric :: Second , Pad :: Zero ) ,
1181+ // `%.3f`: leading dot followed by 3 fractional-second digits.
1182+ Item :: Fixed ( Fixed :: Nanosecond3 ) ,
1183+ ] ;
11681184
11691185// Hand-written `Serialize` mirroring serde's untagged behavior for the JSON-native
11701186// variants, plus a string rendering for `Timestamp`.
@@ -1178,7 +1194,7 @@ impl Serialize for DBResponsePrimitive {
11781194 DBResponsePrimitive :: Float64 ( n) => serializer. serialize_f64 ( * n) ,
11791195 DBResponsePrimitive :: String ( s) => serializer. serialize_str ( s) ,
11801196 DBResponsePrimitive :: Timestamp ( dt) => {
1181- serializer. serialize_str ( & dt. format ( TIMESTAMP_FORMAT ) . to_string ( ) )
1197+ serializer. collect_str ( & dt. format_with_items ( TIMESTAMP_ITEMS . iter ( ) ) )
11821198 }
11831199 DBResponsePrimitive :: Uncommon ( v) => v. serialize ( serializer) ,
11841200 }
@@ -1293,7 +1309,9 @@ impl Display for DBResponsePrimitive {
12931309 DBResponsePrimitive :: UInt64 ( n) => n. to_string ( ) ,
12941310 DBResponsePrimitive :: Float64 ( n) => n. to_string ( ) ,
12951311 DBResponsePrimitive :: String ( s) => s. clone ( ) ,
1296- DBResponsePrimitive :: Timestamp ( dt) => dt. format ( TIMESTAMP_FORMAT ) . to_string ( ) ,
1312+ DBResponsePrimitive :: Timestamp ( dt) => {
1313+ dt. format_with_items ( TIMESTAMP_ITEMS . iter ( ) ) . to_string ( )
1314+ }
12971315 DBResponsePrimitive :: Uncommon ( v) => {
12981316 serde_json:: to_string ( & v) . unwrap_or_else ( |_| v. to_string ( ) )
12991317 }
0 commit comments