fix: remove system time deps from crates (#1621)

* fix: remove system time deps from crates

* fix: remove system time deps from crates

* fix: smater feature gate

* docs: add some todos

* Update time.rs

* Update Cargo.toml

* build: remove hard dep chrono
This commit is contained in:
Myriad-Dreamin 2025-04-08 01:46:05 +08:00 committed by GitHub
parent 35b718452e
commit 769fc93df9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 190 additions and 90 deletions

View file

@ -1,8 +1,18 @@
//! Cross platform time utilities.
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};
/// Returns the current datetime in utc (UTC+0).
pub fn utc_now() -> UtcDateTime {
now().into()
}
/// Returns the current system time (UTC+0).
#[cfg(any(feature = "system", feature = "web"))]
pub fn now() -> Time {
@ -22,3 +32,30 @@ pub fn now() -> Time {
pub fn now() -> Time {
Time::UNIX_EPOCH
}
/// The trait helping convert to a [`UtcDateTime`].
pub trait ToUtcDateTime {
/// Converts to a [`UtcDateTime`].
fn to_utc_datetime(self) -> Option<UtcDateTime>;
}
impl ToUtcDateTime for i64 {
/// Converts a UNIX timestamp to a [`UtcDateTime`].
fn to_utc_datetime(self) -> Option<UtcDateTime> {
UtcDateTime::from_unix_timestamp(self).ok()
}
}
impl ToUtcDateTime for Time {
/// Converts a system time to a [`UtcDateTime`].
fn to_utc_datetime(self) -> Option<UtcDateTime> {
Some(UtcDateTime::from(self))
}
}
/// Converts a [`UtcDateTime`] to typst's datetime.
#[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)
}