Skip to content

Commit fe43396

Browse files
committed
Make format_fixed a method
1 parent 06a6b01 commit fe43396

File tree

1 file changed

+164
-158
lines changed

1 file changed

+164
-158
lines changed

src/format/formatting.rs

Lines changed: 164 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
125125
#[cfg(feature = "alloc")]
126126
Item::OwnedLiteral(ref s) | Item::OwnedSpace(ref s) => w.write_str(s),
127127
Item::Numeric(ref spec, ref pad) => self.format_numeric(w, spec, pad),
128-
Item::Fixed(ref spec) => {
129-
format_fixed(w, self.date, self.time, self.off.as_ref(), spec, locale)
130-
}
128+
Item::Fixed(ref spec) => self.format_fixed(w, spec, locale),
131129
Item::Error => Err(fmt::Error),
132130
}?;
133131
}
@@ -197,6 +195,169 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
197195
Err(fmt::Error) // insufficient arguments for given format
198196
}
199197
}
198+
199+
#[cfg(feature = "alloc")]
200+
fn format_fixed(
201+
&self,
202+
w: &mut impl Write,
203+
spec: &Fixed,
204+
locale: Option<Locale>,
205+
) -> fmt::Result {
206+
use self::Fixed::*;
207+
208+
let locale = locale.unwrap_or(default_locale());
209+
210+
let ret = match *spec {
211+
ShortMonthName => self.date.map(|d| {
212+
w.write_str(short_months(locale)[d.month0() as usize])?;
213+
Ok(())
214+
}),
215+
LongMonthName => self.date.map(|d| {
216+
w.write_str(long_months(locale)[d.month0() as usize])?;
217+
Ok(())
218+
}),
219+
ShortWeekdayName => self.date.map(|d| {
220+
w.write_str(short_weekdays(locale)[d.weekday().num_days_from_sunday() as usize])?;
221+
Ok(())
222+
}),
223+
LongWeekdayName => self.date.map(|d| {
224+
w.write_str(long_weekdays(locale)[d.weekday().num_days_from_sunday() as usize])?;
225+
Ok(())
226+
}),
227+
LowerAmPm => self.time.map(|t| {
228+
let ampm = if t.hour12().0 { am_pm(locale)[1] } else { am_pm(locale)[0] };
229+
for c in ampm.chars().flat_map(|c| c.to_lowercase()) {
230+
w.write_char(c)?
231+
}
232+
Ok(())
233+
}),
234+
UpperAmPm => self.time.map(|t| {
235+
w.write_str(if t.hour12().0 { am_pm(locale)[1] } else { am_pm(locale)[0] })?;
236+
Ok(())
237+
}),
238+
Nanosecond => self.time.map(|t| {
239+
let nano = t.nanosecond() % 1_000_000_000;
240+
if nano == 0 {
241+
Ok(())
242+
} else {
243+
w.write_str(decimal_point(locale))?;
244+
if nano % 1_000_000 == 0 {
245+
write!(w, "{:03}", nano / 1_000_000)
246+
} else if nano % 1_000 == 0 {
247+
write!(w, "{:06}", nano / 1_000)
248+
} else {
249+
write!(w, "{:09}", nano)
250+
}
251+
}
252+
}),
253+
Nanosecond3 => self.time.map(|t| {
254+
let nano = t.nanosecond() % 1_000_000_000;
255+
w.write_str(decimal_point(locale))?;
256+
write!(w, "{:03}", nano / 1_000_000)
257+
}),
258+
Nanosecond6 => self.time.map(|t| {
259+
let nano = t.nanosecond() % 1_000_000_000;
260+
w.write_str(decimal_point(locale))?;
261+
write!(w, "{:06}", nano / 1_000)
262+
}),
263+
Nanosecond9 => self.time.map(|t| {
264+
let nano = t.nanosecond() % 1_000_000_000;
265+
w.write_str(decimal_point(locale))?;
266+
write!(w, "{:09}", nano)
267+
}),
268+
Internal(InternalFixed { val: InternalInternal::Nanosecond3NoDot }) => {
269+
self.time.map(|t| {
270+
let nano = t.nanosecond() % 1_000_000_000;
271+
write!(w, "{:03}", nano / 1_000_000)
272+
})
273+
}
274+
Internal(InternalFixed { val: InternalInternal::Nanosecond6NoDot }) => {
275+
self.time.map(|t| {
276+
let nano = t.nanosecond() % 1_000_000_000;
277+
write!(w, "{:06}", nano / 1_000)
278+
})
279+
}
280+
Internal(InternalFixed { val: InternalInternal::Nanosecond9NoDot }) => {
281+
self.time.map(|t| {
282+
let nano = t.nanosecond() % 1_000_000_000;
283+
write!(w, "{:09}", nano)
284+
})
285+
}
286+
TimezoneName => self.off.as_ref().map(|(name, _)| {
287+
w.write_str(name)?;
288+
Ok(())
289+
}),
290+
TimezoneOffset | TimezoneOffsetZ => self.off.as_ref().map(|&(_, off)| {
291+
OffsetFormat {
292+
precision: OffsetPrecision::Minutes,
293+
colons: Colons::Maybe,
294+
allow_zulu: *spec == TimezoneOffsetZ,
295+
padding: Pad::Zero,
296+
}
297+
.format(w, off)
298+
}),
299+
TimezoneOffsetColon | TimezoneOffsetColonZ => self.off.as_ref().map(|&(_, off)| {
300+
OffsetFormat {
301+
precision: OffsetPrecision::Minutes,
302+
colons: Colons::Colon,
303+
allow_zulu: *spec == TimezoneOffsetColonZ,
304+
padding: Pad::Zero,
305+
}
306+
.format(w, off)
307+
}),
308+
TimezoneOffsetDoubleColon => self.off.as_ref().map(|&(_, off)| {
309+
OffsetFormat {
310+
precision: OffsetPrecision::Seconds,
311+
colons: Colons::Colon,
312+
allow_zulu: false,
313+
padding: Pad::Zero,
314+
}
315+
.format(w, off)
316+
}),
317+
TimezoneOffsetTripleColon => self.off.as_ref().map(|&(_, off)| {
318+
OffsetFormat {
319+
precision: OffsetPrecision::Hours,
320+
colons: Colons::None,
321+
allow_zulu: false,
322+
padding: Pad::Zero,
323+
}
324+
.format(w, off)
325+
}),
326+
Internal(InternalFixed { val: InternalInternal::TimezoneOffsetPermissive }) => {
327+
return Err(fmt::Error);
328+
}
329+
RFC2822 =>
330+
// same as `%a, %d %b %Y %H:%M:%S %z`
331+
{
332+
if let (Some(d), Some(t), Some(&(_, off))) =
333+
(self.date, self.time, self.off.as_ref())
334+
{
335+
Some(write_rfc2822(w, crate::NaiveDateTime::new(d, t), off))
336+
} else {
337+
None
338+
}
339+
}
340+
RFC3339 =>
341+
// same as `%Y-%m-%dT%H:%M:%S%.f%:z`
342+
{
343+
if let (Some(d), Some(t), Some(&(_, off))) =
344+
(self.date, self.time, self.off.as_ref())
345+
{
346+
Some(write_rfc3339(
347+
w,
348+
crate::NaiveDateTime::new(d, t),
349+
off.fix(),
350+
SecondsFormat::AutoSi,
351+
false,
352+
))
353+
} else {
354+
None
355+
}
356+
}
357+
};
358+
359+
ret.unwrap_or(Err(fmt::Error)) // insufficient arguments for given format
360+
}
200361
}
201362

