bahlo / sonyflake-rs

🃏 A distributed unique ID generator inspired by Twitter's Snowflake.
https://docs.rs/sonyflake/
Apache License 2.0
174 stars 10 forks source link

how do i conver this into a date time? #18

Closed patchthecode closed 1 year ago

patchthecode commented 1 year ago

After converting the generated id into ints components using the decompose function, i got this

{"machine-id": 376, "id": 435182517281096056, "time": 25938899355, "sequence": 0, "msb": 0}

How do i convert 25938899355 in to a date? is this possible?

negezor commented 1 year ago

In the future, if anyone needs:

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

let your_epoch = Utc.with_ymd_and_hms(2014, 9, 1, 0, 0, 0);

let parts = decompose(id);

Utc.timestamp_nanos(your_epoch.timestamp_nanos_opt().unwrap() + parts.nanos_time())
bahlo commented 1 year ago

Released with 0.2, thanks @negezor for putting in the work!