1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use filetime::{self, FileTime};
use std::cmp::{self, PartialEq, PartialOrd};
use std::fs;
use std::io;
use std::path::Path;
pub trait Timestamped {
fn timestamp(&self) -> Timestamp;
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct Timestamp { pub secs: u64, pub nsecs: u64 }
#[allow(non_snake_case)]
pub fn Timestamp(ms: u64) -> Timestamp {
Timestamp::new(ms / 1_000, (ms % 1_000) * 1_000_000)
}
impl Timestamp {
pub fn new(secs: u64, ns: u64) -> Timestamp {
Timestamp { secs: secs, nsecs: ns }
}
pub fn to_filetime(&self) -> FileTime {
assert!(self.nsecs < ::std::u32::MAX as u64);
FileTime::from_seconds_since_1970(self.secs, self.nsecs as u32)
}
pub fn to_ms(&self) -> u64 {
self.secs * 1000 + self.nsecs / 1_000_000
}
pub fn set_file_times<P: AsRef<Path>>(&self, p: P) -> io::Result<()> {
let t = self.to_filetime();
filetime::set_file_times(p, t, t)
}
}
impl PartialEq<u64> for Timestamp {
fn eq(&self, other: &u64) -> bool {
self.to_ms().eq(other)
}
}
impl PartialEq<i64> for Timestamp {
fn eq(&self, other: &i64) -> bool {
if *other < 0 {
false
} else {
let other = *other as u64;
self.to_ms().eq(&other)
}
}
}
impl PartialOrd<u64> for Timestamp {
fn partial_cmp(&self, other: &u64) -> Option<cmp::Ordering> {
self.to_ms().partial_cmp(other)
}
}
impl Timestamped for fs::Metadata {
fn timestamp(&self) -> Timestamp {
let ft = FileTime::from_last_modification_time( self );
let s = ft.seconds_relative_to_1970();
let ns = ft.nanoseconds();
Timestamp::new(s as u64, ns as u64)
}
}