@@ -116,20 +116,23 @@ pub(crate) fn string_to_datetime_formatted<T: TimeZone>(
116116 // custom parser (or switching to Jiff)
117117 let tz: Option < chrono_tz:: Tz > = if format. ends_with ( " %Z" ) {
118118 // grab the string after the last space as the named timezone
119- let parts : Vec < & str > = datetime_str. rsplitn ( 2 , ' ' ) . collect ( ) ;
120- let timezone_name = parts [ 0 ] ;
121- datetime_str = parts [ 1 ] ;
122-
123- // attempt to parse the timezone name
124- let result : Result < chrono_tz :: Tz , chrono_tz :: ParseError > = timezone_name. parse ( ) ;
125- let Ok ( tz) = result else {
126- return Err ( err ( & result. unwrap_err ( ) . to_string ( ) ) ) ;
127- } ;
119+ if let Some ( ( dt_str , timezone_name ) ) = datetime_str. rsplit_once ( ' ' ) {
120+ datetime_str = dt_str ;
121+
122+ // attempt to parse the timezone name
123+ let result : Result < chrono_tz :: Tz , chrono_tz :: ParseError > =
124+ timezone_name. parse ( ) ;
125+ let Ok ( tz) = result else {
126+ return Err ( err ( & result. unwrap_err ( ) . to_string ( ) ) ) ;
127+ } ;
128128
129- // successfully parsed the timezone name, remove the ' %Z' from the format
130- format = format. trim_end_matches ( " %Z" ) ;
129+ // successfully parsed the timezone name, remove the ' %Z' from the format
130+ format = & format[ ..format . len ( ) - 3 ] ;
131131
132- Some ( tz)
132+ Some ( tz)
133+ } else {
134+ None
135+ }
133136 } else if format. contains ( "%Z" ) {
134137 return Err ( err (
135138 "'%Z' is only supported at the end of the format string preceded by a space" ,
0 commit comments