rustHow to add second to time in Rust
Adding seconds to a time in Rust can be done using the add_secs method of the std::time::SystemTime struct. This method takes a u64 representing the number of seconds to add to the time.
Example code
use std::time::{SystemTime, UNIX_EPOCH};
let start = SystemTime::now();
let ten_seconds_later = start.add_secs(10);
Output example
ten_seconds_later = SystemTime { tv_sec: 1599450090, tv_nsec: 845005050 }
Code explanation
use std::time::{SystemTime, UNIX_EPOCH};: imports theSystemTimeandUNIX_EPOCHstructs from thestd::timemodule.let start = SystemTime::now();: creates aSystemTimeobject representing the current time.let ten_seconds_later = start.add_secs(10);: adds 10 seconds to thestartSystemTimeobject and stores the result in theten_seconds_latervariable.
Helpful links
Related
- How to get time from milliseconds in Rust
- How to sleep in Rust
- How to get execution time in Rust
- Using now to get current time in Rust
- How to get current date in Rust
- What type to use for datetime in Rust
- How to convert timestamp to datetime in Rust
- How to format time in Rust
- How to format datetime in Rust
More of Rust
- How to perform matrix operations in Rust?
- How to use regex lookbehind in Rust?
- Regex example to match multiline string in Rust?
- How to use regex to match a double quote in Rust?
- Hashshet example in Rust
- How to compare two Rust HashMaps?
- Yield example in Rust
- Generator example in Rust
- How to do a for loop with index in Rust
- How to match whitespace with a regex in Rust?
See more codes...