Skip to content

Commit ba1f884

Browse files
GuillaumeGomezfrewsxcv
authored andcommitted
Add time module missing docs
1 parent f7dbec3 commit ba1f884

File tree

1 file changed

+115
-14
lines changed

1 file changed

+115
-14
lines changed

src/libstd/time/mod.rs

+115-14
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub use self::duration::Duration;
3434
mod duration;
3535

3636
/// A measurement of a monotonically increasing clock.
37-
/// Opaque and useful only with `Duration`.
37+
/// Opaque and useful only with `Duration`.
3838
///
3939
/// Instants are always guaranteed to be greater than any previously measured
4040
/// instant when created, and are often useful for tasks such as measuring
@@ -73,23 +73,28 @@ pub struct Instant(time::Instant);
7373
/// A measurement of the system clock, useful for talking to
7474
/// external entities like the file system or other processes.
7575
///
76-
/// Distinct from the `Instant` type, this time measurement **is not
76+
/// Distinct from the [`Instant`] type, this time measurement **is not
7777
/// monotonic**. This means that you can save a file to the file system, then
7878
/// save another file to the file system, **and the second file has a
7979
/// `SystemTime` measurement earlier than the first**. In other words, an
8080
/// operation that happens after another operation in real time may have an
8181
/// earlier `SystemTime`!
8282
///
8383
/// Consequently, comparing two `SystemTime` instances to learn about the
84-
/// duration between them returns a `Result` instead of an infallible `Duration`
84+
/// duration between them returns a [`Result`] instead of an infallible [`Duration`]
8585
/// to indicate that this sort of time drift may happen and needs to be handled.
8686
///
87-
/// Although a `SystemTime` cannot be directly inspected, the `UNIX_EPOCH`
87+
/// Although a `SystemTime` cannot be directly inspected, the [`UNIX_EPOCH`]
8888
/// constant is provided in this module as an anchor in time to learn
8989
/// information about a `SystemTime`. By calculating the duration from this
9090
/// fixed point in time, a `SystemTime` can be converted to a human-readable time,
9191
/// or perhaps some other string representation.
9292
///
93+
/// [`Instant`]: ../../std/time/struct.Instant.html
94+
/// [`Result`]: ../../std/result/enum.Result.html
95+
/// [`Duration`]: ../../std/time/struct.Duration.html
96+
/// [`UNIX_EPOCH`]: ../../std/time/constant.UNIX_EPOCH.html
97+
///
9398
/// Example:
9499
///
95100
/// ```no_run
@@ -117,14 +122,38 @@ pub struct Instant(time::Instant);
117122
#[stable(feature = "time2", since = "1.8.0")]
118123
pub struct SystemTime(time::SystemTime);
119124

120-
/// An error returned from the `duration_since` method on `SystemTime`,
121-
/// used to learn how far in the opposite direction a system time lies.
125+
/// An error returned from the `duration_since` and `elapsed` methods on
126+
/// `SystemTime`, used to learn how far in the opposite direction a system time
127+
/// lies.
128+
///
129+
/// # Examples
130+
///
131+
/// ```no_run
132+
/// use std::thread::sleep;
133+
/// use std::time::{Duration, SystemTime};
134+
///
135+
/// let sys_time = SystemTime::now();
136+
/// sleep(Duration::from_secs(1));
137+
/// let new_sys_time = SystemTime::now();
138+
/// match sys_time.duration_since(new_sys_time) {
139+
/// Ok(_) => {}
140+
/// Err(e) => println!("SystemTimeError difference: {:?}", e.duration()),
141+
/// }
142+
/// ```
122143
#[derive(Clone, Debug)]
123144
#[stable(feature = "time2", since = "1.8.0")]
124145
pub struct SystemTimeError(Duration);
125146

