mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-11-23 20:56:42 +00:00
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:
parent
35b718452e
commit
769fc93df9
12 changed files with 190 additions and 90 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue