pub struct Duration { /* private fields */ }Implementations§
Source§impl Duration
impl Duration
pub const fn from_secs(secs: u64) -> Self
Sourcepub const fn from_seconds(secs: u64) -> Self
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}Sourcepub const fn from_minutes(minutes: u64) -> Self
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}Sourcepub const fn from_hours(hours: u64) -> Self
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}Sourcepub const fn from_days(days: u64) -> Self
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
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}Sourcepub const fn from_weeks(weeks: u64) -> Self
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}Sourcepub fn format(&self) -> String
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 AddAssign<Duration> for Instant
impl AddAssign<Duration> for Instant
Source§fn add_assign(&mut self, duration: Duration)
fn add_assign(&mut self, duration: Duration)
Performs the
+= operation. Read moreimpl Copy for Duration
Auto Trait Implementations§
impl Freeze for Duration
impl RefUnwindSafe for Duration
impl Send for Duration
impl Sync for Duration
impl Unpin for Duration
impl UnsafeUnpin for Duration
impl UnwindSafe for Duration
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more