Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current time in milliseconds?

Tags:

rust

People also ask

How do you convert timestamps to milliseconds?

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 .

How do you write milliseconds in time?

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.

How do you convert UTC time to 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.

How do you get the current time in Millis Kotlin?

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.

Rust 1.33

let in_ms = since_the_epoch.as_millis();

Rust 1.27

let in_ms = since_the_epoch.as_secs() as u128 * 1000 + 
            since_the_epoch.subsec_millis() as u128;

Rust 1.8

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()