X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FTimeUuid.java;fp=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FTimeUuid.java;h=2e0587d12748128445ee3b81b64439fa8b6a59f2;hb=98f2c711418690e62e35c931e35d1dc92fd07926;hp=2276cd26831976344acb80e53b708aca902da854;hpb=2b036a42c6b5ace47fba14ca9d09ee6e37bf6c5f;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/TimeUuid.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/TimeUuid.java index 2276cd268..2e0587d12 100644 --- a/org.argeo.api.uuid/src/org/argeo/api/uuid/TimeUuid.java +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/TimeUuid.java @@ -76,4 +76,23 @@ public class TimeUuid extends TypedUuid { Duration duration = Duration.between(TimeUuid.TIMESTAMP_ZERO, instant); return durationToTimestamp(duration); } + + /** + * Crate a time UUID with this instant as timestamp and clock and node id set to + * zero. + */ + public static UUID fromInstant(Instant instant) { + long timestamp = instantToTimestamp(instant); + long mostSig = toMostSignificantBits(timestamp); + UUID uuid = new UUID(mostSig, UuidFactory.LEAST_SIG_RFC4122_VARIANT); + return uuid; + } + + /** Convert timestamp in UUID format to most significant bits of a time UUID. */ + static long toMostSignificantBits(long timestamp) { + long mostSig = UuidFactory.MOST_SIG_VERSION1 | ((timestamp & 0xFFFFFFFFL) << 32) // time_low + | (((timestamp >> 32) & 0xFFFFL) << 16) // time_mid + | ((timestamp >> 48) & 0x0FFFL);// time_hi_and_version + return mostSig; + } }