How can I get the current time in milliseconds like I can in Java?
System.currentTimeMillis()
How can I get the current time in milliseconds like I can in Java?
System.currentTimeMillis()
Since Rust 1.8, you do not need to use a crate. Instead, you can use SystemTime
and UNIX_EPOCH
:
use std::time::{SystemTime, UNIX_EPOCH};
fn main() {
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
println!("{:?}", since_the_epoch);
}
If you need exactly milliseconds, you can convert the Duration
.
let in_ms = since_the_epoch.as_millis();
let in_ms = since_the_epoch.as_secs() as u128 * 1000 +
since_the_epoch.subsec_millis() as u128;
let in_ms = since_the_epoch.as_secs() * 1000 +
since_the_epoch.subsec_nanos() as u64 / 1_000_000;
If you just want to do simple timings with the milliseconds, you can use std::time::Instant
like this:
use std::time::Instant;
fn main() {
let start = Instant::now();
// do stuff
let elapsed = start.elapsed();
// Debug format
println!("Debug: {:?}", elapsed);
// Format as milliseconds rounded down
// Since Rust 1.33:
println!("Millis: {} ms", elapsed.as_millis());
// Before Rust 1.33:
println!("Millis: {} ms",
(elapsed.as_secs() * 1_000) + (elapsed.subsec_nanos() / 1_000_000) as u64);
}
Output:
Debug: 10.93993ms
Millis: 10 ms
Millis: 10 ms
You can use the time crate:
extern crate time;
fn main() {
println!("{}", time::now());
}
It returns a Tm
which you can get whatever precision you want.
I've found a clear solution with chrono in coinnect:
use chrono::prelude::*;
pub fn get_unix_timestamp_ms() -> i64 {
let now = Utc::now();
now.timestamp_millis()
}
pub fn get_unix_timestamp_us() -> i64 {
let now = Utc::now();
now.timestamp_nanos()
}
As @Shepmaster mentioned, this is the equivalent of Java's System.currentTimeMillis()
in Rust.
use std::time::{SystemTime, UNIX_EPOCH};
fn get_epoch_ms() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis()
}
extern crate time;
fn timestamp() -> f64 {
let timespec = time::get_time();
// 1459440009.113178
let mills: f64 = timespec.sec as f64 + (timespec.nsec as f64 / 1000.0 / 1000.0 / 1000.0);
mills
}
fn main() {
let ts = timestamp();
println!("Time Stamp: {:?}", ts);
}
System.currentTimeMillis()
in Java returns the difference in milliseconds between the current time and midnight, January 1, 1970.
In Rust we have time::get_time()
which returns a Timespec
with the current time as seconds and the offset in nanoseconds since midnight, January 1, 1970.
Example (using Rust 1.13):
extern crate time; //Time library
fn main() {
//Get current time
let current_time = time::get_time();
//Print results
println!("Time in seconds {}\nOffset in nanoseconds {}",
current_time.sec,
current_time.nsec);
//Calculate milliseconds
let milliseconds = (current_time.sec as i64 * 1000) +
(current_time.nsec as i64 / 1000 / 1000);
println!("System.currentTimeMillis(): {}", milliseconds);
}
Reference: Time crate, System.currentTimeMillis()
There seems to be a simple one-liner that yields a u128
, which is pretty close to what a java long
is.
let timestamp = std::time::UNIX_EPOCH.elapsed().unwrap().as_millis()
The docs do warn about clock drifting related to SystemTime objects, but I think this does not apply here, given we're checking against a point in time way back.