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 theSystemTime
andUNIX_EPOCH
structs from thestd::time
module.let start = SystemTime::now();
: creates aSystemTime
object representing the current time.let ten_seconds_later = start.add_secs(10);
: adds 10 seconds to thestart
SystemTime
object and stores the result in theten_seconds_later
variable.
Helpful links
Related
- Using now to get current time in Rust
- How to get time from milliseconds in Rust
- How to get execution time in Rust
- How to get current date in Rust
- How to convert datetime to timestamp in Rust
- How to sleep in Rust
- How to add day to date in Rust
- What type to use for datetime in Rust
- How to format datetime in Rust
More of Rust
- How to use regex to match a double quote in Rust?
- How to replace a capture group using Rust regex?
- Bitwise XOR operator usage in Rust
- How to use modifiers in a Rust regex?
- How to map with index in Rust
- How to convert a u8 slice to a hex string in Rust?
- How to calculate the inverse of a matrix in Rust?
- How to use an enum in a Rust HashMap?
- How to match the end of a line in a Rust regex?
- How to create a Rust regex from a string?
See more codes...