-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
349f423
commit 2364e7c
Showing
7 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#[cfg(test)] | ||
use mock_instant::global::SystemTime; | ||
|
||
#[cfg(not(test))] | ||
use std::time::SystemTime; | ||
|
||
/// The number of seconds since the Unix epoch. Returns 0 if the system clock is set before the | ||
/// Unix epoch. | ||
pub(crate) fn unix_timestamp() -> u64 { | ||
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { | ||
Ok(n) => n.as_secs(), | ||
Err(_) => 0, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::time::Duration; | ||
|
||
use mock_instant::global::MockClock; | ||
|
||
#[test] | ||
fn returns_duration_since_unix_epoch() { | ||
MockClock::set_system_time(Duration::from_secs(123)); | ||
assert_eq!(super::unix_timestamp(), 123); | ||
} | ||
|
||
// Ideally, we'd be able to test the case where `duration_since` returns an error, but it | ||
// seems to only happen when system time is set before the Unix epoch, which is not possible | ||
// with the current implementation of `MockClock` because `set_system_time` expects a duration, | ||
// and a duration cannot be negative. | ||
} |
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