Skip to content

Commit 0c276da

Browse files
committed
Merge pull request #4575 from cpeterso/timespec-nsec-comment
Add Timespec comment and assert about negative nsec
2 parents d3bb499 + f5a3ce6 commit 0c276da

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libstd/time.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use core::prelude::*;
2020
use core::result::{Result, Ok, Err};
2121
use core::str;
2222

23+
const NSEC_PER_SEC: i32 = 1_000_000_000_i32;
24+
2325
#[abi = "cdecl"]
2426
extern mod rustrt {
2527
#[legacy_exports]
@@ -40,8 +42,17 @@ extern mod rustrt {
4042
#[auto_decode]
4143
pub struct Timespec { sec: i64, nsec: i32 }
4244

45+
/*
46+
* Timespec assumes that pre-epoch Timespecs have negative sec and positive
47+
* nsec fields. Darwin's and Linux's struct timespec functions handle pre-
48+
* epoch timestamps using a "two steps back, one step forward" representation,
49+
* though the man pages do not actually document this. For example, the time
50+
* -1.2 seconds before the epoch is represented by `Timespec { sec: -2_i64,
51+
* nsec: 800_000_000_i32 }`.
52+
*/
4353
impl Timespec {
4454
static pure fn new(sec: i64, nsec: i32) -> Timespec {
55+
assert nsec >= 0 && nsec < NSEC_PER_SEC;
4556
Timespec { sec: sec, nsec: nsec }
4657
}
4758
}

0 commit comments

Comments
 (0)