@@ -167,19 +167,25 @@ impl DateTime {
167167 let offset = self . offset ( ) ;
168168 let add_sub = if offset >= 0 { '+' } else { '-' } ;
169169 let mut result = String :: with_capacity ( fmt. len ( ) ) ;
170- let mut chars = fmt. chars ( ) ;
171- while let Some ( c) = chars. next ( ) {
172- result. push ( c) ;
170+ let chars = fmt. as_bytes ( ) ;
171+ let mut index = 0 ;
172+ let mut iter = chars. iter ( ) ;
173+ while let Some ( c) = iter. next ( ) {
174+ result. push ( * c as char ) ;
173175 if result. ends_with ( ".000000000" ) {
174176 for _ in 0 ..".000000000" . len ( ) {
175177 result. pop ( ) ;
176178 }
177179 write ! ( result, ".{:09}" , self . nano( ) ) . unwrap ( )
178180 } else if result. ends_with ( ".000000" ) {
181+ if ( index + 3 ) < fmt. len ( ) && chars[ index + 1 ] == '0' as u8 && chars[ index + 2 ] == '0' as u8 && chars[ index + 3 ] == '0' as u8 {
182+ index += 1 ;
183+ continue ;
184+ }
179185 for _ in 0 ..".000000" . len ( ) {
180186 result. pop ( ) ;
181187 }
182- write ! ( result, ".{:06}" , self . nano( ) / 1000 ) . unwrap ( )
188+ write ! ( result, ".{:06}" , self . nano( ) / 1000 ) . unwrap ( ) ;
183189 } else if result. ends_with ( "+00:00" ) {
184190 for _ in 0 .."+00:00" . len ( ) {
185191 result. pop ( ) ;
@@ -218,6 +224,7 @@ impl DateTime {
218224 }
219225 write ! ( result, "{:02}" , self . sec( ) ) . unwrap ( ) ;
220226 }
227+ index += 1 ;
221228 }
222229 result
223230 }
0 commit comments