@@ -121,6 +121,24 @@ impl<T> MappedLocalTime<T> {
121
121
MappedLocalTime :: Ambiguous ( min, max) => MappedLocalTime :: Ambiguous ( f ( min) , f ( max) ) ,
122
122
}
123
123
}
124
+
125
+ /// Maps a `MappedLocalTime<T>` into `MappedLocalTime<U>` with given function.
126
+ ///
127
+ /// Returns `MappedLocalTime::None` if the function returns `None`.
128
+ #[ must_use]
129
+ pub ( crate ) fn and_then < U , F : FnMut ( T ) -> Option < U > > ( self , mut f : F ) -> MappedLocalTime < U > {
130
+ match self {
131
+ MappedLocalTime :: None => MappedLocalTime :: None ,
132
+ MappedLocalTime :: Single ( v) => match f ( v) {
133
+ Some ( new) => MappedLocalTime :: Single ( new) ,
134
+ None => MappedLocalTime :: None ,
135
+ } ,
136
+ MappedLocalTime :: Ambiguous ( min, max) => match ( f ( min) , f ( max) ) {
137
+ ( Some ( min) , Some ( max) ) => MappedLocalTime :: Ambiguous ( min, max) ,
138
+ _ => MappedLocalTime :: None ,
139
+ } ,
140
+ }
141
+ }
124
142
}
125
143
126
144
/// The conversion result from the local time to the timezone-aware datetime types.
@@ -547,26 +565,11 @@ pub trait TimeZone: Sized + Clone {
547
565
/// Converts the local `NaiveDateTime` to the timezone-aware `DateTime` if possible.
548
566
#[ allow( clippy:: wrong_self_convention) ]
549
567
fn from_local_datetime ( & self , local : & NaiveDateTime ) -> MappedLocalTime < DateTime < Self > > {
550
- // Return `MappedLocalTime::None` when the offset pushes a value out of range, instead of
551
- // panicking.
552
- match self . offset_from_local_datetime ( local) {
553
- MappedLocalTime :: None => MappedLocalTime :: None ,
554
- MappedLocalTime :: Single ( offset) => match local. checked_sub_offset ( offset. fix ( ) ) {
555
- Some ( dt) => {
556
- MappedLocalTime :: Single ( DateTime :: from_naive_utc_and_offset ( dt, offset) )
557
- }
558
- None => MappedLocalTime :: None ,
559
- } ,
560
- MappedLocalTime :: Ambiguous ( o1, o2) => {
561
- match ( local. checked_sub_offset ( o1. fix ( ) ) , local. checked_sub_offset ( o2. fix ( ) ) ) {
562
- ( Some ( d1) , Some ( d2) ) => MappedLocalTime :: Ambiguous (
563
- DateTime :: from_naive_utc_and_offset ( d1, o1) ,
564
- DateTime :: from_naive_utc_and_offset ( d2, o2) ,
565
- ) ,
566
- _ => MappedLocalTime :: None ,
567
- }
568
- }
569
- }
568
+ self . offset_from_local_datetime ( local) . and_then ( |off| {
569
+ local
570
+ . checked_sub_offset ( off. fix ( ) )
571
+ . map ( |dt| DateTime :: from_naive_utc_and_offset ( dt, off) )
572
+ } )
570
573
}
571
574
572
575
/// Creates the offset for given UTC `NaiveDate`. This cannot fail.
0 commit comments