202363
#[cfg(feature = "alloc")]
@@ -255,161 +416,6 @@ pub fn format_item(
255416
.fmt(w)
256417
}
257418

258-
#[cfg(feature = "alloc")]
259-
fn format_fixed(
260-
w: &mut impl Write,
261-
date: Option<NaiveDate>,
262-
time: Option<NaiveTime>,
263-
off: Option<&(String, FixedOffset)>,
264-
spec: &Fixed,
265-
locale: Option<Locale>,
266-
) -> fmt::Result {
267-
use self::Fixed::*;
268-
269-
let locale = locale.unwrap_or(default_locale());
270-
271-
let ret = match *spec {
272-
ShortMonthName => date.map(|d| {
273-
w.write_str(short_months(locale)[d.month0() as usize])?;
274-
Ok(())
275-
}),
276-
LongMonthName => date.map(|d| {
277-
w.write_str(long_months(locale)[d.month0() as usize])?;
278-
Ok(())
279-
}),
280-
ShortWeekdayName => date.map(|d| {
281-
w.write_str(short_weekdays(locale)[d.weekday().num_days_from_sunday() as usize])?;
282-
Ok(())
283-
}),
284-
LongWeekdayName => date.map(|d| {
285-
w.write_str(long_weekdays(locale)[d.weekday().num_days_from_sunday() as usize])?;
286-
Ok(())
287-
}),
288-
LowerAmPm => time.map(|t| {
289-
let ampm = if t.hour12().0 { am_pm(locale)[1] } else { am_pm(locale)[0] };
290-
for c in ampm.chars().flat_map(|c| c.to_lowercase()) {
291-
w.write_char(c)?
292-
}
293-
Ok(())
294-
}),
295-
UpperAmPm => time.map(|t| {
296-
w.write_str(if t.hour12().0 { am_pm(locale)[1] } else { am_pm(locale)[0] })?;
297-
Ok(())
298-
}),
299-
Nanosecond => time.map(|t| {
300-
let nano = t.nanosecond() % 1_000_000_000;
301-
if nano == 0 {
302-
Ok(())
303-
} else {
304-
w.write_str(decimal_point(locale))?;
305-
if nano % 1_000_000 == 0 {
306-
write!(w, "{:03}", nano / 1_000_000)
307-
} else if nano % 1_000 == 0 {
308-
write!(w, "{:06}", nano / 1_000)
309-
} else {
310-
write!(w, "{:09}", nano)
311-
}
312-
}
313-
}),
314-
Nanosecond3 => time.map(|t| {
315-
let nano = t.nanosecond() % 1_000_000_000;
316-
w.write_str(decimal_point(locale))?;
317-
write!(w, "{:03}", nano / 1_000_000)
318-
}),
319-
Nanosecond6 => time.map(|t| {
320-
let nano = t.nanosecond() % 1_000_000_000;
321-
w.write_str(decimal_point(locale))?;
322-
write!(w, "{:06}", nano / 1_000)
323-
}),
324-
Nanosecond9 => time.map(|t| {
325-
let nano = t.nanosecond() % 1_000_000_000;
326-
w.write_str(decimal_point(locale))?;
327-
write!(w, "{:09}", nano)
328-
}),
329-
Internal(InternalFixed { val: InternalInternal::Nanosecond3NoDot }) => time.map(|t| {
330-
let nano = t.nanosecond() % 1_000_000_000;
331-
write!(w, "{:03}", nano / 1_000_000)
332-
}),
333-
Internal(InternalFixed { val: InternalInternal::Nanosecond6NoDot }) => time.map(|t| {
334-
let nano = t.nanosecond() % 1_000_000_000;
335-
write!(w, "{:06}", nano / 1_000)
336-
}),
337-
Internal(InternalFixed { val: InternalInternal::Nanosecond9NoDot }) => time.map(|t| {
338-
let nano = t.nanosecond() % 1_000_000_000;
339-
write!(w, "{:09}", nano)
340-
}),
341-
TimezoneName => off.map(|(name, _)| {
342-
w.write_str(name)?;
343-
Ok(())
344-
}),
345-
TimezoneOffset | TimezoneOffsetZ => off.map(|&(_, off)| {
346-
OffsetFormat {
347-
precision: OffsetPrecision::Minutes,
348-
colons: Colons::Maybe,
349-
allow_zulu: *spec == TimezoneOffsetZ,
350-
padding: Pad::Zero,
351-
}
352-
.format(w, off)
353-
}),
354-
TimezoneOffsetColon | TimezoneOffsetColonZ => off.map(|&(_, off)| {
355-
OffsetFormat {
356-
precision: OffsetPrecision::Minutes,
357-
colons: Colons::Colon,
358-
allow_zulu: *spec == TimezoneOffsetColonZ,
359-
padding: Pad::Zero,
360-
}
361-
.format(w, off)
362-
}),
363-
TimezoneOffsetDoubleColon => off.map(|&(_, off)| {
364-
OffsetFormat {
365-
precision: OffsetPrecision::Seconds,
366-
colons: Colons::Colon,
367-
allow_zulu: false,
368-
padding: Pad::Zero,
369-
}
370-
.format(w, off)
371-
}),
372-
TimezoneOffsetTripleColon => off.map(|&(_, off)| {
373-
OffsetFormat {
374-
precision: OffsetPrecision::Hours,
375-
colons: Colons::None,
376-
allow_zulu: false,
377-
padding: Pad::Zero,
378-
}
379-
.format(w, off)
380-
}),
381-
Internal(InternalFixed { val: InternalInternal::TimezoneOffsetPermissive }) => {
382-
return Err(fmt::Error);
383-
}
384-
RFC2822 =>
385-
// same as `%a, %d %b %Y %H:%M:%S %z`
386-
{
387-
if let (Some(d), Some(t), Some(&(_, off))) = (date, time, off) {
388-
Some(write_rfc2822(w, crate::NaiveDateTime::new(d, t), off))
389-
} else {
390-
None
391-
}
392-
}
393-
RFC3339 =>
394-
// same as `%Y-%m-%dT%H:%M:%S%.f%:z`
395-
{
396-
if let (Some(d), Some(t), Some(&(_, off))) = (date, time, off) {
397-
Some(write_rfc3339(
398-
w,
399-
crate::NaiveDateTime::new(d, t),
400-
off.fix(),
401-
SecondsFormat::AutoSi,
402-
false,
403-
))
404-
} else {
405-
None
406-
}
407-
}
408-
};
409-
410-
ret.unwrap_or(Err(fmt::Error)) // insufficient arguments for given format
411-
}
412-
413419
#[cfg(any(feature = "alloc", feature = "serde"))]
414420
impl OffsetFormat {
415421
/// Writes an offset from UTC with the format defined by `self`.

0 commit comments

Comments
 (0)