126147
impl Instant {
127148
/// Returns an instant corresponding to "now".
149+
///
150+
/// # Examples
151+
///
152+
/// ```
153+
/// use std::time::Instant;
154+
///
155+
/// let now = Instant::now();
156+
/// ```
128157
#[stable(feature = "time2", since = "1.8.0")]
129158
pub fn now() -> Instant {
130159
Instant(time::Instant::now())
@@ -138,6 +167,18 @@ impl Instant {
138167
/// only be possible if `earlier` was created after `self`. Because
139168
/// `Instant` is monotonic, the only time that this should happen should be
140169
/// a bug.
170+
///
171+
/// # Examples
172+
///
173+
/// ```no_run
174+
/// use std::time::{Duration, Instant};
175+
/// use std::thread::sleep;
176+
///
177+
/// let now = Instant::now();
178+
/// sleep(Duration::new(1, 0));
179+
/// let new_now = Instant::now();
180+
/// println!("{:?}", new_now.duration_since(now));
181+
/// ```
141182
#[stable(feature = "time2", since = "1.8.0")]
142183
pub fn duration_since(&self, earlier: Instant) -> Duration {
143184
self.0.sub_instant(&earlier.0)
@@ -218,6 +259,14 @@ impl fmt::Debug for Instant {
218259

219260
impl SystemTime {
220261
/// Returns the system time corresponding to "now".
262+
///
263+
/// # Examples
264+
///
265+
/// ```
266+
/// use std::time::SystemTime;
267+
///
268+
/// let sys_time = SystemTime::now();
269+
/// ```
221270
#[stable(feature = "time2", since = "1.8.0")]
222271
pub fn now() -> SystemTime {
223272
SystemTime(time::SystemTime::now())
@@ -229,11 +278,26 @@ impl SystemTime {
229278
/// guaranteed to always be before later measurements (due to anomalies such
230279
/// as the system clock being adjusted either forwards or backwards).
231280
///
232-
/// If successful, `Ok(Duration)` is returned where the duration represents
281+
/// If successful, [`Ok`]`(`[`Duration`]`)` is returned where the duration represents
233282
/// the amount of time elapsed from the specified measurement to this one.
234283
///
235-
/// Returns an `Err` if `earlier` is later than `self`, and the error
284+
/// Returns an [`Err`] if `earlier` is later than `self`, and the error
236285
/// contains how far from `self` the time is.
286+
///
287+
/// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
288+
/// [`Duration`]: ../../std/time/struct.Duration.html
289+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
290+
///
291+
/// # Examples
292+
///
293+
/// ```
294+
/// use std::time::SystemTime;
295+
///
296+
/// let sys_time = SystemTime::now();
297+
/// let difference = sys_time.duration_since(sys_time)
298+
/// .expect("SystemTime::duration_since failed");
299+
/// println!("{:?}", difference);
300+
/// ```
237301
#[stable(feature = "time2", since = "1.8.0")]
238302
pub fn duration_since(&self, earlier: SystemTime)
239303
-> Result<Duration, SystemTimeError> {
@@ -244,12 +308,28 @@ impl SystemTime {
244308
///
245309
/// This function may fail as the underlying system clock is susceptible to
246310
/// drift and updates (e.g. the system clock could go backwards), so this
247-
/// function may not always succeed. If successful, `Ok(duration)` is
311+
/// function may not always succeed. If successful, [`Ok`]`(`[`Duration`]`)` is
248312
/// returned where the duration represents the amount of time elapsed from
249313
/// this time measurement to the current time.
250314
///
251-
/// Returns an `Err` if `self` is later than the current system time, and
315+
/// Returns an [`Err`] if `self` is later than the current system time, and
252316
/// the error contains how far from the current system time `self` is.
317+
///
318+
/// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
319+
/// [`Duration`]: ../../std/time/struct.Duration.html
320+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
321+
///
322+
/// # Examples
323+
///
324+
/// ```no_run
325+
/// use std::thread::sleep;
326+
/// use std::time::{Duration, SystemTime};
327+
///
328+
/// let sys_time = SystemTime::now();
329+
/// let one_sec = Duration::from_secs(1);
330+
/// sleep(one_sec);
331+
/// assert!(sys_time.elapsed().unwrap() >= one_sec);
332+
/// ```
253333
#[stable(feature = "time2", since = "1.8.0")]
254334
pub fn elapsed(&self) -> Result<Duration, SystemTimeError> {
255335
SystemTime::now().duration_since(*self)
@@ -300,19 +380,40 @@ impl fmt::Debug for SystemTime {
300380
///
301381
/// This constant is defined to be "1970-01-01 00:00:00 UTC" on all systems with
302382
/// respect to the system clock. Using `duration_since` on an existing
303-
/// `SystemTime` instance can tell how far away from this point in time a
383+
/// [`SystemTime`] instance can tell how far away from this point in time a
304384
/// measurement lies, and using `UNIX_EPOCH + duration` can be used to create a
305-
/// `SystemTime` instance to represent another fixed point in time.
385+
/// [`SystemTime`] instance to represent another fixed point in time.
386+
///
387+
/// [`SystemTime`]: ../../std/time/struct.SystemTime.html
306388
#[stable(feature = "time2", since = "1.8.0")]
307389
pub const UNIX_EPOCH: SystemTime = SystemTime(time::UNIX_EPOCH);
308390

309391
impl SystemTimeError {
310392
/// Returns the positive duration which represents how far forward the
311393
/// second system time was from the first.
312394
///
313-
/// A `SystemTimeError` is returned from the `duration_since`
314-
/// operation whenever the second system time represents a point later
395+
/// A `SystemTimeError` is returned from the [`duration_since`] and [`elapsed`]
396+
/// methods of [`SystemTime`] whenever the second system time represents a point later
315397
/// in time than the `self` of the method call.
398+
///
399+
/// [`duration_since`]: ../../std/time/struct.SystemTime.html#method.duration_since
400+
/// [`elapsed`]: ../../std/time/struct.SystemTime.html#method.elapsed
401+
/// [`SystemTime`]: ../../std/time/struct.SystemTime.html
402+
///
403+
/// # Examples
404+
///
405+
/// ```no_run
406+
/// use std::thread::sleep;
407+
/// use std::time::{Duration, SystemTime};
408+
///
409+
/// let sys_time = SystemTime::now();
410+
/// sleep(Duration::from_secs(1));
411+
/// let new_sys_time = SystemTime::now();
412+
/// match sys_time.duration_since(new_sys_time) {
413+
/// Ok(_) => {}
414+
/// Err(e) => println!("SystemTimeError difference: {:?}", e.duration()),
415+
/// }
416+
/// ```
316417
#[stable(feature = "time2", since = "1.8.0")]
317418
pub fn duration(&self) -> Duration {
318419
self.0

0 commit comments

Comments
 (0)