Skip to main content

Duration

Struct Duration 

Source
pub struct Duration { /* private fields */ }

Implementations§

Source§

impl Duration

Source

pub const fn from_secs(secs: u64) -> Self

Source

pub const fn from_seconds(secs: u64) -> Self

Examples found in repository?
examples/when.rs (line 14)
3fn main() {
4    let now = Instant::now_local();
5
6    let mut args = std::env::args().skip(1);
7
8    let next = args.next().unwrap();
9
10    if let Ok(count) = str::parse::<u64>(&next) {
11        let kind: String = args.next().expect("kind");
12
13        let duration: Duration = match kind.as_str() {
14            "seconds" => Duration::from_seconds(count),
15            "minutes" => Duration::from_minutes(count),
16            "hours" => Duration::from_hours(count),
17            "days" => Duration::from_days(count),
18            "weeks" => Duration::from_weeks(count),
19            // TODO
20            "months" => Duration::from_days(count * 30),
21            // TODO
22            "years" => Duration::from_days(count * 356),
23            kind => {
24                panic!("unknown {kind:?}");
25            }
26        };
27
28        let then: Instant = now - duration;
29
30        let after = args.next();
31        let after: &str = after.as_deref().unwrap_or_default();
32        let format = match after {
33            "git" => "%year-%month-%date %hour:%minute:%second",
34            "" => FORMATS::FULL_MINIMAL,
35            unknown => {
36                panic!("unknown {unknown:?}");
37            }
38        };
39
40        let formatted = then.format(format);
41        println!("{formatted}");
42    } else if let "is" | "was" = next.as_str() {
43        let then = Instant::parse_english(&mut args).unwrap();
44        if let Some(next) = args.next()
45            && next == "to"
46        {
47            let next = Instant::parse_english(&mut args).unwrap();
48            let value = match next.difference(then) {
49                Ok(future) => future,
50                Err(past) => past,
51            };
52            println!("{value}", value = value.format());
53        } else {
54            match then.difference(now) {
55                Ok(future) => {
56                    println!("in {future} time", future = future.format());
57                }
58                Err(past) => {
59                    println!("{past} ago", past = past.format());
60                }
61            }
62        }
63    } else {
64        panic!("unknown {next:?}. expected data or is / when");
65    }
66}
Source

pub const fn from_minutes(minutes: u64) -> Self

Examples found in repository?
examples/when.rs (line 15)
3fn main() {
4    let now = Instant::now_local();
5
6    let mut args = std::env::args().skip(1);
7
8    let next = args.next().unwrap();
9
10    if let Ok(count) = str::parse::<u64>(&next) {
11        let kind: String = args.next().expect("kind");
12
13        let duration: Duration = match kind.as_str() {
14            "seconds" => Duration::from_seconds(count),
15            "minutes" => Duration::from_minutes(count),
16            "hours" => Duration::from_hours(count),
17            "days" => Duration::from_days(count),
18            "weeks" => Duration::from_weeks(count),
19            // TODO
20            "months" => Duration::from_days(count * 30),
21            // TODO
22            "years" => Duration::from_days(count * 356),
23            kind => {
24                panic!("unknown {kind:?}");
25            }
26        };
27
28        let then: Instant = now - duration;
29
30        let after = args.next();
31        let after: &str = after.as_deref().unwrap_or_default();
32        let format = match after {
33            "git" => "%year-%month-%date %hour:%minute:%second",
34            "" => FORMATS::FULL_MINIMAL,
35            unknown => {
36                panic!("unknown {unknown:?}");
37            }
38        };
39
40        let formatted = then.format(format);
41        println!("{formatted}");
42    } else if let "is" | "was" = next.as_str() {
43        let then = Instant::parse_english(&mut args).unwrap();
44        if let Some(next) = args.next()
45            && next == "to"
46        {
47            let next = Instant::parse_english(&mut args).unwrap();
48            let value = match next.difference(then) {
49                Ok(future) => future,
50                Err(past) => past,
51            };
52            println!("{value}", value = value.format());
53        } else {
54            match then.difference(now) {
55                Ok(future) => {
56                    println!("in {future} time", future = future.format());
57                }
58                Err(past) => {
59                    println!("{past} ago", past = past.format());
60                }
61            }
62        }
63    } else {
64        panic!("unknown {next:?}. expected data or is / when");
65    }
66}
Source

pub const fn from_hours(hours: u64) -> Self

