-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SystemHooks trait and adjust now as needed
- Loading branch information
Showing
6 changed files
with
110 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,42 @@ | ||
use alloc::string::{String, ToString}; | ||
use alloc::string::String; | ||
|
||
use crate::{TemporalError, TemporalResult}; | ||
use crate::{time::EpochNanoseconds, TemporalResult}; | ||
|
||
#[cfg(feature = "sys")] | ||
use crate::TemporalError; | ||
#[cfg(feature = "sys")] | ||
use alloc::string::ToString; | ||
#[cfg(feature = "sys")] | ||
use web_time::{SystemTime, UNIX_EPOCH}; | ||
|
||
// TODO: Need to implement SystemTime handling for non_std. | ||
|
||
pub trait SystemHooks { | ||
/// Returns the current system time in `EpochNanoseconds`. | ||
fn get_system_nanoseconds(&self) -> TemporalResult<EpochNanoseconds>; | ||
/// Returns the current system IANA Time Zone Identifier. | ||
fn get_system_time_zone(&self) -> TemporalResult<String>; | ||
} | ||
|
||
#[cfg(feature = "sys")] | ||
pub struct DefaultSystemHooks; | ||
|
||
#[cfg(feature = "sys")] | ||
impl SystemHooks for DefaultSystemHooks { | ||
fn get_system_nanoseconds(&self) -> TemporalResult<EpochNanoseconds> { | ||
EpochNanoseconds::try_from(get_system_nanoseconds()?) | ||
} | ||
|
||
fn get_system_time_zone(&self) -> TemporalResult<String> { | ||
iana_time_zone::get_timezone().map_err(|e| TemporalError::general(e.to_string())) | ||
} | ||
} | ||
|
||
/// Returns the system time in nanoseconds. | ||
#[cfg(feature = "now")] | ||
#[cfg(feature = "sys")] | ||
pub(crate) fn get_system_nanoseconds() -> TemporalResult<u128> { | ||
SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.map_err(|e| TemporalError::general(e.to_string())) | ||
.map(|d| d.as_nanos()) | ||
} | ||
|
||
/// Returns the system tz identifier | ||
pub(crate) fn get_system_tz_identifier() -> TemporalResult<String> { | ||
iana_time_zone::get_timezone().map_err(|e| TemporalError::general(e.to_string())) | ||
} |