Skip to content

Commit

Permalink
Switch to new formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed May 24, 2023
1 parent c7fb640 commit f478695
Showing 1 changed file with 48 additions and 58 deletions.
106 changes: 48 additions & 58 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,6 @@ enum InternalInternal {
Nanosecond9NoDot,
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Colons {
None,
Single,
Double,
Triple,
}

/// A single formatting item. This is used for both formatting and parsing.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum Item<'a> {
Expand Down Expand Up @@ -800,19 +791,41 @@ fn format_inner(
result.push_str(name);
Ok(())
}),
TimezoneOffsetColon => off
.map(|&(_, off)| write_local_minus_utc(result, off, false, Colons::Single)),
TimezoneOffsetDoubleColon => off
.map(|&(_, off)| write_local_minus_utc(result, off, false, Colons::Double)),
TimezoneOffsetTripleColon => off
.map(|&(_, off)| write_local_minus_utc(result, off, false, Colons::Triple)),
TimezoneOffsetColonZ => off
.map(|&(_, off)| write_local_minus_utc(result, off, true, Colons::Single)),
TimezoneOffset => {
off.map(|&(_, off)| write_local_minus_utc(result, off, false, Colons::None))
TimezoneOffset | TimezoneOffsetZ => {
let format = UtcOffsetFormat {
precision: OffsetPrecision::Minutes,
colons: Colon::Maybe,
allow_zulu: *spec == TimezoneOffsetZ,
padding: Pad::Zero,
};
off.map(|&(_, off)| format.format(result, off))
}
TimezoneOffsetColon | TimezoneOffsetColonZ => {
let format = UtcOffsetFormat {
precision: OffsetPrecision::Minutes,
colons: Colon::Colon,
allow_zulu: *spec == TimezoneOffsetColonZ,
padding: Pad::Zero,
};
off.map(|&(_, off)| format.format(result, off))
}
TimezoneOffsetZ => {
off.map(|&(_, off)| write_local_minus_utc(result, off, true, Colons::None))
TimezoneOffsetDoubleColon => {
let format = UtcOffsetFormat {
precision: OffsetPrecision::Seconds,
colons: Colon::Colon,
allow_zulu: false,
padding: Pad::Zero,
};
off.map(|&(_, off)| format.format(result, off))
}
TimezoneOffsetTripleColon => {
let format = UtcOffsetFormat {
precision: OffsetPrecision::Hours,
colons: Colon::None,
allow_zulu: false,
padding: Pad::Zero,
};
off.map(|&(_, off)| format.format(result, off))
}
Internal(InternalFixed { val: InternalInternal::TimezoneOffsetPermissive }) => {
panic!("Do not try to write %#z it is undefined")
Expand Down Expand Up @@ -848,41 +861,6 @@ fn format_inner(
Ok(())
}

/// Prints an offset from UTC in the format of `+HHMM` or `+HH:MM`.
/// `Z` instead of `+00[:]00` is allowed when `allow_zulu` is true.
#[cfg(any(feature = "alloc", feature = "std", test))]
fn write_local_minus_utc(
result: &mut String,
off: FixedOffset,
allow_zulu: bool,
colon_type: Colons,
) -> fmt::Result {
let off = off.local_minus_utc();
if allow_zulu && off == 0 {
result.push('Z');
return Ok(());
}
let (sign, off) = if off < 0 { ('-', -off) } else { ('+', off) };
result.push(sign);

write_hundreds(result, (off / 3600) as u8)?;

match colon_type {
Colons::None => write_hundreds(result, (off / 60 % 60) as u8),
Colons::Single => {
result.push(':');
write_hundreds(result, (off / 60 % 60) as u8)
}
Colons::Double => {
result.push(':');
write_hundreds(result, (off / 60 % 60) as u8)?;
result.push(':');
write_hundreds(result, (off % 60) as u8)
}
Colons::Triple => Ok(()),
}
}

/// Writes the date, time and offset to the string. same as `%Y-%m-%dT%H:%M:%S%.f%:z`
#[cfg(any(feature = "alloc", feature = "std", test))]
pub(crate) fn write_rfc3339(
Expand All @@ -893,7 +871,13 @@ pub(crate) fn write_rfc3339(
// reuse `Debug` impls which already print ISO 8601 format.
// this is faster in this way.
write!(result, "{:?}", dt)?;
write_local_minus_utc(result, off, false, Colons::Single)
let offset_format = UtcOffsetFormat {
precision: OffsetPrecision::Minutes,
colons: Colon::Colon,
allow_zulu: false,
padding: Pad::Zero,
};
offset_format.format(result, off)
}

#[cfg(any(feature = "alloc", feature = "std", test))]
Expand Down Expand Up @@ -937,7 +921,13 @@ fn write_rfc2822_inner(
let sec = t.second() + t.nanosecond() / 1_000_000_000;
write_hundreds(result, sec as u8)?;
result.push(' ');
write_local_minus_utc(result, off, false, Colons::None)
let offset_format = UtcOffsetFormat {
precision: OffsetPrecision::Minutes,
colons: Colon::None,
allow_zulu: false,
padding: Pad::Zero,
};
offset_format.format(result, off)
}

/// Equivalent to `{:02}` formatting for n < 100.
Expand Down

0 comments on commit f478695

Please sign in to comment.