Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locales #453

Merged
merged 68 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
f4b9ad8
Initial commit
cecton Jul 11, 2020
d150d93
Implement locales for format functions
cecton Jul 11, 2020
330edb6
WIP
cecton Jul 12, 2020
b4296af
WIP
cecton Jul 12, 2020
3777c97
Merge commit 130253ef2b914d300c1a0c6cff36cd3a5d846767 (no conflict)
cecton Jul 12, 2020
19f54ab
WIP
cecton Jul 12, 2020
870a7cc
WIP
cecton Jul 12, 2020
f27f6ce
WIP
cecton Jul 12, 2020
44e4fb1
WIP
cecton Jul 12, 2020
083575a
WIP
cecton Jul 12, 2020
9b7fd23
WIP
cecton Jul 12, 2020
61c17d7
WIP
cecton Jul 12, 2020
0213867
WIP
cecton Jul 12, 2020
1e6c086
WIP
cecton Jul 17, 2020
6de9c1a
WIP
cecton Jul 17, 2020
d10deae
WIP
cecton Jul 17, 2020
1f9648f
WIP
cecton Jul 17, 2020
26f3c77
WIP
cecton Jul 17, 2020
a65a2b3
WIP
cecton Jul 17, 2020
e4f16d7
WIP
cecton Jul 17, 2020
f32e4f2
WIP
cecton Jul 17, 2020
ee0853b
WIP
cecton Jul 17, 2020
3f2659e
WIP
cecton Jul 17, 2020
9085b05
WIP
cecton Jul 18, 2020
eacc085
WIP
cecton Jul 18, 2020
e6d601a
Update src/format/mod.rs
cecton Jul 18, 2020
aef41e9
Update src/format/mod.rs
cecton Jul 18, 2020
08be423
Update src/format/mod.rs
cecton Jul 18, 2020
a63d0cf
Update src/format/mod.rs
cecton Jul 18, 2020
12ca2c5
Update src/format/mod.rs
cecton Jul 18, 2020
3c02970
WIP
cecton Jul 19, 2020
72c960c
Update src/format/mod.rs
cecton Jul 19, 2020
be0cdd3
WIP
cecton Jul 19, 2020
048b144
WIP
cecton Jul 19, 2020
1acab1d
WIP
cecton Jul 19, 2020
2052de7
WIP
cecton Jul 19, 2020
1f2fd4f
WIP
cecton Jul 19, 2020
d786694
WIP
cecton Jul 19, 2020
1a80aa0
WIP
cecton Jul 19, 2020
3ef34a0
WIP
cecton Jul 19, 2020
3d52936
WIP
cecton Jul 19, 2020
e07be46
WIP
cecton Jul 19, 2020
5986e3a
WIP
cecton Jul 19, 2020
a417628
WIP
cecton Jul 19, 2020
9cb89fa
WIP
cecton Jul 19, 2020
5d1d9f1
WIP
cecton Jul 19, 2020
119c413
WIP
cecton Jul 19, 2020
790f9d5
Small clean-up
cecton Jul 19, 2020
87aa146
WIP
cecton Jul 24, 2020
2d9c1dc
Remove allow dead_code
cecton Jul 24, 2020
1dbb7fa
Merge commit 41345f10a1592ab9cc538a5cc7d7cad983d6a67c (no conflict)
cecton Jul 24, 2020
76fbfc2
Merge commit bfddc1e5fa9d40c1e9d9ab46fecb3bf7f39315e6 (conflicts)
cecton Jul 24, 2020
ffbb600
Merge branch 'locales' of github.com:cecton/chrono into locales
cecton Jul 24, 2020
dc66629
CLEANUP
cecton Jul 24, 2020
ab0c06a
WIP
cecton Jul 24, 2020
c7a491b
WIP
cecton Jul 24, 2020
16d29e4
Merge commit 53e63c3709332647811f1c738d5ba4a13980d439 (no conflict)
cecton Jul 24, 2020
ea3d20e
Update doc
cecton Jul 24, 2020
f6ecdcb
Revert "Update doc"
cecton Jul 26, 2020
2cdb6d3
Revert "WIP"
cecton Jul 26, 2020
05358ae
CLEANUP
cecton Jul 26, 2020
d1ae67b
WIP
cecton Jul 26, 2020
702c640
Merge commit 0579d2af083e3b73164fd799c424b0481c3823e7 (no conflict)
cecton Jul 26, 2020
90b3e8e
doc
cecton Jul 26, 2020
b6c5395
Doc comments
cecton Jul 26, 2020
6ebc477
Merge commit 38810a33bf4581adfc45a31156b77c3078cb04d7 (conflicts)
cecton Jul 30, 2020
6220e7f
Merge commit 370a20cb7130f9df53c2c50b1f965b58ba279729 (no conflict)
cecton Jul 30, 2020
bc96105
Rename locales feature to "unstable-locales" to make it easier to change
quodlibetor Jul 31, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
Forked at: aaee912
Parent branch: chronotope/main
  • Loading branch information
