Skip to main content

Instant

Struct Instant 

Source
pub struct Instant { /* private fields */ }
Expand description

represents times after 1970. second precision

Implementations§

Source§

impl Instant

Source

pub fn now_utc() -> Self

Source

pub fn now_local() -> Self

Examples found in repository?
examples/when.rs (line 4)
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}
More examples
Hide additional examples
examples/now.rs (line 9)
4fn main() {
5    let mut args = std::env::args().skip(1);
6    // let format = arg.as_deref().unwrap_or_default();
7
8    let mut format = Cow::Borrowed(FORMATS::FULL_MINIMAL);
9    let mut now = Instant::now_local();
10
11    while let Some(arg) = args.next() {
12        match arg.as_str() {
13            "--help" => {
14                println!("now *format*");
15                println!("in date-utilities");
16                println!("by kaleidawave");
17                return;
18            }
19            "--format" => {
20                let next = args.next().unwrap();
21                format = match next.as_str() {
22                    "FULL_MINIMAL" => Cow::Borrowed(FORMATS::FULL_MINIMAL),
23                    "DATE_MONTH_YEAR" => Cow::Borrowed(FORMATS::DATE_MONTH_YEAR),
24                    "MONTH_DATE_YEAR" => Cow::Borrowed(FORMATS::MONTH_DATE_YEAR),
25                    "DATE_NAME_MONTH_YEAR" => Cow::Borrowed(FORMATS::DATE_NAME_MONTH_YEAR),
26                    "TIME_DATE_NAME_MONTH_YEAR" => {
27                        Cow::Borrowed(FORMATS::TIME_DATE_NAME_MONTH_YEAR)
28                    }
29                    "TIME" => Cow::Borrowed(FORMATS::TIME),
30                    "WEEK" => Cow::Borrowed(FORMATS::WEEK),
31                    "TIME_WITH_SECONDS" => Cow::Borrowed(FORMATS::TIME_WITH_SECONDS),
32                    _ => Cow::Owned(next),
33                };
34            }
35            "--add" => {
36                let next = args.next().unwrap();
37                let duration = duration_from_string(&next);
38                now += duration;
39            }
40            "--sub" => {
41                let next = args.next().unwrap();
42                let duration = duration_from_string(&next);
43                now -= duration;
44            }
45            arg => {
46                panic!("unknown {arg:?}");
47            }
48        }
49    }
50
51    let formatted = now.format(&format);
52
53    {
54        use std::io::{self, IsTerminal, Write};
55        let mut stdout = io::stdout().lock();
56        if stdout.is_terminal() {
57            writeln!(&mut stdout, "{formatted}").unwrap();
58        } else {
59            write!(&mut stdout, "{formatted}").unwrap();
60        }
61    }
62}
Source

pub fn from_utc_secs(utc_secs: u64) -> Self

Source

pub fn from_system_time(time: SystemTime) -> Self

Source

pub fn format(&self, template: &str) -> String

Examples found in repository?
examples/when.rs (line 40)
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}
More examples
Hide additional examples
examples/now.rs (line 51)
4fn main() {
5    let mut args = std::env::args().skip(1);
6    // let format = arg.as_deref().unwrap_or_default();
7
8    let mut format = Cow::Borrowed(FORMATS::FULL_MINIMAL);
9    let mut now = Instant::now_local();
10
11    while let Some(arg) = args.next() {
12        match arg.as_str() {
13            "--help" => {
14                println!("now *format*");
15                println!("in date-utilities");
16                println!("by kaleidawave");
17                return;
18            }
19            "--format" => {
20                let next = args.next().unwrap();
21                format = match next.as_str() {
22                    "FULL_MINIMAL" => Cow::Borrowed(FORMATS::FULL_MINIMAL),
23                    "DATE_MONTH_YEAR" => Cow::Borrowed(FORMATS::DATE_MONTH_YEAR),
24                    "MONTH_DATE_YEAR" => Cow::Borrowed(FORMATS::MONTH_DATE_YEAR),
25                    "DATE_NAME_MONTH_YEAR" => Cow::Borrowed(FORMATS::DATE_NAME_MONTH_YEAR),
26                    "TIME_DATE_NAME_MONTH_YEAR" => {
27                        Cow::Borrowed(FORMATS::TIME_DATE_NAME_MONTH_YEAR)
28                    }
29                    "TIME" => Cow::Borrowed(FORMATS::TIME),
30                    "WEEK" => Cow::Borrowed(FORMATS::WEEK),
31                    "TIME_WITH_SECONDS" => Cow::Borrowed(FORMATS::TIME_WITH_SECONDS),
32                    _ => Cow::Owned(next),
33                };
34            }
35            "--add" => {
36                let next = args.next().unwrap();
37                let duration = duration_from_string(&next);
38                now += duration;
39            }
40            "--sub" => {
41                let next = args.next().unwrap();
42                let duration = duration_from_string(&next);
43                now -= duration;
44            }
45            arg => {
46                panic!("unknown {arg:?}");
47            }
48        }
49    }
50
51    let formatted = now.format(&format);
52
53    {
54        use std::io::{self, IsTerminal, Write};
55        let mut stdout = io::stdout().lock();
56        if stdout.is_terminal() {
57            writeln!(&mut stdout, "{formatted}").unwrap();
58        } else {
59            write!(&mut stdout, "{formatted}").unwrap();
60        }
61    }
62}
Source

pub fn seconds(&self) -> u64

with utc_offset applied

Source

pub fn new( year: u64, month: u64, day: u64, hour: u64, minute: u64, second: u64, ) -> Self

month and day are one indexed

Source

pub fn parse_english<S: AsRef<str>>( words: impl Iterator<Item = S>, ) -> Result<Self, &'static str>

nth month year TODO errors

Examples found in repository?
examples/when.rs (line 43)
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 difference(&self, other: Instant) -> Result<Duration, Duration>

Returns the Duration between dates. If other

Examples found in repository?
examples/when.rs (line 48)
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 set_time(self, hours: u64, minutes: u64, seconds: u64) -> Self

Source

pub fn year(&self) -> u64

Source

pub fn day_of_week(&self) -> u8

Source

pub fn day_of_week_str(&self) -> &str

Source

pub fn week_of_year(&self) -> u64

1 indexed

Source

pub fn month(&self) -> (&'static str, u64, u64)

Source

pub fn utc_start_of_year(year: u64) -> Self

TODO utc vs local etc

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 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> 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, 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.