@@ -127,9 +127,7 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
127127 #[ cfg( feature = "alloc" ) ]
128128 Item :: OwnedLiteral ( ref s) | Item :: OwnedSpace ( ref s) => w. write_str ( s) ,
129129 Item :: Numeric ( ref spec, ref pad) => self . format_numeric ( w, spec, pad) ,
130- Item :: Fixed ( ref spec) => {
131- format_fixed ( w, self . date , self . time , self . off . as_ref ( ) , spec, locale)
132- }
130+ Item :: Fixed ( ref spec) => self . format_fixed ( w, spec, locale) ,
133131 Item :: Error => Err ( fmt:: Error ) ,
134132 } ?;
135133 }
@@ -199,6 +197,169 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
199197 Err ( fmt:: Error ) // insufficient arguments for given format
200198 }
201199 }
200+
201+ #[ cfg( feature = "alloc" ) ]
202+ fn format_fixed (
203+ & self ,
204+ w : & mut impl Write ,
205+ spec : & Fixed ,
206+ locale : Option < Locale > ,
207+ ) -> fmt:: Result {
208+ use self :: Fixed :: * ;
209+
210+ let locale = locale. unwrap_or ( default_locale ( ) ) ;
211+
212+ let ret = match * spec {
213+ ShortMonthName => self . date . map ( |d| {
214+ w. write_str ( short_months ( locale) [ d. month0 ( ) as usize ] ) ?;
215+ Ok ( ( ) )
216+ } ) ,
217+ LongMonthName => self . date . map ( |d| {
218+ w. write_str ( long_months ( locale) [ d. month0 ( ) as usize ] ) ?;
219+ Ok ( ( ) )
220+ } ) ,
221+ ShortWeekdayName => self . date . map ( |d| {
222+ w. write_str ( short_weekdays ( locale) [ d. weekday ( ) . num_days_from_sunday ( ) as usize ] ) ?;
223+ Ok ( ( ) )
224+ } ) ,
225+ LongWeekdayName => self . date . map ( |d| {
226+ w. write_str ( long_weekdays ( locale) [ d. weekday ( ) . num_days_from_sunday ( ) as usize ] ) ?;
227+ Ok ( ( ) )
228+ } ) ,
229+ LowerAmPm => self . time . map ( |t| {
230+ let ampm = if t. hour12 ( ) . 0 { am_pm ( locale) [ 1 ] } else { am_pm ( locale) [ 0 ] } ;
231+ for c in ampm. chars ( ) . flat_map ( |c| c. to_lowercase ( ) ) {
232+ w. write_char ( c) ?
233+ }
234+ Ok ( ( ) )
235+ } ) ,
236+ UpperAmPm => self . time . map ( |t| {
237+ w. write_str ( if t. hour12 ( ) . 0 { am_pm ( locale) [ 1 ] } else { am_pm ( locale) [ 0 ] } ) ?;
238+ Ok ( ( ) )
239+ } ) ,
240+ Nanosecond => self . time . map ( |t| {
241+ let nano = t. nanosecond ( ) % 1_000_000_000 ;
242+ if nano == 0 {
243+ Ok ( ( ) )
244+ } else {
245+ w. write_str ( decimal_point ( locale) ) ?;
246+ if nano % 1_000_000 == 0 {
247+ write ! ( w, "{:03}" , nano / 1_000_000 )
248+ } else if nano % 1_000 == 0 {
249+ write ! ( w, "{:06}" , nano / 1_000 )
250+ } else {
251+ write ! ( w, "{:09}" , nano)
252+ }
253+ }
254+ } ) ,
255+ Nanosecond3 => self . time . map ( |t| {
256+ let nano = t. nanosecond ( ) % 1_000_000_000 ;
257+ w. write_str ( decimal_point ( locale) ) ?;
258+ write ! ( w, "{:03}" , nano / 1_000_000 )
259+ } ) ,
260+ Nanosecond6 => self . time . map ( |t| {
261+ let nano = t. nanosecond ( ) % 1_000_000_000 ;
262+ w. write_str ( decimal_point ( locale) ) ?;
263+ write ! ( w, "{:06}" , nano / 1_000 )
264+ } ) ,
265+ Nanosecond9 => self . time . map ( |t| {
266+ let nano = t. nanosecond ( ) % 1_000_000_000 ;
267+ w. write_str ( decimal_point ( locale) ) ?;
268+ write ! ( w, "{:09}" , nano)
269+ } ) ,
270+ Internal ( InternalFixed { val : InternalInternal :: Nanosecond3NoDot } ) => {
271+ self . time . map ( |t| {
272+ let nano = t. nanosecond ( ) % 1_000_000_000 ;
273+ write ! ( w, "{:03}" , nano / 1_000_000 )
274+ } )
275+ }
276+ Internal ( InternalFixed { val : InternalInternal :: Nanosecond6NoDot } ) => {
277+ self . time . map ( |t| {
278+ let nano = t. nanosecond ( ) % 1_000_000_000 ;
279+ write ! ( w, "{:06}" , nano / 1_000 )
280+ } )
281+ }
282+ Internal ( InternalFixed { val : InternalInternal :: Nanosecond9NoDot } ) => {
283+ self . time . map ( |t| {
284+ let nano = t. nanosecond ( ) % 1_000_000_000 ;
285+ write ! ( w, "{:09}" , nano)
286+ } )
287+ }
288+ TimezoneName => self . off . as_ref ( ) . map ( |( name, _) | {
289+ w. write_str ( name) ?;
290+ Ok ( ( ) )
291+ } ) ,
292+ TimezoneOffset | TimezoneOffsetZ => self . off . as_ref ( ) . map ( |& ( _, off) | {
293+ OffsetFormat {
294+ precision : OffsetPrecision :: Minutes ,
295+ colons : Colons :: Maybe ,
296+ allow_zulu : * spec == TimezoneOffsetZ ,
297+ padding : Pad :: Zero ,
298+ }
299+ . format ( w, off)
300+ } ) ,
301+ TimezoneOffsetColon | TimezoneOffsetColonZ => self . off . as_ref ( ) . map ( |& ( _, off) | {
302+ OffsetFormat {
303+ precision : OffsetPrecision :: Minutes ,
304+ colons : Colons :: Colon ,
305+ allow_zulu : * spec == TimezoneOffsetColonZ ,
306+ padding : Pad :: Zero ,
307+ }
308+ . format ( w, off)
309+ } ) ,
310+ TimezoneOffsetDoubleColon => self . off . as_ref ( ) . map ( |& ( _, off) | {
311+ OffsetFormat {
312+ precision : OffsetPrecision :: Seconds ,
313+ colons : Colons :: Colon ,
314+ allow_zulu : false ,
315+ padding : Pad :: Zero ,
316+ }
317+ . format ( w, off)
318+ } ) ,
319+ TimezoneOffsetTripleColon => self . off . as_ref ( ) . map ( |& ( _, off) | {
320+ OffsetFormat {
321+ precision : OffsetPrecision :: Hours ,
322+ colons : Colons :: None ,
323+ allow_zulu : false ,
324+ padding : Pad :: Zero ,
325+ }
326+ . format ( w, off)
327+ } ) ,
328+ Internal ( InternalFixed { val : InternalInternal :: TimezoneOffsetPermissive } ) => {
329+ return Err ( fmt:: Error ) ;
330+ }
331+ RFC2822 =>
332+ // same as `%a, %d %b %Y %H:%M:%S %z`
333+ {
334+ if let ( Some ( d) , Some ( t) , Some ( & ( _, off) ) ) =
335+ ( self . date , self . time , self . off . as_ref ( ) )
336+ {
337+ Some ( write_rfc2822 ( w, crate :: NaiveDateTime :: new ( d, t) , off) )
338+ } else {
339+ None
340+ }
341+ }
342+ RFC3339 =>
343+ // same as `%Y-%m-%dT%H:%M:%S%.f%:z`
344+ {
345+ if let ( Some ( d) , Some ( t) , Some ( & ( _, off) ) ) =
346+ ( self . date , self . time , self . off . as_ref ( ) )
347+ {
348+ Some ( write_rfc3339 (
349+ w,
350+ crate :: NaiveDateTime :: new ( d, t) ,
351+ off. fix ( ) ,
352+ SecondsFormat :: AutoSi ,
353+ false ,
354+ ) )
355+ } else {
356+ None
357+ }
358+ }
359+ } ;
360+
361+ ret. unwrap_or ( Err ( fmt:: Error ) ) // insufficient arguments for given format
362+ }
202363}
203364
204365#[ cfg( feature = "alloc" ) ]
@@ -257,161 +418,6 @@ pub fn format_item(
257418 . fmt ( w)
258419}
259420
260- #[ cfg( feature = "alloc" ) ]
261- fn format_fixed (
262- w : & mut impl Write ,
263- date : Option < NaiveDate > ,
264- time : Option < NaiveTime > ,
265- off : Option < & ( String , FixedOffset ) > ,
266- spec : & Fixed ,
267- locale : Option < Locale > ,
268- ) -> fmt:: Result {
269- use self :: Fixed :: * ;
270-
271- let locale = locale. unwrap_or ( default_locale ( ) ) ;
272-
273- let ret = match * spec {
274- ShortMonthName => date. map ( |d| {
275- w. write_str ( short_months ( locale) [ d. month0 ( ) as usize ] ) ?;
276- Ok ( ( ) )
277- } ) ,
278- LongMonthName => date. map ( |d| {
279- w. write_str ( long_months ( locale) [ d. month0 ( ) as usize ] ) ?;
280- Ok ( ( ) )
281- } ) ,
282- ShortWeekdayName => date. map ( |d| {
283- w. write_str ( short_weekdays ( locale) [ d. weekday ( ) . num_days_from_sunday ( ) as usize ] ) ?;
284- Ok ( ( ) )
285- } ) ,
286- LongWeekdayName => date. map ( |d| {
287- w. write_str ( long_weekdays ( locale) [ d. weekday ( ) . num_days_from_sunday ( ) as usize ] ) ?;
288- Ok ( ( ) )
289- } ) ,
290- LowerAmPm => time. map ( |t| {
291- let ampm = if t. hour12 ( ) . 0 { am_pm ( locale) [ 1 ] } else { am_pm ( locale) [ 0 ] } ;
292- for c in ampm. chars ( ) . flat_map ( |c| c. to_lowercase ( ) ) {
293- w. write_char ( c) ?
294- }
295- Ok ( ( ) )
296- } ) ,
297- UpperAmPm => time. map ( |t| {
298- w. write_str ( if t. hour12 ( ) . 0 { am_pm ( locale) [ 1 ] } else { am_pm ( locale) [ 0 ] } ) ?;
299- Ok ( ( ) )
300- } ) ,
301- Nanosecond => time. map ( |t| {
302- let nano = t. nanosecond ( ) % 1_000_000_000 ;
303- if nano == 0 {
304- Ok ( ( ) )
305- } else {
306- w. write_str ( decimal_point ( locale) ) ?;
307- if nano % 1_000_000 == 0 {
308- write ! ( w, "{:03}" , nano / 1_000_000 )
309- } else if nano % 1_000 == 0 {
310- write ! ( w, "{:06}" , nano / 1_000 )
311- } else {
312- write ! ( w, "{:09}" , nano)
313- }
314- }
315- } ) ,
316- Nanosecond3 => time. map ( |t| {
317- let nano = t. nanosecond ( ) % 1_000_000_000 ;
318- w. write_str ( decimal_point ( locale) ) ?;
319- write ! ( w, "{:03}" , nano / 1_000_000 )
320- } ) ,
321- Nanosecond6 => time. map ( |t| {
322- let nano = t. nanosecond ( ) % 1_000_000_000 ;
323- w. write_str ( decimal_point ( locale) ) ?;
324- write ! ( w, "{:06}" , nano / 1_000 )
325- } ) ,
326- Nanosecond9 => time. map ( |t| {
327- let nano = t. nanosecond ( ) % 1_000_000_000 ;
328- w. write_str ( decimal_point ( locale) ) ?;
329- write ! ( w, "{:09}" , nano)
330- } ) ,
331- Internal ( InternalFixed { val : InternalInternal :: Nanosecond3NoDot } ) => time. map ( |t| {
332- let nano = t. nanosecond ( ) % 1_000_000_000 ;
333- write ! ( w, "{:03}" , nano / 1_000_000 )
334- } ) ,
335- Internal ( InternalFixed { val : InternalInternal :: Nanosecond6NoDot } ) => time. map ( |t| {
336- let nano = t. nanosecond ( ) % 1_000_000_000 ;
337- write ! ( w, "{:06}" , nano / 1_000 )
338- } ) ,
339- Internal ( InternalFixed { val : InternalInternal :: Nanosecond9NoDot } ) => time. map ( |t| {
340- let nano = t. nanosecond ( ) % 1_000_000_000 ;
341- write ! ( w, "{:09}" , nano)
342- } ) ,
343- TimezoneName => off. map ( |( name, _) | {
344- w. write_str ( name) ?;
345- Ok ( ( ) )
346- } ) ,
347- TimezoneOffset | TimezoneOffsetZ => off. map ( |& ( _, off) | {
348- OffsetFormat {
349- precision : OffsetPrecision :: Minutes ,
350- colons : Colons :: Maybe ,
351- allow_zulu : * spec == TimezoneOffsetZ ,
352- padding : Pad :: Zero ,
353- }
354- . format ( w, off)
355- } ) ,
356- TimezoneOffsetColon | TimezoneOffsetColonZ => off. map ( |& ( _, off) | {
357- OffsetFormat {
358- precision : OffsetPrecision :: Minutes ,
359- colons : Colons :: Colon ,
360- allow_zulu : * spec == TimezoneOffsetColonZ ,
361- padding : Pad :: Zero ,
362- }
363- . format ( w, off)
364- } ) ,
365- TimezoneOffsetDoubleColon => off. map ( |& ( _, off) | {
366- OffsetFormat {
367- precision : OffsetPrecision :: Seconds ,
368- colons : Colons :: Colon ,
369- allow_zulu : false ,
370- padding : Pad :: Zero ,
371- }
372- . format ( w, off)
373- } ) ,
374- TimezoneOffsetTripleColon => off. map ( |& ( _, off) | {
375- OffsetFormat {
376- precision : OffsetPrecision :: Hours ,
377- colons : Colons :: None ,
378- allow_zulu : false ,
379- padding : Pad :: Zero ,
380- }
381- . format ( w, off)
382- } ) ,
383- Internal ( InternalFixed { val : InternalInternal :: TimezoneOffsetPermissive } ) => {
384- return Err ( fmt:: Error ) ;
385- }
386- RFC2822 =>
387- // same as `%a, %d %b %Y %H:%M:%S %z`
388- {
389- if let ( Some ( d) , Some ( t) , Some ( & ( _, off) ) ) = ( date, time, off) {
390- Some ( write_rfc2822 ( w, crate :: NaiveDateTime :: new ( d, t) , off) )
391- } else {
392- None
393- }
394- }
395- RFC3339 =>
396- // same as `%Y-%m-%dT%H:%M:%S%.f%:z`
397- {
398- if let ( Some ( d) , Some ( t) , Some ( & ( _, off) ) ) = ( date, time, off) {
399- Some ( write_rfc3339 (
400- w,
401- crate :: NaiveDateTime :: new ( d, t) ,
402- off. fix ( ) ,
403- SecondsFormat :: AutoSi ,
404- false ,
405- ) )
406- } else {
407- None
408- }
409- }
410- } ;
411-
412- ret. unwrap_or ( Err ( fmt:: Error ) ) // insufficient arguments for given format
413- }
414-
415421#[ cfg( any( feature = "alloc" , feature = "serde" , feature = "rustc-serialize" ) ) ]
416422impl OffsetFormat {
417423 /// Writes an offset from UTC with the format defined by `self`.
0 commit comments