@@ -16,21 +16,38 @@ use std::{
16
16
/// [`SystemClock::elapsed`] instead. The `UNIX_EPOCH` constant is at
17
17
/// [`SystemClock::UNIX_EPOCH`].
18
18
///
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
+ ///
19
26
/// [`std::time::SystemTime`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html
20
27
/// [`SystemClock`]: struct.SystemClock.html
21
28
/// [`SystemClock::now`]: struct.SystemClock.html#method.now
22
29
/// [`SystemClock::elapsed`]: struct.SystemClock.html#method.elapsed
23
30
/// [`SystemClock::UNIX_EPOCH`]: struct.SystemClock.html#associatedconstant.UNIX_EPOCH
31
+ /// [`filetime` crate]: https://crates.io/crates/filetime
24
32
#[ derive( Clone , Copy , Eq , PartialEq , Hash , Ord , PartialOrd ) ]
25
33
pub struct SystemTime {
26
34
pub ( crate ) std : time:: SystemTime ,
27
35
}
28
36
29
37
impl SystemTime {
30
38
/// 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`.
31
40
#[ 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
+ }
34
51
}
35
52
36
53
/// Constructs a new instance of `std::time::SystemTime` from the given `Self`.
0 commit comments