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

fmt: support formatting Timestamp with a specific offset #122

Merged
merged 8 commits into from
Aug 31, 2024
Merged

Conversation

BurntSushi
Copy link
Owner

This PR does a couple things.

First, it adds a new
jiff::fmt::DateTimePrinter::print_timestamp_with_offset routine for
formatting a jiff::Timestamp as an RFC 3339 string with a specific
offset. In particular, this draws a contrast between the existing
print_timestamp method which will always use Zulu time. (Zulu is UTC,
but where the offset is unknown.)

Secondly, we add new helper methods on the datetime and span printers
to format datetimes and spans directly into a string. This is just
for convenience, since otherwise creating a String and passing
it in as a buffer can be a bit annoying. Also, since it's fixed to
a String, these methods are infallible and can never fail, which
further simplifies their use.

This makes it possible to format a `Timestamp` with a specific offset.
This was already technically possible with the `strftime` API, but
adding explicit support for it to the `jiff::fmt::temporal` module seems
like good sense.

Note that no dedicated parsing routine is added for this because it is
already supported automatically.
These are just on the config settings. In newer versions of Rust, I'd
expect these to get cross-crate inlined since they are trivial. And of
course, when used in a const context, it doesn't matter if they get
inlined. But these really should always be inlined, so add the
annotation to allow it in contexts where they might otherwise not.

I was tempted to add inline annotations to basically every other
parse/print routine as well, but I didn't want to overdo it without a
more compelling motivation.
I think this is a big ergonomic win for a very common case. It makes
using the datetime and span printers much nicer, and they are necessary
to use in some cases when the output format needs more configuration.
This mimics the previous commit, but for the RFC 2822 datetime printer.
This provides a more convenient and more easily discoverable interface
to `jiff::fmt::temporal::DateTimePrinter::print_timestamp_with_offset`.
@BurntSushi BurntSushi changed the title fmt/temporal: support formatting Timestamp with a specific offset fmt: support formatting Timestamp with a specific offset Aug 31, 2024
@BurntSushi BurntSushi changed the title fmt: support formatting Timestamp with a specific offset fmt: support formatting Timestamp with a specific offset Aug 31, 2024
This lets us test Rust snippets in the CHANGELOG, which is nice. And
it lets us include the CHANGELOG in the rustdoc output itself, thereby
hopefully increasing visibility.
This was an oversight. Jiff now has two duration types, so this section
basically needed to be entirely rewritten.
@BurntSushi BurntSushi merged commit 464641e into master Aug 31, 2024
17 checks passed
@BurntSushi BurntSushi deleted the ag/stuff branch August 31, 2024 18:52
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.

1 participant