Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a DateTime from epoch milliseconds #264

Closed
nevi-me opened this issue Jul 14, 2018 · 4 comments
Closed

Creating a DateTime from epoch milliseconds #264

nevi-me opened this issue Jul 14, 2018 · 4 comments

Comments

@nevi-me
Copy link

nevi-me commented Jul 14, 2018

I've been able to convert a DateTime to milliseconds with timestamp_millis(), but I am struggling to find a trait member that allows me to get a DateTime with milliseconds.

Is it currently possible to do this?

@govo
Copy link

govo commented Oct 28, 2021

So, we need to convert millis to nanosec first?

@evanxg852000
Copy link

Possible way

use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};

fn main() {
    let time  = chrono::Utc::now();
    let ts_millis = time.timestamp_millis();
    println!("timestamp milli {} -> {}", time, ts_millis);

    let ts_secs = ts_millis / 1000;
    let ts_ns = (ts_millis % 1000) * 1_000_000 ;
    let dt = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(ts_secs, ts_ns as u32), Utc);

    println!("timestamp milli {} -> {}", dt, dt.timestamp_millis());

}

@xmakro
Copy link
Contributor

xmakro commented Jan 4, 2024

Could we add a DateTime::from_timestamp_millis(millis: i64) for this?

@djc
Copy link
Member

djc commented Jan 9, 2024

I'm open to reviewing a PR for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants