11#![ cfg_attr( not( feature = "with_protocol" ) , allow( unused) ) ]
2- use chrono:: { DateTime , TimeZone , Utc } ;
2+ use chrono:: { DateTime , LocalResult , TimeZone , Utc } ;
33
44/// Converts a datetime object into a float timestamp.
55pub fn datetime_to_timestamp ( dt : & DateTime < Utc > ) -> f64 {
@@ -10,14 +10,14 @@ pub fn datetime_to_timestamp(dt: &DateTime<Utc>) -> f64 {
1010 }
1111}
1212
13- pub fn timestamp_to_datetime ( ts : f64 ) -> DateTime < Utc > {
13+ pub fn timestamp_to_datetime ( ts : f64 ) -> LocalResult < DateTime < Utc > > {
1414 let secs = ts as i64 ;
1515 let micros = ( ts. fract ( ) * 1_000_000f64 ) as u32 ;
16- Utc . timestamp_opt ( secs, micros * 1000 ) . unwrap ( )
16+ Utc . timestamp_opt ( secs, micros * 1000 )
1717}
1818
1919pub mod ts_seconds_float {
20- use chrono:: { DateTime , TimeZone , Utc } ;
20+ use chrono:: { DateTime , LocalResult , TimeZone , Utc } ;
2121 use serde:: { de, ser} ;
2222 use std:: fmt;
2323
@@ -57,7 +57,14 @@ pub mod ts_seconds_float {
5757 where
5858 E : de:: Error ,
5959 {
60- Ok ( timestamp_to_datetime ( value) )
60+ match timestamp_to_datetime ( value) {
61+ LocalResult :: None => Err ( E :: custom ( format ! ( "No such local time for {}" , value) ) ) ,
62+ LocalResult :: Single ( date) => Ok ( date) ,
63+ LocalResult :: Ambiguous ( t1, t2) => Err ( E :: custom ( format ! (
64+ "Ambiguous local time, ranging from {:?} to {:?}" ,
65+ t1, t2
66+ ) ) ) ,
67+ }
6168 }
6269
6370 fn visit_i64 < E > ( self , value : i64 ) -> Result < DateTime < Utc > , E >
0 commit comments