Skip to content

Commit 5ca22ef

Browse files
committed
Implement --cfg emulate_second_only_system
1 parent 1b58bb1 commit 5ca22ef

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

cap-primitives/src/fs/system_time_spec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ pub enum SystemTimeSpec {
1515

1616
impl SystemTimeSpec {
1717
/// Constructs a new instance of `Self` from the given `fs_set_times::SystemTimeSpec`.
18+
// TODO: Make this a `const fn` once `SystemTime::from_std` is a `const fn`.
1819
#[inline]
19-
pub const fn from_std(std: fs_set_times::SystemTimeSpec) -> Self {
20+
pub fn from_std(std: fs_set_times::SystemTimeSpec) -> Self {
2021
match std {
2122
fs_set_times::SystemTimeSpec::SymbolicNow => SystemTimeSpec::SymbolicNow,
2223
fs_set_times::SystemTimeSpec::Absolute(time) => {

cap-primitives/src/time/system_clock.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ impl SystemClock {
1717
/// This corresponds to [`std::time::SystemTime::UNIX_EPOCH`].
1818
///
1919
/// [`std::time::SystemTime::UNIX_EPOCH`]: https://doc.rust-lang.org/std/time/constant.UNIX_EPOCH.html
20-
pub const UNIX_EPOCH: SystemTime = SystemTime::from_std(time::SystemTime::UNIX_EPOCH);
20+
pub const UNIX_EPOCH: SystemTime = SystemTime {
21+
std: time::SystemTime::UNIX_EPOCH,
22+
};
2123

2224
/// Constructs a new instance of `Self`.
2325
///

cap-primitives/src/time/system_time.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,38 @@ use std::{
1616
/// [`SystemClock::elapsed`] instead. The `UNIX_EPOCH` constant is at
1717
/// [`SystemClock::UNIX_EPOCH`].
1818
///
19+
/// Similar to the [`filetime` crate], when
20+
/// `RUSTFLAGS=--cfg emulate_second_only_system` is set, `SystemTime` will
21+
/// round times from the operating system down to the second. This emulates
22+
/// the behavior of some file systems, mostly
23+
/// [HFS](https://en.wikipedia.org/wiki/HFS_Plus), allowing debugging on other
24+
/// hardware.
25+
///
1926
/// [`std::time::SystemTime`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html
2027
/// [`SystemClock`]: struct.SystemClock.html
2128
/// [`SystemClock::now`]: struct.SystemClock.html#method.now
2229
/// [`SystemClock::elapsed`]: struct.SystemClock.html#method.elapsed
2330
/// [`SystemClock::UNIX_EPOCH`]: struct.SystemClock.html#associatedconstant.UNIX_EPOCH
31+
/// [`filetime` crate]: https://crates.io/crates/filetime
2432
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
2533
pub struct SystemTime {
2634
pub(crate) std: time::SystemTime,
2735
}
2836

2937
impl SystemTime {
3038
/// Constructs a new instance of `Self` from the given `std::time::SystemTime`.
39+
// TODO: Make this a `const fn` once `time::Duration::checked_add` is a `const fn`.
3140
#[inline]
32-
pub const fn from_std(std: time::SystemTime) -> Self {
33-
Self { std }
41+
pub fn from_std(std: time::SystemTime) -> Self {
42+
if cfg!(emulate_second_only_system) {
43+
let duration = std.duration_since(time::SystemTime::UNIX_EPOCH).unwrap();
44+
let secs = time::Duration::from_secs(duration.as_secs());
45+
Self {
46+
std: time::SystemTime::UNIX_EPOCH.checked_add(secs).unwrap(),
47+
}
48+
} else {
49+
Self { std }
50+
}
3451
}
3552

3653
/// Constructs a new instance of `std::time::SystemTime` from the given `Self`.

0 commit comments

Comments
 (0)