How do I print a Zoned as an RFC 3339 timestamp with an offset (and no time zone annotation)?
#421
-
|
Is there a way to convert The original use case is fast/logforth#169 (comment) that I'd like to print a timestamp with offset in RFC3339 format. Currently, Logforth writes: let time = match self.tz.clone() {
Some(tz) => Timestamp::now().to_zoned(tz),
None => Zoned::now(),
};
format!("{time:.6} ...")I can foresee a refactor as: let ts = Timestamp::now();
let tz = self.tz.clone().unwrap_or_else(|| TimeZone::system());
let offset = tz.to_offset(ts);
format!("{:.6} ...", ts.display_with_offset(offset));And I'd expect a possible tighter form: let time = match self.tz.clone() {
Some(tz) => Timestamp::now().to_zoned(tz),
None => Zoned::now(),
};
let (ts, tz) = time.to_timestamp_with_timezone();
let offset = tz.to_offset(ts);
format!("{:.6} ...", ts.display_with_offset(offset))Given that it's a reasonable self-reflection over |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Just noticed that Zoned has a |
Beta Was this translation helpful? Give feedback.
-
|
Yeah, the use jiff::Zoned;
fn main() -> anyhow::Result<()> {
let zdt = Zoned::now();
println!("{zdt}");
println!("{}", zdt.timestamp().display_with_offset(zdt.offset()));
Ok(())
}Has this output: |
Beta Was this translation helpful? Give feedback.
Yeah, the
Zoned::offset()method is the key. Bringing it all together:Has this output: