1- use crate :: { impl_try_convert_to_string , TryConvert } ;
1+ use crate :: TryConvert ;
22use chrono:: { DateTime , FixedOffset , NaiveDateTime , Utc } ;
33
4+ macro_rules! impl_try_convert_datetime_to_string {
5+ ( $type: ty) => {
6+ impl TryConvert <$type> for String {
7+ fn try_convert( value: $type) -> Result <Self , String > {
8+ Ok ( value. to_rfc3339( ) )
9+ }
10+ }
11+ } ;
12+ }
13+
414// DateTime<Utc> conversions
515impl TryConvert < String > for DateTime < Utc > {
616 fn try_convert ( value : String ) -> Result < Self , String > {
@@ -20,7 +30,7 @@ impl TryConvert<String> for DateTime<Utc> {
2030 }
2131}
2232
23- impl_try_convert_to_string ! ( DateTime <Utc >) ;
33+ impl_try_convert_datetime_to_string ! ( DateTime <Utc >) ;
2434
2535// DateTime<FixedOffset> conversions
2636impl TryConvert < String > for DateTime < FixedOffset > {
@@ -46,7 +56,7 @@ impl TryConvert<String> for DateTime<FixedOffset> {
4656 }
4757}
4858
49- impl_try_convert_to_string ! ( DateTime <FixedOffset >) ;
59+ impl_try_convert_datetime_to_string ! ( DateTime <FixedOffset >) ;
5060
5161// NaiveDateTime conversions
5262impl TryConvert < String > for NaiveDateTime {
@@ -68,15 +78,23 @@ impl TryConvert<String> for NaiveDateTime {
6878 }
6979}
7080
71- impl_try_convert_to_string ! ( NaiveDateTime ) ;
81+
82+ // implemented via Debug according to the implementation of the serde module
83+ // https://github.com/chronotope/chrono/blob/e632ffd3b89d3cfaa96776f2368ee4c21a972766/src/naive/datetime/serde.rs#L6-L26
84+ // https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html#impl-Debug-for-NaiveDateTime
85+ impl TryConvert < NaiveDateTime > for String {
86+ fn try_convert ( value : NaiveDateTime ) -> Result < Self , String > {
87+ Ok ( format ! ( "{:?}" , value) )
88+ }
89+ }
7290
7391#[ cfg( test) ]
7492mod tests {
7593 use super :: * ;
7694 use pretty_assertions:: assert_eq;
7795
7896 #[ test]
79- fn test_conversion_datetime ( ) {
97+ fn test_conversion_datetime_from_string ( ) {
8098 let datetime = DateTime :: < Utc > :: try_convert ( "1645491600" . to_string ( ) ) . unwrap ( ) ;
8199 assert_eq ! (
82100 datetime,
@@ -151,7 +169,10 @@ mod tests {
151169 datetime,
152170 "2021-01-01T00:00:00Z" . parse:: <DateTime <Utc >>( ) . unwrap( )
153171 ) ;
172+ }
154173
174+ #[ test]
175+ fn test_conversion_datetime_errors ( ) {
155176 // Error cases
156177 let error = DateTime :: < Utc > :: try_convert ( "2021-01-01T00:00:00" . to_string ( ) )
157178 . expect_err ( "Invalid datetime" ) ;
@@ -177,4 +198,23 @@ mod tests {
177198 "failed to parse '' as NaiveDateTime: premature end of input"
178199 ) ;
179200 }
201+
202+ #[ test]
203+ fn test_conversion_datetime_to_string ( ) {
204+ let datetime = "2021-01-01T00:00:00.123456789+00:00"
205+ . parse :: < DateTime < Utc > > ( )
206+ . unwrap ( ) ;
207+ let string = String :: try_convert ( datetime) . unwrap ( ) ;
208+ assert_eq ! ( string, "2021-01-01T00:00:00.123456789+00:00" ) ;
209+
210+ let datetime = "2021-01-01T00:00:00.123456789+00:00"
211+ . parse :: < DateTime < FixedOffset > > ( )
212+ . unwrap ( ) ;
213+ let string = String :: try_convert ( datetime) . unwrap ( ) ;
214+ assert_eq ! ( string, "2021-01-01T00:00:00.123456789+00:00" ) ;
215+
216+ let datetime = "2021-01-01T00:00:00.123456789" . parse :: < NaiveDateTime > ( ) . unwrap ( ) ;
217+ let string = String :: try_convert ( datetime) . unwrap ( ) ;
218+ assert_eq ! ( string, "2021-01-01T00:00:00.123456789" ) ;
219+ }
180220}
0 commit comments