Examples found in repository?
examples/when.rs (line 16)
3fn main() {
4    let now = Instant::now_local();
5
6    let mut args = std::env::args().skip(1);
7
8    let next = args.next().unwrap();
9
10    if let Ok(count) = str::parse::<u64>(&next) {
11        let kind: String = args.next().expect("kind");
12
13        let duration: Duration = match kind.as_str() {
14            "seconds" => Duration::from_seconds(count),
15            "minutes" => Duration::from_minutes(count),
16            "hours" => Duration::from_hours(count),
17            "days" => Duration::from_days(count),
18            "weeks" => Duration::from_weeks(count),
19            // TODO
20            "months" => Duration::from_days(count * 30),
21            // TODO
22            "years" => Duration::from_days(count * 356),
23            kind => {
24                panic!("unknown {kind:?}");
25            }
26        };
27
28        let then: Instant = now - duration;
29
30        let after = args.next();
31        let after: &str = after.as_deref().unwrap_or_default();
32        let format = match after {
33            "git" => "%year-%month-%date %hour:%minute:%second",
34            "" => FORMATS::FULL_MINIMAL,
35            unknown => {
36                panic!("unknown {unknown:?}");
37            }
38        };
39
40        let formatted = then.format(format);
41        println!("{formatted}");
42    } else if let "is" | "was" = next.as_str() {
43        let then = Instant::parse_english(&mut args).unwrap();
44        if let Some(next) = args.next()
45            && next == "to"
46        {
47            let next = Instant::parse_english(&mut args).unwrap();
48            let value = match next.difference(then) {
49                Ok(future) => future,
50                Err(past) => past,
51            };
52            println!("{value}", value = value.format());
53        } else {
54            match then.difference(now) {
55                Ok(future) => {
56                    println!("in {future} time", future = future.format());
57                }
58                Err(past) => {
59                    println!("{past} ago", past = past.format());
60                }
61            }
62        }
63    } else {
64        panic!("unknown {next:?}. expected data or is / when");
65    }
66}
Source

pub const fn from_days(days: u64) -> Self

Examples found in repository?
examples/now.rs (line 73)
64fn duration_from_string(on: &str) -> Duration {
65    fn non_alphabetic(c: char) -> bool {
66        !c.is_alphabetic()
67    }
68
69    let (left, right) = on.rsplit_once(non_alphabetic).unwrap();
70
71    let left: u64 = str::parse(left).unwrap();
72    match right {
73        "day" | "days" => Duration::from_days(left),
74        "week" | "weeks" => Duration::from_days(left * 7),
75        time => panic!("unknown duration {time}"),
76    }
77}
More examples
Hide additional examples
examples/when.rs (line 17)
3fn main() {
4    let now = Instant::now_local();
5
6    let mut args = std::env::args().skip(1);
7
8    let next = args.next().unwrap();
9
10    if let Ok(count) = str::parse::<u64>(&next) {
11        let kind: String = args.next().expect("kind");
12
13        let duration: Duration = match kind.as_str() {
14            "seconds" => Duration::from_seconds(count),
15            "minutes" => Duration::from_minutes(count),
16            "hours" => Duration::from_hours(count),
17            "days" => Duration::from_days(count),
18            "weeks" => Duration::from_weeks(count),
19            // TODO
20            "months" => Duration::from_days(count * 30),
21            // TODO
22            "years" => Duration::from_days(count * 356),
23            kind => {
24                panic!("unknown {kind:?}");
25            }
26        };
27
28        let then: Instant = now - duration;
29
30        let after = args.next();
31        let after: &str = after.as_deref().unwrap_or_default();
32        let format = match after {
33            "git" => "%year-%month-%date %hour:%minute:%second",
34            "" => FORMATS::FULL_MINIMAL,
35            unknown => {
36                panic!("unknown {unknown:?}");
37            }
38        };
39
40        let formatted = then.format(format);
41        println!("{formatted}");
42    } else if let "is" | "was" = next.as_str() {
43        let then = Instant::parse_english(&mut args).unwrap();
44        if let Some(next) = args.next()
45            && next == "to"
46        {
47            let next = Instant::parse_english(&mut args).unwrap();
48            let value = match next.difference(then) {
49                Ok(future) => future,
50                Err(past) => past,
51            };
52            println!("{value}", value = value.format());
53        } else {
54            match then.difference(now) {
55                Ok(future) => {
56                    println!("in {future} time", future = future.format());
57                }
58                Err(past) => {
59                    println!("{past} ago", past = past.format());
60                }
61            }
62        }
63    } else {
64        panic!("unknown {next:?}. expected data or is / when");
65    }
66}
Source

pub const fn from_weeks(weeks: u64) -> Self

Examples found in repository?
examples/when.rs (line 18)
3fn main() {
4    let now = Instant::now_local();
5
6    let mut args = std::env::args().skip(1);
7
8    let next = args.next().unwrap();
9
10    if let Ok(count) = str::parse::<u64>(&next) {
11        let kind: String = args.next().expect("kind");
12
13        let duration: Duration = match kind.as_str() {
14            "seconds" => Duration::from_seconds(count),
15            "minutes" => Duration::from_minutes(count),
16            "hours" => Duration::from_hours(count),
17            "days" => Duration::from_days(count),
18            "weeks" => Duration::from_weeks(count),
19            // TODO
20            "months" => Duration::from_days(count * 30),
21            // TODO
22            "years" => Duration::from_days(count * 356),
23            kind => {
24                panic!("unknown {kind:?}");
25            }
26        };
27
28        let then: Instant = now - duration;
29
30        let after = args.next();
31        let after: &str = after.as_deref().unwrap_or_default();
32        let format = match after {
33            "git" => "%year-%month-%date %hour:%minute:%second",
34            "" => FORMATS::FULL_MINIMAL,
35            unknown => {
36                panic!("unknown {unknown:?}");
37            }
38        };
39
40        let formatted = then.format(format);
41        println!("{formatted}");
42    } else if let "is" | "was" = next.as_str() {
43        let then = Instant::parse_english(&mut args).unwrap();
44        if let Some(next) = args.next()
45            && next == "to"
46        {
47            let next = Instant::parse_english(&mut args).unwrap();
48            let value = match next.difference(then) {
49                Ok(future) => future,
50                Err(past) => past,
51            };
52            println!("{value}", value = value.format());
53        } else {
54            match then.difference(now) {
55                Ok(future) => {
56                    println!("in {future} time", future = future.format());
57                }
58                Err(past) => {
59                    println!("{past} ago", past = past.format());
60                }
61            }
62        }
63    } else {
64        panic!("unknown {next:?}. expected data or is / when");
65    }
66}
Source

pub fn format(&self) -> String

Examples found in repository?
examples/when.rs (line 52)
3fn main() {
4    let now = Instant::now_local();
5
6    let mut args = std::env::args().skip(1);
7
8    let next = args.next().unwrap();
9
10    if let Ok(count) = str::parse::<u64>(&next) {
11        let kind: String = args.next().expect("kind");
12
13        let duration: Duration = match kind.as_str() {
14            "seconds" => Duration::from_seconds(count),
15            "minutes" => Duration::from_minutes(count),
16            "hours" => Duration::from_hours(count),
17            "days" => Duration::from_days(count),
18            "weeks" => Duration::from_weeks(count),
19            // TODO
20            "months" => Duration::from_days(count * 30),
21            // TODO
22            "years" => Duration::from_days(count * 356),
23            kind => {
24                panic!("unknown {kind:?}");
25            }
26        };
27
28        let then: Instant = now - duration;
29
30        let after = args.next();
31        let after: &str = after.as_deref().unwrap_or_default();
32        let format = match after {
33            "git" => "%year-%month-%date %hour:%minute:%second",
34            "" => FORMATS::FULL_MINIMAL,
35            unknown => {
36                panic!("unknown {unknown:?}");
37            }
38        };
39
40        let formatted = then.format(format);
41        println!("{formatted}");
42    } else if let "is" | "was" = next.as_str() {
43        let then = Instant::parse_english(&mut args).unwrap();
44        if let Some(next) = args.next()
45            && next == "to"
46        {
47            let next = Instant::parse_english(&mut args).unwrap();
48            let value = match next.difference(then) {
49                Ok(future) => future,
50                Err(past) => past,
51            };
52            println!("{value}", value = value.format());
53        } else {
54            match then.difference(now) {
55                Ok(future) => {
56                    println!("in {future} time", future = future.format());
57                }
58                Err(past) => {
59                    println!("{past} ago", past = past.format());
60                }
61            }
62        }
63    } else {
64        panic!("unknown {next:?}. expected data or is / when");
65    }
66}

Trait Implementations§

Source§

impl Add<Duration> for Instant

Source§

type Output = Instant

The resulting type after applying the + operator.
Source§

fn add(self, duration: Duration) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<Duration> for Instant

Source§

fn add_assign(&mut self, duration: Duration)

Performs the += operation. Read more
Source§

impl Clone for Duration

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Duration

Source§

impl Sub<Duration> for Instant

Source§

type Output = Instant

The resulting type after applying the - operator.
Source§

fn sub(self, duration: Duration) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign<Duration> for Instant

Source§

fn sub_assign(&mut self, duration: Duration)

Performs the -= operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.