Coverage Report

Created: 2026-03-31 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/humantime-2.3.0/src/wrapper.rs
Line
Count
Source
1
use std::fmt;
2
use std::ops::Deref;
3
use std::str::FromStr;
4
use std::time::{Duration as StdDuration, SystemTime};
5
6
use crate::date::{self, format_rfc3339, parse_rfc3339_weak};
7
use crate::duration::{self, format_duration, parse_duration};
8
9
/// A wrapper for duration that has `FromStr` implementation
10
///
11
/// This is useful if you want to use it somewhere where `FromStr` is
12
/// expected.
13
///
14
/// See `parse_duration` for the description of the format.
15
///
16
/// # Example
17
///
18
/// ```
19
/// use std::time::Duration;
20
/// let x: Duration;
21
/// x = "12h 5min 2ns".parse::<humantime::Duration>().unwrap().into();
22
/// assert_eq!(x, Duration::new(12*3600 + 5*60, 2))
23
/// ```
24
///
25
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
26
pub struct Duration(StdDuration);
27
28
/// A wrapper for SystemTime that has `FromStr` implementation
29
///
30
/// This is useful if you want to use it somewhere where `FromStr` is
31
/// expected.
32
///
33
/// See `parse_rfc3339_weak` for the description of the format. The "weak"
34
/// format is used as it's more pemissive for human input as this is the
35
/// expected use of the type (e.g. command-line parsing).
36
///
37
/// # Example
38
///
39
/// ```
40
/// use std::time::SystemTime;
41
/// let x: SystemTime;
42
/// x = "2018-02-16T00:31:37Z".parse::<humantime::Timestamp>().unwrap().into();
43
/// assert_eq!(humantime::format_rfc3339(x).to_string(), "2018-02-16T00:31:37Z");
44
/// ```
45
///
46
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
47
pub struct Timestamp(SystemTime);
48
49
impl AsRef<StdDuration> for Duration {
50
0
    fn as_ref(&self) -> &StdDuration {
51
0
        &self.0
52
0
    }
53
}
54
55
impl Deref for Duration {
56
    type Target = StdDuration;
57
0
    fn deref(&self) -> &StdDuration {
58
0
        &self.0
59
0
    }
60
}
61
62
impl From<Duration> for StdDuration {
63
0
    fn from(val: Duration) -> Self {
64
0
        val.0
65
0
    }
66
}
67
68
impl From<StdDuration> for Duration {
69
0
    fn from(dur: StdDuration) -> Duration {
70
0
        Duration(dur)
71
0
    }
72
}
73
74
impl FromStr for Duration {
75
    type Err = duration::Error;
76
0
    fn from_str(s: &str) -> Result<Duration, Self::Err> {
77
0
        parse_duration(s).map(Duration)
78
0
    }
79
}
80
81
impl fmt::Display for Duration {
82
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83
0
        format_duration(self.0).fmt(f)
84
0
    }
85
}
86
87
impl AsRef<SystemTime> for Timestamp {
88
0
    fn as_ref(&self) -> &SystemTime {
89
0
        &self.0
90
0
    }
91
}
92
93
impl Deref for Timestamp {
94
    type Target = SystemTime;
95
0
    fn deref(&self) -> &SystemTime {
96
0
        &self.0
97
0
    }
98
}
99
100
impl From<Timestamp> for SystemTime {
101
0
    fn from(val: Timestamp) -> Self {
102
0
        val.0
103
0
    }
104
}
105
106
impl From<SystemTime> for Timestamp {
107
0
    fn from(dur: SystemTime) -> Timestamp {
108
0
        Timestamp(dur)
109
0
    }
110
}
111
112
impl FromStr for Timestamp {
113
    type Err = date::Error;
114
0
    fn from_str(s: &str) -> Result<Timestamp, Self::Err> {
115
0
        parse_rfc3339_weak(s).map(Timestamp)
116
0
    }
117
}
118
119
impl fmt::Display for Timestamp {
120
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121
0
        format_rfc3339(self.0).fmt(f)
122
0
    }
123
}