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

How to get a Dart DateTime from a uuid.v1() ? #70

Closed
jdavies opened this issue Jun 5, 2021 · 1 comment
Closed

How to get a Dart DateTime from a uuid.v1() ? #70

jdavies opened this issue Jun 5, 2021 · 1 comment

Comments

@jdavies
Copy link

jdavies commented Jun 5, 2021

Given the nature of the v1 uuid, I'd like to be able to easily convert the uuid into a Dart DateTime object. How would I do that?
Thanks in advance!

@daegalus
Copy link
Owner

daegalus commented Jun 6, 2021

It won't be super accurate but the first 16 hex digits are the time.

First 8 hex digits are time_low
The following 4 digits are time_mid
Then the following 4 are time high and the version. So it's always 1xxx for UUIDv1. I think in most cases you can drop that 1 and get the time. But if that doesn't work, keep the one. I forget if we lose some time data or not.

To reconstruct time you need to rearrange them to be time_high, time_mid, time_low and then decode it to a byte array, then convert the byte array to a long/int.

That will be the timestamp, which you can use in datetime.

The experimental/draft UUIDv6 in the future will not mess with the time stamp so it can be easier to decode the time from that.

I hope that helped.

You can look at the code for V1 and do it in reverse for the time stuff. You will need to get comfortable with bitshifts and integer manipulation

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

2 participants