Skip to content

Commit

Permalink
Localize decimal point
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jul 3, 2023
1 parent bd23f5b commit 720eb41
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
20 changes: 19 additions & 1 deletion src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::offset::{FixedOffset, TimeZone, Utc};
#[cfg(feature = "clock")]
use crate::offset::{Local, Offset};
use crate::oldtime::Duration;
use crate::{Datelike, Days, LocalResult, Months, NaiveDateTime};
use crate::{Datelike, Days, LocalResult, Months, NaiveDateTime, Timelike};

#[derive(Clone)]
struct DstTester;
Expand Down Expand Up @@ -987,3 +987,21 @@ fn test_auto_conversion() {
let utc_dt2: DateTime<Utc> = cdt_dt.into();
assert_eq!(utc_dt, utc_dt2);
}

#[test]
#[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))]
fn locale_decimal_point() {
use crate::Locale::{ar_SY, nl_NL};
let dt =
Utc.with_ymd_and_hms(2018, 9, 5, 18, 58, 0).unwrap().with_nanosecond(123456780).unwrap();

assert_eq!(dt.format_localized("%T%.f", nl_NL).to_string(), "18:58:00,123456780");
assert_eq!(dt.format_localized("%T%.3f", nl_NL).to_string(), "18:58:00,123");
assert_eq!(dt.format_localized("%T%.6f", nl_NL).to_string(), "18:58:00,123456");
assert_eq!(dt.format_localized("%T%.9f", nl_NL).to_string(), "18:58:00,123456780");

assert_eq!(dt.format_localized("%T%.f", ar_SY).to_string(), "18:58:00.123456780");
assert_eq!(dt.format_localized("%T%.3f", ar_SY).to_string(), "18:58:00.123");
assert_eq!(dt.format_localized("%T%.6f", ar_SY).to_string(), "18:58:00.123456");
assert_eq!(dt.format_localized("%T%.9f", ar_SY).to_string(), "18:58:00.123456780");
}
25 changes: 17 additions & 8 deletions src/format/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Locales {
short_weekdays: &'static [&'static str],
long_weekdays: &'static [&'static str],
am_pm: &'static [&'static str],
decimal_point: &'static str,
}

#[cfg(any(feature = "alloc", feature = "std"))]
Expand All @@ -49,6 +50,7 @@ impl Locales {
short_weekdays: locales::short_weekdays(locale),
long_weekdays: locales::long_weekdays(locale),
am_pm: locales::am_pm(locale),
decimal_point: locales::decimal_point(locale),
}
}
#[cfg(not(feature = "unstable-locales"))]
Expand Down Expand Up @@ -81,6 +83,7 @@ impl Locales {
"Saturday",
],
am_pm: &["AM", "PM"],
decimal_point: ".",
}
}
}
Expand Down Expand Up @@ -387,25 +390,31 @@ fn format_inner(
let nano = t.nanosecond() % 1_000_000_000;
if nano == 0 {
Ok(())
} else if nano % 1_000_000 == 0 {
write!(result, ".{:03}", nano / 1_000_000)
} else if nano % 1_000 == 0 {
write!(result, ".{:06}", nano / 1_000)
} else {
write!(result, ".{:09}", nano)
result.push_str(locale.decimal_point);
if nano % 1_000_000 == 0 {
write!(result, "{:03}", nano / 1_000_000)
} else if nano % 1_000 == 0 {
write!(result, "{:06}", nano / 1_000)
} else {
write!(result, "{:09}", nano)
}
}
}),
Nanosecond3 => time.map(|t| {
let nano = t.nanosecond() % 1_000_000_000;
write!(result, ".{:03}", nano / 1_000_000)
result.push_str(locale.decimal_point);
write!(result, "{:03}", nano / 1_000_000)
}),
Nanosecond6 => time.map(|t| {
let nano = t.nanosecond() % 1_000_000_000;
write!(result, ".{:06}", nano / 1_000)
result.push_str(locale.decimal_point);
write!(result, "{:06}", nano / 1_000)
}),
Nanosecond9 => time.map(|t| {
let nano = t.nanosecond() % 1_000_000_000;
write!(result, ".{:09}", nano)
result.push_str(locale.decimal_point);
write!(result, "{:09}", nano)
}),
Internal(InternalFixed { val: InternalInternal::Nanosecond3NoDot }) => {
time.map(|t| {
Expand Down
4 changes: 4 additions & 0 deletions src/format/locales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub(crate) const fn am_pm(locale: Locale) -> &'static [&'static str] {
locale_match!(locale => LC_TIME::AM_PM)
}

pub(crate) const fn decimal_point(locale: Locale) -> &'static str {
locale_match!(locale => LC_NUMERIC::DECIMAL_POINT)
}

pub(crate) const fn d_fmt(locale: Locale) -> &'static str {
locale_match!(locale => LC_TIME::D_FMT)
}
Expand Down

0 comments on commit 720eb41

Please sign in to comment.