uhrwerk 0.1.0

Simple scheduler for recurring tasks
Documentation
use log::error;
use time::{OffsetDateTime, UtcOffset};

/// Time providers are the source of time used by the scheduler.
///
/// For most purposes, the default [`DefaultTimeProvider`] is sufficient. The main use
/// case for custom time providers is writing tests.
pub trait TimeProvider {
	/// Return the current time with the specified UTC offset.
	fn now(offset: UtcOffset) -> OffsetDateTime;
}

/// The default time provider based on the system clock.
pub struct DefaultTimeProvider;

impl TimeProvider for DefaultTimeProvider {
	/// Return the current time with the specified UTC offset, based on the system clock.
	fn now(offset: UtcOffset) -> OffsetDateTime {
		OffsetDateTime::now_utc().to_offset(offset)
	}
}

pub(crate) fn local_offset() -> UtcOffset {
	UtcOffset::current_local_offset()
		.inspect_err(|err| {
			error!("Failed to determine local offset, falling back to UTC: {err}")
		})
		.unwrap_or(UtcOffset::UTC)
}