-
Notifications
You must be signed in to change notification settings - Fork 716
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 OffsetTime
formatter as an alternative to LocalTime
without unsoundness
#1771
Comments
This proposal seems reasonable to me, and I'd happily review a PR for it if you open one. 👍 I don't think we should remove the |
Thanks, I'll send a PR (probably this weekend). I didn't explain the "Replace" alternative properly. I did not mean to remove I think adding a separate |
## Motivation Currently, the `time` crate refuses to determine the local timezone offset on Unix unless the program is single-threaded or the `unsound_local_offset` feature is enabled. Because `LocalTime` determines the local timezone offset every time it formats a message, and `tracing` is frequently used in multi-threaded programs, `LocalTime` effectively requires `unsound_local_offset`. `unsound_local_offset` is inconvenient to enable and potentially dangerous. ## Solution Add an `OffsetTime` formatter that formats time with a fixed offset, passed in when it is initialized. Make it convenient to initialize this with the local timezone offset. The main advantage of this formatter over `LocalTime` is that if it is initialized early, it will succeed without `unsound_local_offset`. An additional advantage is that the program can handle errors determining the local offset, because they now occur in the program's initialization code rather than in tracing's formatting code. This is not a drop-in replacement for LocalTime, because it behaves differently if the program keeps running across a DST change. `LocalTime` will pick up the new local offset, while `OffsetTime` will continue to use the offset in effect when the program started. Fixes #1771
## Motivation Currently, the `time` crate refuses to determine the local timezone offset on Unix unless the program is single-threaded or the `unsound_local_offset` feature is enabled. Because `LocalTime` determines the local timezone offset every time it formats a message, and `tracing` is frequently used in multi-threaded programs, `LocalTime` effectively requires `unsound_local_offset`. `unsound_local_offset` is inconvenient to enable and potentially dangerous. ## Solution Add an `OffsetTime` formatter that formats time with a fixed offset, passed in when it is initialized. Make it convenient to initialize this with the local timezone offset. The main advantage of this formatter over `LocalTime` is that if it is initialized early, it will succeed without `unsound_local_offset`. An additional advantage is that the program can handle errors determining the local offset, because they now occur in the program's initialization code rather than in tracing's formatting code. This is not a drop-in replacement for LocalTime, because it behaves differently if the program keeps running across a DST change. `LocalTime` will pick up the new local offset, while `OffsetTime` will continue to use the offset in effect when the program started. Fixes tokio-rs#1771
Feature Request
Crates
tracing-subscriber v0.3.3
Motivation
Provide a workaround for
LocalTime
typically requiring--cfg unsound_local_offset
(as described in #1688) for programs that can accept their tracing output not following timezone changes while the program runs.Proposal
Add an
OffsetTime
formatter that is initialized with a format and a timezone offset. This formats time the same wayLocalTime
does, but with a fixed offset. Make this easy to initialize with the local time's UTC offset. Document that this will typically fail unless called early at startup.Even if initializing
OffsetTime
fails, that failure is easier to handle than it is forLocalTime
: failure occurs when the formatter is initialized (typically at program startup, in application code), not when it is used (when outputting an event, intracing
code).I've implemented a working proof of concept. It needs tests, docstring polish, and some API polish, but I've used this to confirm this approach actually works without
unsound_local_offset
(in a program whereLocalTime
did not).I should be able to turn that code into a proper PR if this proposal is accepted.
Alternatives
LocalTime
with this approach. Keeps the API simpler, but not following DST changes is not ideal.unsound_local_offset
or single-threadedness.unsound_local_offset
is (more or less deliberately) inconvenient to use, and enables unsound behavior. Although the circumstances under which programs are actually unsound are rare, proving they will not occur for any program is difficult, particularly if they link to any non-Rust code.time
crate comes up with a way of avoiding the unsound behavior (Better solution for getting local offset on unix time-rs/time#380). This is not trivial: the most straightforward approach involves reimplementing parsing zoneinfo files, and as far as I know their format and location are both implementation-defined. They are considering other approaches involving forking, but I don't see how those will work without then exec-ing a helper program that tells us our local timezone offset, which does not seem very practical for a low-dependency library liketime
.tracing
to do than it is fortime
, but fortokio
users we might be able to implement something involving a long-running (single-threaded) helper process communicating timezone changes back to the main program. This seems like a lot of complexity (for a feature I personally do not need), but possible.The text was updated successfully, but these errors were encountered: