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

Add validation/fixes for time formatting strings #5

Merged
merged 4 commits into from
Jul 4, 2023
Merged

Add validation/fixes for time formatting strings #5

merged 4 commits into from
Jul 4, 2023

Conversation

pitdicker
Copy link
Collaborator

@pitdicker pitdicker commented Jul 3, 2023

In chronotope/chrono#1058 in chrono we run into two issues.

Three locales are missing T_FMT_AMPM (ff_SN, km_KH, ug_CN). This makes us unable to use locale_match! for T_FMT_AMPM. We could hack around it, but it seems better to me to fix the locales.
If I look at the three cases, the ones that define AMPM should have "%l:%M:%S %p" and otherwise use "".

The other issue is more of a detail, but something I would appreciate.
Since chronotope/chrono#1152 we try to parse a format string without allocating. For this we switch from parsing the user-supplied format string to a localized format string when it encounters %x, %X or %c (and hopefully soon on %r).
The iterator is able to hold a reference to the original string, and to the localized string that is being parsed.

Now if the localized string references another localized string, such as %r inside %c, we would have to add the ability to switch to parsing a third format string.

It would be great pure-rust-locales could inline the format string of %x, %X and %r into D_T_FMT.
But if this is not an option, we can work around it.

Copy link
Collaborator

@cecton cecton left a comment

Choose a reason for hiding this comment

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

Awesome! It seems to be working nicely

@cecton
Copy link
Collaborator

cecton commented Jul 3, 2023

If you don't mind, can you get someone from chrono who review the PR there to review also this one here? It's just to be sure I'm not missing some functional aspect

@pitdicker
Copy link
Collaborator Author

Good point. To be honest a corresponding PR doesn't exist yet... I'll see what I can do.

@@ -66,3 +68,32 @@ fn main() -> Result<()> {

Ok(())
}

fn validate(objects: &mut Vec<Object>) {
Copy link
Member

Choose a reason for hiding this comment

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

I feel like validate() is not a great name for something that takes a mutable reference?


fn validate_t_fmt_ampm(objects: &mut Vec<Object>) {
for object in objects.iter_mut() {
if object.name == "LC_TIME" {
Copy link
Member

Choose a reason for hiding this comment

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

Can we do an early return for != "LC_TIME" to save on rightward drift?

validate_t_fmt_ampm(objects);
}

fn validate_t_fmt_ampm(objects: &mut Vec<Object>) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a docstring here that explains what this is trying to do and why?

Comment on lines 91 to 92
let value = match am_pm_empty {
true => vec![Value::String(String::new())],
Copy link
Member

Choose a reason for hiding this comment

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

Should we still be pushing a value if !am_pm_empty? The logic here doesn't really make sense to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This duplicates what other locales do which don't have a 12-hour format for the time.
In those locales AM_PM has two empty strings for AM and PM, and T_FMT_AMPM has an empty string.

}
}
for (key, ref mut value) in object.values.iter_mut() {
match (key.as_str(), value) {
Copy link
Member

Choose a reason for hiding this comment

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

Could also do with some comments, I guess?

@pitdicker
Copy link
Collaborator Author

@djc Thank you for reviewing this quickly!

@cecton cecton merged commit eccd659 into chronotope:main Jul 4, 2023
1 check passed
@cecton
Copy link
Collaborator

cecton commented Jul 4, 2023

Released in 0.6.0

@pitdicker
Copy link
Collaborator Author

Thank you for all the work and picking it up this quickly.

@cecton
Copy link
Collaborator

cecton commented Jul 5, 2023

Thanks to you for contributing to the internalization of chrono ❤️ It was a huge hurdle for me back in the days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants