mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-11-25 12:06:11 -05:00
use jiff crate
This commit is contained in:
@@ -15,17 +15,16 @@ path = "lib.rs"
|
||||
|
||||
[dependencies]
|
||||
ahash = "0.8"
|
||||
chrono = "0.4.23"
|
||||
coded = { git = "https://github.com/scottlamb/coded", rev = "2c97994974a73243d5dd12134831814f42cdb0e8"}
|
||||
futures = "0.3"
|
||||
jiff = { workspace = true }
|
||||
libc = "0.2"
|
||||
nix = { workspace = true }
|
||||
nix = { workspace = true, features = ["time"] }
|
||||
nom = "7.0.0"
|
||||
rusqlite = { workspace = true }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
slab = "0.4"
|
||||
time = "0.1"
|
||||
tracing = { workspace = true }
|
||||
tracing-core = { workspace = true }
|
||||
tracing-log = { workspace = true }
|
||||
|
||||
@@ -3,28 +3,91 @@
|
||||
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
||||
|
||||
//! Clock interface and implementations for testability.
|
||||
//!
|
||||
//! Note these types are in a more standard nanosecond-based format, where
|
||||
//! [`crate::time`] uses Moonfire's 90 kHz time base.
|
||||
|
||||
use std::mem;
|
||||
use nix::sys::time::{TimeSpec, TimeValLike as _};
|
||||
use std::sync::Mutex;
|
||||
use std::sync::{mpsc, Arc};
|
||||
use std::thread;
|
||||
use std::time::Duration as StdDuration;
|
||||
use time::{Duration, Timespec};
|
||||
pub use std::time::Duration;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::error::Error;
|
||||
use crate::shutdown::ShutdownError;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
pub struct SystemTime(pub TimeSpec);
|
||||
|
||||
impl SystemTime {
|
||||
pub fn new(sec: nix::sys::time::time_t, nsec: i64) -> Self {
|
||||
SystemTime(TimeSpec::new(sec, nsec))
|
||||
}
|
||||
|
||||
pub fn as_secs(&self) -> i64 {
|
||||
self.0.num_seconds()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add<Duration> for SystemTime {
|
||||
type Output = SystemTime;
|
||||
|
||||
fn add(self, rhs: Duration) -> SystemTime {
|
||||
SystemTime(self.0 + TimeSpec::from(rhs))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Instant(pub TimeSpec);
|
||||
|
||||
impl Instant {
|
||||
pub fn from_secs(secs: i64) -> Self {
|
||||
Instant(TimeSpec::seconds(secs))
|
||||
}
|
||||
|
||||
pub fn saturating_sub(&self, o: &Instant) -> Duration {
|
||||
if o > self {
|
||||
Duration::default()
|
||||
} else {
|
||||
Duration::from(self.0 - o.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Instant {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: should use saturating always?
|
||||
impl std::ops::Sub<Instant> for Instant {
|
||||
type Output = Duration;
|
||||
|
||||
fn sub(self, rhs: Instant) -> Duration {
|
||||
Duration::from(self.0 - rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add<Duration> for Instant {
|
||||
type Output = Instant;
|
||||
|
||||
fn add(self, rhs: Duration) -> Instant {
|
||||
Instant(self.0 + TimeSpec::from(rhs))
|
||||
}
|
||||
}
|
||||
|
||||
/// Abstract interface to the system clocks. This is for testability.
|
||||
pub trait Clocks: Send + Sync + 'static {
|
||||
/// Gets the current time from `CLOCK_REALTIME`.
|
||||
fn realtime(&self) -> Timespec;
|
||||
fn realtime(&self) -> SystemTime;
|
||||
|
||||
/// Gets the current time from a monotonic clock.
|
||||
///
|
||||
/// On Linux, this uses `CLOCK_BOOTTIME`, which includes suspended time.
|
||||
/// On other systems, it uses `CLOCK_MONOTONIC`.
|
||||
fn monotonic(&self) -> Timespec;
|
||||
fn monotonic(&self) -> Instant;
|
||||
|
||||
/// Causes the current thread to sleep for the specified time.
|
||||
fn sleep(&self, how_long: Duration);
|
||||
@@ -33,7 +96,7 @@ pub trait Clocks: Send + Sync + 'static {
|
||||
fn recv_timeout<T>(
|
||||
&self,
|
||||
rcv: &mpsc::Receiver<T>,
|
||||
timeout: StdDuration,
|
||||
timeout: Duration,
|
||||
) -> Result<T, mpsc::RecvTimeoutError>;
|
||||
}
|
||||
|
||||
@@ -52,7 +115,7 @@ where
|
||||
Err(e) => e.into(),
|
||||
};
|
||||
shutdown_rx.check()?;
|
||||
let sleep_time = Duration::seconds(1);
|
||||
let sleep_time = Duration::from_secs(1);
|
||||
warn!(
|
||||
exception = %e.chain(),
|
||||
"sleeping for 1 s after error"
|
||||
@@ -64,49 +127,38 @@ where
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct RealClocks {}
|
||||
|
||||
impl RealClocks {
|
||||
fn get(&self, clock: libc::clockid_t) -> Timespec {
|
||||
unsafe {
|
||||
let mut ts = mem::MaybeUninit::uninit();
|
||||
assert_eq!(0, libc::clock_gettime(clock, ts.as_mut_ptr()));
|
||||
let ts = ts.assume_init();
|
||||
Timespec::new(
|
||||
// On 32-bit arm builds, `tv_sec` is an `i32` and requires conversion.
|
||||
// On other platforms, the `.into()` is a no-op.
|
||||
#[allow(clippy::useless_conversion)]
|
||||
ts.tv_sec.into(),
|
||||
ts.tv_nsec as i32,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clocks for RealClocks {
|
||||
fn realtime(&self) -> Timespec {
|
||||
self.get(libc::CLOCK_REALTIME)
|
||||
fn realtime(&self) -> SystemTime {
|
||||
SystemTime(
|
||||
nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME)
|
||||
.expect("clock_gettime(REALTIME) should succeed"),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn monotonic(&self) -> Timespec {
|
||||
self.get(libc::CLOCK_BOOTTIME)
|
||||
fn monotonic(&self) -> Instant {
|
||||
Instant(
|
||||
nix::time::clock_gettime(nix::time::ClockId::CLOCK_BOOTTIME)
|
||||
.expect("clock_gettime(BOOTTIME) should succeed"),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn monotonic(&self) -> Timespec {
|
||||
self.get(libc::CLOCK_MONOTONIC)
|
||||
fn monotonic(&self) -> Instant {
|
||||
Instant(
|
||||
nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC)
|
||||
.expect("clock_gettime(MONOTONIC) should succeed"),
|
||||
)
|
||||
}
|
||||
|
||||
fn sleep(&self, how_long: Duration) {
|
||||
match how_long.to_std() {
|
||||
Ok(d) => thread::sleep(d),
|
||||
Err(err) => warn!(%err, "invalid duration {:?}", how_long),
|
||||
};
|
||||
thread::sleep(how_long)
|
||||
}
|
||||
|
||||
fn recv_timeout<T>(
|
||||
&self,
|
||||
rcv: &mpsc::Receiver<T>,
|
||||
timeout: StdDuration,
|
||||
timeout: Duration,
|
||||
) -> Result<T, mpsc::RecvTimeoutError> {
|
||||
rcv.recv_timeout(timeout)
|
||||
}
|
||||
@@ -117,7 +169,7 @@ impl Clocks for RealClocks {
|
||||
pub struct TimerGuard<'a, C: Clocks + ?Sized, S: AsRef<str>, F: FnOnce() -> S + 'a> {
|
||||
clocks: &'a C,
|
||||
label_f: Option<F>,
|
||||
start: Timespec,
|
||||
start: Instant,
|
||||
}
|
||||
|
||||
impl<'a, C: Clocks + ?Sized, S: AsRef<str>, F: FnOnce() -> S + 'a> TimerGuard<'a, C, S, F> {
|
||||
@@ -138,9 +190,9 @@ where
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
let elapsed = self.clocks.monotonic() - self.start;
|
||||
if elapsed.num_seconds() >= 1 {
|
||||
if elapsed.as_secs() >= 1 {
|
||||
let label_f = self.label_f.take().unwrap();
|
||||
warn!("{} took {}!", label_f().as_ref(), elapsed);
|
||||
warn!("{} took {:?}!", label_f().as_ref(), elapsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,42 +202,42 @@ where
|
||||
pub struct SimulatedClocks(Arc<SimulatedClocksInner>);
|
||||
|
||||
struct SimulatedClocksInner {
|
||||
boot: Timespec,
|
||||
boot: SystemTime,
|
||||
uptime: Mutex<Duration>,
|
||||
}
|
||||
|
||||
impl SimulatedClocks {
|
||||
pub fn new(boot: Timespec) -> Self {
|
||||
pub fn new(boot: SystemTime) -> Self {
|
||||
SimulatedClocks(Arc::new(SimulatedClocksInner {
|
||||
boot,
|
||||
uptime: Mutex::new(Duration::seconds(0)),
|
||||
uptime: Mutex::new(Duration::from_secs(0)),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl Clocks for SimulatedClocks {
|
||||
fn realtime(&self) -> Timespec {
|
||||
fn realtime(&self) -> SystemTime {
|
||||
self.0.boot + *self.0.uptime.lock().unwrap()
|
||||
}
|
||||
fn monotonic(&self) -> Timespec {
|
||||
Timespec::new(0, 0) + *self.0.uptime.lock().unwrap()
|
||||
fn monotonic(&self) -> Instant {
|
||||
Instant(TimeSpec::from(*self.0.uptime.lock().unwrap()))
|
||||
}
|
||||
|
||||
/// Advances the clock by the specified amount without actually sleeping.
|
||||
fn sleep(&self, how_long: Duration) {
|
||||
let mut l = self.0.uptime.lock().unwrap();
|
||||
*l = *l + how_long;
|
||||
*l += how_long;
|
||||
}
|
||||
|
||||
/// Advances the clock by the specified amount if data is not immediately available.
|
||||
fn recv_timeout<T>(
|
||||
&self,
|
||||
rcv: &mpsc::Receiver<T>,
|
||||
timeout: StdDuration,
|
||||
timeout: Duration,
|
||||
) -> Result<T, mpsc::RecvTimeoutError> {
|
||||
let r = rcv.recv_timeout(StdDuration::new(0, 0));
|
||||
let r = rcv.recv_timeout(Duration::new(0, 0));
|
||||
if r.is_err() {
|
||||
self.sleep(Duration::from_std(timeout).unwrap());
|
||||
self.sleep(timeout);
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
@@ -14,24 +14,48 @@ use std::fmt;
|
||||
use std::ops;
|
||||
use std::str::FromStr;
|
||||
|
||||
use super::clock::SystemTime;
|
||||
|
||||
type IResult<'a, I, O> = nom::IResult<I, O, nom::error::VerboseError<&'a str>>;
|
||||
|
||||
pub const TIME_UNITS_PER_SEC: i64 = 90_000;
|
||||
|
||||
/// The zone to use for all time handling.
|
||||
///
|
||||
/// In normal operation this is assigned from `jiff::tz::TimeZone::system()` at
|
||||
/// startup, but tests set it to a known political time zone instead.
|
||||
///
|
||||
/// Note that while fresh calls to `jiff::tz::TimeZone::system()` might return
|
||||
/// new values, this time zone is fixed for the entire run. This is important
|
||||
/// for `moonfire_db::days::Map`, where it's expected that adding values and
|
||||
/// then later subtracting them will cancel out.
|
||||
static GLOBAL_ZONE: std::sync::OnceLock<jiff::tz::TimeZone> = std::sync::OnceLock::new();
|
||||
|
||||
pub fn init_zone<F: FnOnce() -> jiff::tz::TimeZone>(f: F) {
|
||||
GLOBAL_ZONE.get_or_init(f);
|
||||
}
|
||||
|
||||
pub fn global_zone() -> jiff::tz::TimeZone {
|
||||
GLOBAL_ZONE
|
||||
.get()
|
||||
.expect("global zone should be initialized")
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// A time specified as 90,000ths of a second since 1970-01-01 00:00:00 UTC.
|
||||
#[derive(Clone, Copy, Default, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
pub struct Time(pub i64);
|
||||
|
||||
/// Returns a parser for a `len`-digit non-negative number which fits into an i32.
|
||||
fn fixed_len_num<'a>(len: usize) -> impl FnMut(&'a str) -> IResult<'a, &'a str, i32> {
|
||||
/// Returns a parser for a `len`-digit non-negative number which fits into `T`.
|
||||
fn fixed_len_num<'a, T: FromStr>(len: usize) -> impl FnMut(&'a str) -> IResult<'a, &'a str, T> {
|
||||
map_res(
|
||||
take_while_m_n(len, len, |c: char| c.is_ascii_digit()),
|
||||
|input: &str| input.parse::<i32>(),
|
||||
|input: &str| input.parse(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Parses `YYYY-mm-dd` into pieces.
|
||||
fn parse_datepart(input: &str) -> IResult<&str, (i32, i32, i32)> {
|
||||
fn parse_datepart(input: &str) -> IResult<&str, (i16, i8, i8)> {
|
||||
tuple((
|
||||
fixed_len_num(4),
|
||||
preceded(tag("-"), fixed_len_num(2)),
|
||||
@@ -40,7 +64,7 @@ fn parse_datepart(input: &str) -> IResult<&str, (i32, i32, i32)> {
|
||||
}
|
||||
|
||||
/// Parses `HH:MM[:SS[:FFFFF]]` into pieces.
|
||||
fn parse_timepart(input: &str) -> IResult<&str, (i32, i32, i32, i32)> {
|
||||
fn parse_timepart(input: &str) -> IResult<&str, (i8, i8, i8, i32)> {
|
||||
let (input, (hr, _, min)) = tuple((fixed_len_num(2), tag(":"), fixed_len_num(2)))(input)?;
|
||||
let (input, stuff) = opt(tuple((
|
||||
preceded(tag(":"), fixed_len_num(2)),
|
||||
@@ -57,16 +81,16 @@ fn parse_zone(input: &str) -> IResult<&str, i32> {
|
||||
map(
|
||||
tuple((
|
||||
opt(nom::character::complete::one_of(&b"+-"[..])),
|
||||
fixed_len_num(2),
|
||||
fixed_len_num::<i32>(2),
|
||||
tag(":"),
|
||||
fixed_len_num(2),
|
||||
fixed_len_num::<i32>(2),
|
||||
)),
|
||||
|(sign, hr, _, min)| {
|
||||
let off = hr * 3600 + min * 60;
|
||||
if sign == Some('-') {
|
||||
off
|
||||
} else {
|
||||
-off
|
||||
} else {
|
||||
off
|
||||
}
|
||||
},
|
||||
),
|
||||
@@ -77,10 +101,6 @@ impl Time {
|
||||
pub const MIN: Self = Time(i64::MIN);
|
||||
pub const MAX: Self = Time(i64::MAX);
|
||||
|
||||
pub fn new(tm: time::Timespec) -> Self {
|
||||
Time(tm.sec * TIME_UNITS_PER_SEC + tm.nsec as i64 * TIME_UNITS_PER_SEC / 1_000_000_000)
|
||||
}
|
||||
|
||||
/// Parses a time as either 90,000ths of a second since epoch or a RFC 3339-like string.
|
||||
///
|
||||
/// The former is 90,000ths of a second since 1970-01-01T00:00:00 UTC, excluding leap seconds.
|
||||
@@ -114,38 +134,22 @@ impl Time {
|
||||
);
|
||||
}
|
||||
let (tm_hour, tm_min, tm_sec, subsec) = opt_time.unwrap_or((0, 0, 0, 0));
|
||||
let mut tm = time::Tm {
|
||||
tm_sec,
|
||||
tm_min,
|
||||
tm_hour,
|
||||
tm_mday,
|
||||
tm_mon,
|
||||
tm_year,
|
||||
tm_wday: 0,
|
||||
tm_yday: 0,
|
||||
tm_isdst: -1,
|
||||
tm_utcoff: 0,
|
||||
tm_nsec: 0,
|
||||
};
|
||||
if tm.tm_mon == 0 {
|
||||
bail!(InvalidArgument, msg("time {input:?} has month 0"));
|
||||
}
|
||||
tm.tm_mon -= 1;
|
||||
if tm.tm_year < 1900 {
|
||||
bail!(InvalidArgument, msg("time {input:?} has year before 1900"));
|
||||
}
|
||||
tm.tm_year -= 1900;
|
||||
|
||||
// The time crate doesn't use tm_utcoff properly; it just calls timegm() if tm_utcoff == 0,
|
||||
// mktime() otherwise. If a zone is specified, use the timegm path and a manual offset.
|
||||
// If no zone is specified, use the tm_utcoff path. This is pretty lame, but follow the
|
||||
// chrono crate's lead and just use 0 or 1 to choose between these functions.
|
||||
let sec = if let Some(off) = opt_zone {
|
||||
tm.to_timespec().sec + i64::from(off)
|
||||
} else {
|
||||
tm.tm_utcoff = 1;
|
||||
tm.to_timespec().sec
|
||||
};
|
||||
let dt = jiff::civil::DateTime::new(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, 0)
|
||||
.map_err(|e| err!(InvalidArgument, source(e)))?;
|
||||
let tz =
|
||||
if let Some(off) = opt_zone {
|
||||
jiff::tz::TimeZone::fixed(jiff::tz::Offset::from_seconds(off).map_err(|e| {
|
||||
err!(InvalidArgument, msg("invalid time zone offset"), source(e))
|
||||
})?)
|
||||
} else {
|
||||
global_zone()
|
||||
};
|
||||
let sec = tz
|
||||
.into_ambiguous_zoned(dt)
|
||||
.compatible()
|
||||
.map_err(|e| err!(InvalidArgument, source(e)))?
|
||||
.timestamp()
|
||||
.as_second();
|
||||
Ok(Time(sec * TIME_UNITS_PER_SEC + i64::from(subsec)))
|
||||
}
|
||||
|
||||
@@ -155,6 +159,18 @@ impl Time {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SystemTime> for Time {
|
||||
fn from(tm: SystemTime) -> Self {
|
||||
Time(tm.0.tv_sec() * TIME_UNITS_PER_SEC + tm.0.tv_nsec() * 9 / 100_000)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<jiff::Timestamp> for Time {
|
||||
fn from(tm: jiff::Timestamp) -> Self {
|
||||
Time((tm.as_nanosecond() * 9 / 100_000) as i64)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for Time {
|
||||
type Err = Error;
|
||||
|
||||
@@ -199,32 +215,39 @@ impl fmt::Debug for Time {
|
||||
|
||||
impl fmt::Display for Time {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let tm = time::at(time::Timespec {
|
||||
sec: self.0 / TIME_UNITS_PER_SEC,
|
||||
nsec: 0,
|
||||
});
|
||||
let zone_minutes = tm.tm_utcoff.abs() / 60;
|
||||
let tm = jiff::Zoned::new(
|
||||
jiff::Timestamp::from_second(self.0 / TIME_UNITS_PER_SEC).map_err(|_| fmt::Error)?,
|
||||
global_zone(),
|
||||
);
|
||||
write!(
|
||||
f,
|
||||
"{}:{:05}{}{:02}:{:02}",
|
||||
tm.strftime("%FT%T").map_err(|_| fmt::Error)?,
|
||||
"{}:{:05}{}",
|
||||
tm.strftime("%FT%T"),
|
||||
self.0 % TIME_UNITS_PER_SEC,
|
||||
if tm.tm_utcoff > 0 { '+' } else { '-' },
|
||||
zone_minutes / 60,
|
||||
zone_minutes % 60
|
||||
tm.strftime("%:z"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A duration specified in 1/90,000ths of a second.
|
||||
/// Durations are typically non-negative, but a `moonfire_db::db::CameraDayValue::duration` may be
|
||||
/// negative.
|
||||
/// Durations are typically non-negative, but a `moonfire_db::db::StreamDayValue::duration` may be
|
||||
/// negative when used as a `<StreamDayValue as Value>::Change`.
|
||||
#[derive(Clone, Copy, Default, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
pub struct Duration(pub i64);
|
||||
|
||||
impl Duration {
|
||||
pub fn to_tm_duration(&self) -> time::Duration {
|
||||
time::Duration::nanoseconds(self.0 * 100000 / 9)
|
||||
impl From<Duration> for jiff::SignedDuration {
|
||||
fn from(d: Duration) -> Self {
|
||||
jiff::SignedDuration::from_nanos(d.0 * 100_000 / 9)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Duration> for std::time::Duration {
|
||||
type Error = std::num::TryFromIntError;
|
||||
|
||||
fn try_from(value: Duration) -> Result<Self, Self::Error> {
|
||||
Ok(std::time::Duration::from_nanos(
|
||||
u64::try_from(value.0)? * 100_000 / 9,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,6 +350,15 @@ impl ops::SubAssign for Duration {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod testutil {
|
||||
pub fn init_zone() {
|
||||
super::init_zone(|| {
|
||||
jiff::tz::TimeZone::get("America/Los_Angeles")
|
||||
.expect("America/Los_Angeles should exist")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Duration, Time, TIME_UNITS_PER_SEC};
|
||||
@@ -334,8 +366,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_parse_time() {
|
||||
std::env::set_var("TZ", "America/Los_Angeles");
|
||||
time::tzset();
|
||||
super::testutil::init_zone();
|
||||
#[rustfmt::skip]
|
||||
let tests = &[
|
||||
("2006-01-02T15:04:05-07:00", 102261550050000),
|
||||
@@ -358,8 +389,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_format_time() {
|
||||
std::env::set_var("TZ", "America/Los_Angeles");
|
||||
time::tzset();
|
||||
super::testutil::init_zone();
|
||||
assert_eq!(
|
||||
"2006-01-02T15:04:05:00000-08:00",
|
||||
format!("{}", Time(102261874050000))
|
||||
|
||||
@@ -17,12 +17,18 @@ use tracing_subscriber::{
|
||||
|
||||
struct FormatSystemd;
|
||||
|
||||
struct ChronoTimer;
|
||||
struct JiffTimer;
|
||||
|
||||
impl FormatTime for ChronoTimer {
|
||||
impl FormatTime for JiffTimer {
|
||||
fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
|
||||
const TIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%S%.6f";
|
||||
write!(w, "{}", chrono::Local::now().format(TIME_FORMAT))
|
||||
|
||||
// Always use the system time zone here, not `base::time::GLOBAL_ZONE`,
|
||||
// to resolve a chicken-and-egg problem. `jiff::tz::TimeZone::system()`
|
||||
// may log an error that is worth seeing. Therefore, we install the
|
||||
// tracing subscriber before initializing `GLOBAL_ZONE`. The latter
|
||||
// only exists to override the zone for tests anyway.
|
||||
write!(w, "{}", jiff::Zoned::now().strftime(TIME_FORMAT))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,7 +145,7 @@ pub fn install() {
|
||||
let sub = tracing_subscriber::registry().with(
|
||||
tracing_subscriber::fmt::Layer::new()
|
||||
.with_writer(std::io::stderr)
|
||||
.with_timer(ChronoTimer)
|
||||
.with_timer(JiffTimer)
|
||||
.with_thread_names(true)
|
||||
.with_filter(filter),
|
||||
);
|
||||
@@ -164,7 +170,7 @@ pub fn install_for_tests() {
|
||||
let sub = tracing_subscriber::registry().with(
|
||||
tracing_subscriber::fmt::Layer::new()
|
||||
.with_test_writer()
|
||||
.with_timer(ChronoTimer)
|
||||
.with_timer(JiffTimer)
|
||||
.with_thread_names(true)
|
||||
.with_filter(filter),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user