Skip to content

Commit

Permalink
fix: dont round off floats while formatting (#679)
Browse files Browse the repository at this point in the history
Co-authored-by: Jacob Pratt <jacob@jhpratt.dev>
  • Loading branch information
dracarys18 and jhpratt authored May 29, 2024
1 parent 38beb4a commit a65ef0e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io;
use std::num::NonZeroU8;

use time::format_description::well_known::iso8601::{DateKind, OffsetPrecision, TimePrecision};
use time::format_description::well_known::{iso8601, Iso8601, Rfc2822, Rfc3339};
Expand Down Expand Up @@ -226,6 +227,26 @@ fn iso_8601() -> time::Result<()> {
Ok(())
}

#[test]
fn iso_8601_issue_678() -> time::Result<()> {
macro_rules! assert_format_config {
($formatted:literal $(, $($config:tt)+)?) => {
assert_eq!(
datetime!(2021-01-02 03:04:05.999_999_999 UTC).format(
&Iso8601::<{ iso8601::Config::DEFAULT$($($config)+)?.encode() }>
)?,
$formatted
);
};
}

assert_format_config!("2021-01-02T03:04:05.999999999Z", .set_time_precision(TimePrecision::Second { decimal_digits: NonZeroU8::new(9) }));
assert_format_config!("2021-01-02T03:04:05.999999Z", .set_time_precision(TimePrecision::Second { decimal_digits: NonZeroU8::new(6) }));
assert_format_config!("2021-01-02T03:04:05.999Z", .set_time_precision(TimePrecision::Second { decimal_digits: NonZeroU8::new(3) }));

Ok(())
}

#[test]
fn format_time() -> time::Result<()> {
let format_output = [
Expand Down
5 changes: 5 additions & 0 deletions time/src/formatting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub(crate) mod formattable;
mod iso8601;

use core::num::NonZeroU8;
use std::io;

Expand Down Expand Up @@ -73,6 +74,10 @@ pub(crate) fn format_float(
) -> io::Result<usize> {
match digits_after_decimal {
Some(digits_after_decimal) => {
// Truncate the decimal points up to the precision
let trunc_num = 10_f64.powi(digits_after_decimal.get().cast_signed().extend());
let value = f64::trunc(value * trunc_num) / trunc_num;

let digits_after_decimal = digits_after_decimal.get().extend();
let width = digits_before_decimal.extend::<usize>() + 1 + digits_after_decimal;
write!(output, "{value:0>width$.digits_after_decimal$}")?;
Expand Down

0 comments on commit a65ef0e

Please sign in to comment.