cecton committed Jul 12, 2020
commit 9b7fd232261e6c4cb0e907c8843e6a85766b4707
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ bincode = { version = "0.8.0" }
num-iter = { version = "0.1.35", default-features = false }
criterion = { version = "0.3" }
doc-comment = { version = "0.3" }
pure-rust-locales = { version = "0.2.1" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is necessary here, if chrono fails to build without the locales feature then that probably means that it's not correctly configured out (missing some #[cfg(locales)] functionality).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sorry that was a leftover, fixed


[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies]
wasm-bindgen-test = "0.3"
Expand Down
6 changes: 3 additions & 3 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::ops::{Add, Sub};
use core::{fmt, hash};
use oldtime::Duration as OldDuration;

#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
#[cfg(any(all(feature = "locales", any(feature = "alloc", feature = "std")), test))]
use format::DelayedFormatLocalized;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be #[cfg(feature = "locales")], anything else probably suggests that something is awry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied DelayFormatLocalized from DelayFormat which has all of this. I just added the condition.

chrono/src/format/mod.rs

Lines 683 to 694 in febb8dc

#[cfg(any(feature = "alloc", feature = "std", test))]
#[derive(Debug)]
pub struct DelayedFormat<I> {
/// The date view, if any.
date: Option<NaiveDate>,
/// The time view, if any.
time: Option<NaiveTime>,
/// The name and local-to-UTC difference for the offset (timezone), if any.
off: Option<(String, FixedOffset)>,
/// An iterator returning formatting items.
items: I,
}

I can put it only on locales if you think it's correct (I'm really not sure)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is really necessary as the feature locales won't work without alloc or std. If we reduce to only feature = "locales" it means that building with --no-default-features --features locales will fail with non really descriptive error messages

#[cfg(any(feature = "alloc", feature = "std", test))]
use format::{DelayedFormat, Item, StrftimeItems};
Expand Down Expand Up @@ -299,7 +299,7 @@ where
}

/// Formats the date with the specified formatting items.
#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
#[cfg(any(all(feature = "locales", any(feature = "alloc", feature = "std")), test))]
#[inline]
pub fn format_localized_with_items<'a, I, B>(&self, items: I) -> DelayedFormatLocalized<I>
where
Expand All @@ -312,7 +312,7 @@ where
/// Formats the date with the specified format string.
/// See the [`format::strftime` module](./format/strftime/index.html)
/// on the supported escape sequences.
#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
#[cfg(any(all(feature = "locales", any(feature = "alloc", feature = "std")), test))]
#[inline]
pub fn format_localized<'a>(&self, fmt: &'a str) -> DelayedFormatLocalized<StrftimeItems<'a>> {
self.format_localized_with_items(StrftimeItems::new(fmt))
Expand Down
6 changes: 3 additions & 3 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::string::ToString;
use core::borrow::Borrow;
#[cfg(any(feature = "alloc", feature = "std", test))]
use format::DelayedFormat;
#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
#[cfg(any(all(feature = "locales", any(feature = "alloc", feature = "std")), test))]
use format::DelayedFormatLocalized;
use format::{parse, ParseError, ParseResult, Parsed, StrftimeItems};
use format::{Fixed, Item};
Expand Down Expand Up @@ -497,7 +497,7 @@ where
}

/// Formats the combined date and time with the specified formatting items.
#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
#[cfg(any(all(feature = "locales", any(feature = "alloc", feature = "std")), test))]
#[inline]
pub fn format_localized_with_items<'a, I, B>(&self, items: I) -> DelayedFormatLocalized<I>
where
Expand All @@ -516,7 +516,7 @@ where
/// Formats the combined date and time with the specified format string.
/// See the [`format::strftime` module](./format/strftime/index.html)
/// on the supported escape sequences.
#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
#[cfg(any(all(feature = "locales", any(feature = "alloc", feature = "std")), test))]
#[inline]
pub fn format_localized<'a>(&self, fmt: &'a str) -> DelayedFormatLocalized<StrftimeItems<'a>> {
self.format_localized_with_items(StrftimeItems::new(fmt))
Expand Down
1 change: 1 addition & 0 deletions src/format/locales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{FormatError, FormatErrorKind, FormatResult};
macro_rules! locale_match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice if this function lived in pure-rust-locales instead of here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

