A simple solution is to get the timedelta object by finding the difference of the given datetime with Epoch time, i.e., midnight 1 January 1970. To obtain time in milliseconds, you can use the timedelta. total_seconds() * 1000 .
Usually we display time in in 12 hour format hh:mm:aa format (e.g. 12:30 PM) or 24 hour format HH:mm (e.g. 13:30), however sometimes we also want to show the milliseconds in the time. To show the milliseconds in the time we include “SSS” in the pattern which displays the Milliseconds.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); format. setTimeZone(TimeZone. getTimeZone("UTC")); Date date = format. parse(text); long millis = date.
currentTimeMillis(); returns the number of milliseconds from 1970-01-01T00:00:00Z, but new Date() gives the current local time.
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()
}
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);
}
Rust Playground
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With