pub use std::time::SystemTime as Time;
pub use time::UtcDateTime;
#[cfg(not(feature = "web"))]
pub use std::time::{Duration, Instant};
#[cfg(feature = "web")]
pub use web_time::{Duration, Instant};
pub fn utc_now() -> UtcDateTime {
now().into()
}
#[cfg(any(feature = "system", feature = "web"))]
pub fn now() -> Time {
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
{
Time::now()
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
{
use web_time::web::SystemTimeExt;
web_time::SystemTime::now().to_std()
}
}
#[cfg(not(any(feature = "system", feature = "web")))]
pub fn now() -> Time {
Time::UNIX_EPOCH
}
pub trait ToUtcDateTime {
fn to_utc_datetime(self) -> Option<UtcDateTime>;
}
impl ToUtcDateTime for i64 {
fn to_utc_datetime(self) -> Option<UtcDateTime> {
UtcDateTime::from_unix_timestamp(self).ok()
}
}
impl ToUtcDateTime for Time {
fn to_utc_datetime(self) -> Option<UtcDateTime> {
Some(UtcDateTime::from(self))
}
}
#[cfg(feature = "typst")]
pub fn to_typst_time(timestamp: UtcDateTime) -> typst::foundations::Datetime {
let datetime = ::time::PrimitiveDateTime::new(timestamp.date(), timestamp.time());
typst::foundations::Datetime::Datetime(datetime)
}