Skip to content

Commit

Permalink
Avoid timezone look-ups on Unix given they always fail
Browse files Browse the repository at this point in the history
Due to time-rs/time#380

Signed-off-by: Jesse Szwedko <[email protected]>
  • Loading branch information
jszwedko authored and Geal committed Dec 5, 2021
1 parent bd997a6 commit 839eb52
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<T: Display> LogFormat<T> for Formatter3164 {
w,
"<{}>{} {} {}[{}]: {}",
encode_priority(severity, self.facility),
time::OffsetDateTime::now_local()
now_local()
.map(|timestamp| timestamp.format(&format).unwrap())
.unwrap(),
hostname,
Expand All @@ -91,7 +91,7 @@ impl<T: Display> LogFormat<T> for Formatter3164 {
w,
"<{}>{} {}[{}]: {}",
encode_priority(severity, self.facility),
time::OffsetDateTime::now_local()
now_local()
.map(|timestamp| timestamp.format(&format).unwrap())
.unwrap(),
self.process,
Expand Down Expand Up @@ -225,6 +225,18 @@ fn encode_priority(severity: Severity, facility: Facility) -> Priority {
facility as u8 | severity as u8
}

#[cfg(unix)]
// On unix platforms, time::OffsetDateTime::now_local always returns an error so use UTC instead
// https://github.com/time-rs/time/issues/380
fn now_local() -> std::result::Result<time::OffsetDateTime, time::error::IndeterminateOffset> {
Ok(time::OffsetDateTime::now_utc())
}

#[cfg(not(unix))]
fn now_local() -> std::result::Result<time::OffsetDateTime, time::error::IndeterminateOffset> {
time::OffsetDateTime::now_local()
}

#[test]
fn test_formatter3164_defaults() {
let d = Formatter3164::default();
Expand Down

0 comments on commit 839eb52

Please sign in to comment.