Skip to content

Commit

Permalink
Add SystemTimeExt
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 24, 2023
1 parent 1d43b5d commit 84d07ea
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .config/topic.dic
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
16
17
1G
1M
1ns
APIs
Atomics
Changelog
CHANGELOG
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Added `web` module containing a platform-specific extension trait to
`SystemTime`, allowing conversion from and to `std::time::SystemTime`.

### Changed
- Improve performance of `SystemTime` by using `Duration` internally.

Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi"))
))]
mod web;
mod time;
#[cfg(any(
all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi"))
),
docsrs
))]
pub mod web;

#[cfg(not(all(
target_family = "wasm",
Expand All @@ -123,4 +131,4 @@ pub use std::time::*;
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi"))
))]
pub use self::web::*;
pub use self::time::*;
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/web/system_time.rs → src/time/system_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::Duration;

/// See [`std::time::SystemTime`].
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SystemTime(Duration);
pub struct SystemTime(pub(crate) Duration);

impl SystemTime {
/// See [`std::time::SystemTime::UNIX_EPOCH`].
Expand Down
49 changes: 49 additions & 0 deletions src/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! Platform-specific extensions to `web-time` for the Web platform.

#![allow(clippy::absolute_paths)]

use std::time::SystemTime as StdSystemTime;

use crate::SystemTime;

/// Web-specific extension to [`web_time::SystemTime`](crate::SystemTime).
pub trait SystemTimeExt {
/// Convert [`web_time::SystemTime`](crate::SystemTime) to
/// [`std::time::SystemTime`].
///
/// # Note
///
/// This might give a misleading impression of compatibility!
///
/// Considering this functionality will probably be used to interact with
/// incompatible APIs of other dependencies, care should be taken that the
/// dependency in question doesn't call [`std::time::SystemTime::now()`]
/// internally, which would panic.
fn to_std(self) -> std::time::SystemTime;

/// Convert [`std::time::SystemTime`] to
/// [`web_time::SystemTime`](crate::SystemTime).
///
/// # Note
///
/// This might give a misleading impression of compatibility!
///
/// Considering this functionality will probably be used to interact with
/// incompatible APIs of other dependencies, care should be taken that the
/// dependency in question doesn't call [`std::time::SystemTime::now()`]
/// internally, which would panic.
fn from_std(time: std::time::SystemTime) -> SystemTime;
}

impl SystemTimeExt for SystemTime {
fn to_std(self) -> std::time::SystemTime {
StdSystemTime::UNIX_EPOCH + self.0
}

fn from_std(time: std::time::SystemTime) -> SystemTime {
Self::UNIX_EPOCH
+ time
.duration_since(StdSystemTime::UNIX_EPOCH)
.expect("found `SystemTime` earlier then unix epoch")
}
}

0 comments on commit 84d07ea

Please sign in to comment.