Skip to content

Commit dee8313

Browse files
committed
auto merge of #16573 : ruud-v-a/rust/timespec-arithmetic, r=alexcrichton
This implements `Add` and `Sub` for `Timespec`, which enables `Timespec` to be used as a time span. For example: ```rust let begin = get_time(); // Do some stuff. let end = get_time(); let delta = end - begin; println!("Doing stuff took {}.{:09d} seconds.", delta.sec, delta.nsec); ``` This resolves one of the points mentioned in #2153.
2 parents eaf810a + 62b1fbe commit dee8313

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

src/libtime/lib.rs

+67-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -90,6 +90,30 @@ impl Timespec {
9090
}
9191
}
9292

93+
impl Add<Timespec, Timespec> for Timespec {
94+
fn add(&self, other: &Timespec) -> Timespec {
95+
let mut sec = self.sec + other.sec;
96+
let mut nsec = self.nsec + other.nsec;
97+
if nsec >= NSEC_PER_SEC {
98+
nsec -= NSEC_PER_SEC;
99+
sec += 1;
100+
}
101+
Timespec::new(sec, nsec)
102+
}
103+
}
104+
105+
impl Sub<Timespec, Timespec> for Timespec {
106+
fn sub(&self, other: &Timespec) -> Timespec {
107+
let mut sec = self.sec - other.sec;
108+
let mut nsec = self.nsec - other.nsec;
109+
if nsec < 0 {
110+
nsec += NSEC_PER_SEC;
111+
sec -= 1;
112+
}
113+
Timespec::new(sec, nsec)
114+
}
115+
}
116+
93117
/**
94118
* Returns the current time as a `timespec` containing the seconds and
95119
* nanoseconds since 1970-01-01T00:00:00Z.
@@ -1489,6 +1513,46 @@ mod tests {
14891513
assert!(d.gt(c));
14901514
}
14911515

1516+
fn test_timespec_add() {
1517+
let a = Timespec::new(1, 2);
1518+
let b = Timespec::new(2, 3);
1519+
let c = a + b;
1520+
assert_eq!(c.sec, 3);
1521+
assert_eq!(c.nsec, 5);
1522+
1523+
let p = Timespec::new(1, super::NSEC_PER_SEC - 2);
1524+
let q = Timespec::new(2, 2);
1525+
let r = p + q;
1526+
assert_eq!(r.sec, 4);
1527+
assert_eq!(r.nsec, 0);
1528+
1529+
let u = Timespec::new(1, super::NSEC_PER_SEC - 2);
1530+
let v = Timespec::new(2, 3);
1531+
let w = u + v;
1532+
assert_eq!(w.sec, 4);
1533+
assert_eq!(w.nsec, 1);
1534+
}
1535+
1536+
fn test_timespec_sub() {
1537+
let a = Timespec::new(2, 3);
1538+
let b = Timespec::new(1, 2);
1539+
let c = a - b;
1540+
assert_eq!(c.sec, 1);
1541+
assert_eq!(c.nsec, 1);
1542+
1543+
let p = Timespec::new(2, 0);
1544+
let q = Timespec::new(1, 2);
1545+
let r = p - q;
1546+
assert_eq!(r.sec, 0);
1547+
assert_eq!(r.nsec, super::NSEC_PER_SEC - 2);
1548+
1549+
let u = Timespec::new(1, 2);
1550+
let v = Timespec::new(2, 3);
1551+
let w = u - v;
1552+
assert_eq!(w.sec, -2);
1553+
assert_eq!(w.nsec, super::NSEC_PER_SEC - 1);
1554+
}
1555+
14921556
#[test]
14931557
#[ignore(cfg(target_os = "android"))] // FIXME #10958
14941558
fn run_tests() {
@@ -1505,6 +1569,8 @@ mod tests {
15051569
test_ctime();
15061570
test_strftime();
15071571
test_timespec_eq_ord();
1572+
test_timespec_add();
1573+
test_timespec_sub();
15081574
}
15091575

15101576
#[bench]

0 commit comments

Comments
 (0)