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

Flexible offset formatting [1/3] #1082

Merged
merged 3 commits into from
Jun 30, 2023

Conversation

pitdicker
Copy link
Collaborator

There are a lot of issues with our current code to parse time zone offsets. For example %z, %:z, %::z and %:::z are all treated identical in the parser. In some cases Z is accepted, and there is a hack to accept UTC. There is no way to parse seconds.

Formatting is in a better state, but it would be nice if it could be more flexible. For example to be able to print seconds optionally, support padding, or print Z.


This is part 1 of a series of PRs to improve offset formatting and parsing. (1) adds a flexible formatter, (2) adds a flexible parser and fixes current parsing bugs, and (3) exposes the functionality in the format string.

With this PR it is now possible to specify the format of an offset:

  • Is the precision in hours, minutes, seconds, or are minutes and/or seconds optional?
  • Are the values seperated by a colon, nothing, or (when parsing) are both acceptable?
  • Is Z used for +00:00?
  • Should the hour value be zero-padded, space-padded (before the sign), or not be padded?

Copy link
Contributor

@jtmoon79 jtmoon79 left a comment

Choose a reason for hiding this comment

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

LGTM

src/format/mod.rs Outdated Show resolved Hide resolved
src/format/mod.rs Outdated Show resolved Hide resolved
src/format/mod.rs Outdated Show resolved Hide resolved
} else {
locale.am_pm[0]
});
let ret = match *spec {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this in a separate commit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To make it easier for you to see what is changed, and what is caused by rustfmt.

Copy link
Member

Choose a reason for hiding this comment

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

So this is old code that was previously not formatted? What changed that rustfmt is now formatting it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did some experiments at the time, but couldn't pin it down. In #1126 (comment) I run into the same issue.

Copy link
Member

Choose a reason for hiding this comment

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

Want to file a rustfmt issue?

src/format/mod.rs Outdated Show resolved Hide resolved
@pitdicker pitdicker force-pushed the offset_formatting_1 branch 3 times, most recently from e687926 to d27793b Compare June 29, 2023 12:46
src/format/mod.rs Outdated Show resolved Hide resolved
src/format/mod.rs Outdated Show resolved Hide resolved
src/format/mod.rs Outdated Show resolved Hide resolved
@pitdicker pitdicker force-pushed the offset_formatting_1 branch 7 times, most recently from f84e948 to fd0f41b Compare June 30, 2023 16:57
Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

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

Okay, please fix up these final nits (unless you disagree) and feel free to merge.

@@ -23,6 +23,8 @@ use crate::{Datelike, Timelike, Weekday};
#[cfg(feature = "unstable-locales")]
use super::locales;
#[cfg(any(feature = "alloc", feature = "std"))]
use super::{Colons, OffsetFormat, OffsetPrecision};
Copy link
Member

Choose a reason for hiding this comment

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

We can merge these imports, right?

off.map(|&(_, off)| write_local_minus_utc(result, off, true, Colons::None))
}
TimezoneOffset | TimezoneOffsetZ => off.map(|&(_, off)| {
let allow_zulu = *spec == TimezoneOffsetZ;
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer inlining this value in the OffsetFormat::new() call.

.format(result, off)
}),
TimezoneOffsetColon | TimezoneOffsetColonZ => off.map(|&(_, off)| {
let allow_zulu = *spec == TimezoneOffsetColonZ;
Copy link
Member

Choose a reason for hiding this comment

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

And here.


impl OffsetFormat {
/// Create a new `OffsetFormat`.
pub const fn new(
Copy link
Member

Choose a reason for hiding this comment

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

Given that the new() calls will still be spread over the same number of lines in practice, I feel like this method isn't really buying us anything, whereas having the field names in the initializer would make the code a little easier to understand (particularly for the allow_zulu field).

pub struct OffsetFormat {
/// See `OffsetPrecision`.
pub precision: OffsetPrecision,
/// Seperator between hours, minutes and seconds.
Copy link
Member

Choose a reason for hiding this comment

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

One more instance of sepe

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you for being thorough, I thought I had them all.

@pitdicker pitdicker merged commit e0e4303 into chronotope:0.4.x Jun 30, 2023
@pitdicker pitdicker deleted the offset_formatting_1 branch June 30, 2023 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants