pub struct Instant { /* private fields */ }Expand description
represents times after 1970. second precision
Implementations§
Source§impl Instant
impl Instant
pub fn now_utc() -> Self
Sourcepub fn now_local() -> Self
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
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}pub fn from_utc_secs(utc_secs: u64) -> Self
pub fn from_system_time(time: SystemTime) -> Self
Sourcepub fn format(&self, template: &str) -> String
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
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}Sourcepub fn new(
year: u64,
month: u64,
day: u64,
hour: u64,
minute: u64,
second: u64,
) -> Self
pub fn new( year: u64, month: u64, day: u64, hour: u64, minute: u64, second: u64, ) -> Self
month and day are one indexed
Sourcepub fn parse_english<S: AsRef<str>>(
words: impl Iterator<Item = S>,
) -> Result<Self, &'static str>
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}Sourcepub fn difference(&self, other: Instant) -> Result<Duration, Duration>
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}pub fn set_time(self, hours: u64, minutes: u64, seconds: u64) -> Self
pub fn year(&self) -> u64
pub fn day_of_week(&self) -> u8
pub fn day_of_week_str(&self) -> &str
Sourcepub fn week_of_year(&self) -> u64
pub fn week_of_year(&self) -> u64
1 indexed
pub fn month(&self) -> (&'static str, u64, u64)
Sourcepub fn utc_start_of_year(year: u64) -> Self
pub fn utc_start_of_year(year: u64) -> Self
TODO utc vs local etc
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 moreAuto Trait Implementations§
impl Freeze for Instant
impl RefUnwindSafe for Instant
impl Send for Instant
impl Sync for Instant
impl Unpin for Instant
impl UnsafeUnpin for Instant
impl UnwindSafe for Instant
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