($locale:expr => $($item:ident)::+) => {{
match $locale {
"POSIX" => Ok(pure_rust_locales::POSIX::$($item)::+),
"en_US" => Ok(pure_rust_locales::en_US::$($item)::+),
"fr_BE" => Ok(pure_rust_locales::fr_BE::$($item)::+),
// TODO: all the locales are available
Expand Down
30 changes: 15 additions & 15 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use offset::{FixedOffset, Offset};
use {Datelike, Timelike};
use {ParseWeekdayError, Weekday};

#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
#[cfg(any(all(feature = "locales", any(feature = "alloc", feature = "std")), test))]
mod locales;

pub use self::parse::parse;
Expand Down Expand Up @@ -391,7 +391,7 @@ pub struct FormatError(FormatErrorKind);
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
enum FormatErrorKind {
/// The locale is unknown or not available.
#[cfg(feature = "locales")]
#[cfg(any(feature = "locales", test))]
UnknownLocale,

/// Format error.
Expand All @@ -412,7 +412,7 @@ pub type FormatResult<T> = Result<T, FormatError>;
impl fmt::Display for FormatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
#[cfg(feature = "locales")]
#[cfg(any(feature = "locales", test))]
FormatErrorKind::UnknownLocale => write!(f, "the locale is unknown or not available"),
FormatErrorKind::Format => write!(f, "{}", fmt::Error),
FormatErrorKind::InsufficientArguments => {
Expand Down Expand Up @@ -463,18 +463,18 @@ fn format_inner<'a>(
#[allow(unused_variables)] locale: &str,
) -> FormatResult<()> {
// full and abbreviated month and weekday names
#[cfg(feature = "locales")]
#[cfg(any(feature = "locales", test))]
let short_months = locales::short_months(locale)?;
#[cfg(feature = "locales")]
#[cfg(any(feature = "locales", test))]
let long_months = locales::long_months(locale)?;
#[cfg(feature = "locales")]
#[cfg(any(feature = "locales", test))]
let short_weekdays = locales::short_weekdays(locale)?;
#[cfg(feature = "locales")]
#[cfg(any(feature = "locales", test))]
let long_weekdays = locales::long_weekdays(locale)?;
#[cfg(not(feature = "locales"))]
#[cfg(not(any(feature = "locales", test)))]
let short_months =
&["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
#[cfg(not(feature = "locales"))]
#[cfg(not(any(feature = "locales", test)))]
let long_months = &[
"January",
"February",
Expand All @@ -489,9 +489,9 @@ fn format_inner<'a>(
"November",
"December",
];
#[cfg(not(feature = "locales"))]
#[cfg(not(any(feature = "locales", test)))]
let short_weekdays = &["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
#[cfg(not(feature = "locales"))]
#[cfg(not(any(feature = "locales", test)))]
let long_weekdays =
&["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

Expand Down Expand Up @@ -888,7 +888,7 @@ impl FromStr for Weekday {
}

/// Formats single formatting item
#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
#[cfg(any(all(feature = "locales", any(feature = "alloc", feature = "std")), test))]
pub fn format_item_localized<'a>(
w: &mut fmt::Formatter,
date: Option<&NaiveDate>,
Expand All @@ -898,14 +898,14 @@ pub fn format_item_localized<'a>(
locale: &str,
) -> FormatResult<()> {
let mut result = String::new();
format_inner_localized(&mut result, date, time, off, item, locale)?;
format_inner(&mut result, date, time, off, item, locale)?;
w.pad(&result)?;
Ok(())
}

/// Tries to format given arguments with given formatting items.
/// Internally used by `DelayedFormat`.
#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
#[cfg(any(all(feature = "locales", any(feature = "alloc", feature = "std")), test))]
pub fn format_localized<'a, I, B>(
w: &mut fmt::Formatter,
date: Option<&NaiveDate>,
Expand All @@ -920,7 +920,7 @@ where
{
let mut result = String::new();
for item in items {
format_inner_localized(&mut result, date, time, off, item.borrow(), locale)?;
format_inner(&mut result, date, time, off, item.borrow(), locale)?;
}
w.pad(&result)?;
Ok(())
Expand Down
1 change: 0 additions & 1 deletion src/format/strftime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@ fn test_strftime_docs() {
}

#[cfg(test)]
#[cfg(feature = "locales")] // TODO: remove
#[test]
fn test_strftime_docs_localized() {
use {FixedOffset, TimeZone, Timelike};
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ extern crate serde as serdelib;
extern crate doc_comment;
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
extern crate js_sys;
#[cfg(feature = "locales")]
#[cfg(any(feature = "locales", test))]
extern crate pure_rust_locales;
#[cfg(feature = "bench")]
extern crate test;
Expand Down