Skip to content

Commit

Permalink
Improve format_number to comply with UTS 35 and fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed Sep 29, 2020
1 parent a4fc70b commit d8a9bc4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
45 changes: 41 additions & 4 deletions components/datetime/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,26 @@ where
write_pattern(self.pattern, self.data, self.date_time, f).map_err(|_| std::fmt::Error)
}
}

// Temporary formatting number with length.
fn format_number(
result: &mut impl fmt::Write,
num: usize,
length: &FieldLength,
) -> Result<(), std::fmt::Error> {
debug_assert!((*length as u8) < 3);
write!(result, "{:0>width$}", num, width = (*length as u8) as usize)
match length {
FieldLength::One => write!(result, "{}", num),
FieldLength::TwoDigit => {
if num < 100 {
write!(result, "{:0>width$}", num, width = 2)
} else {
let buffer = num.to_string();
let len = buffer.len();
result.write_str(&buffer[len - 2..])
}
}
length => write!(result, "{:0>width$}", num, width = *length as usize),
}
}

// Temporary simplified function to get the day of the week
Expand Down Expand Up @@ -90,8 +102,7 @@ where
format_number(w, usize::from(date_time.month()) + 1, &field.length)?
}
length => {
let symbol =
data.get_symbol_for_month(month, length, date_time.month().into());
let symbol = data.get_symbol_for_month(month, length, date_time.month());
w.write_str(symbol)?
}
},
Expand Down Expand Up @@ -143,3 +154,29 @@ where
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_format_numer() {
let values = &[2, 20, 201, 2017, 20173];
let samples = &[
(FieldLength::One, ["2", "20", "201", "2017", "20173"]),
(FieldLength::TwoDigit, ["02", "20", "01", "17", "73"]),
(
FieldLength::Abbreviated,
["002", "020", "201", "2017", "20173"],
),
(FieldLength::Wide, ["0002", "0020", "0201", "2017", "20173"]),
];
for (length, expected) in samples {
for (value, expected) in values.iter().zip(expected) {
let mut s = String::new();
format_number(&mut s, *value, length).unwrap();
assert_eq!(s, *expected);
}
}
}
}
2 changes: 1 addition & 1 deletion components/datetime/tests/fixtures/tests/styles.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
}
},
"output": {
"value": "1/30/2000, 10:15 PM"
"value": "1/30/00, 10:15 PM"
}
},
{
Expand Down

0 comments on commit d8a9bc4

Please sign in to comment.