22
33import java .util .UUID ;
44
5+ import com .fasterxml .uuid .UUIDTimer ;
56import com .fasterxml .uuid .UUIDType ;
67
78public class UUIDUtil
@@ -360,7 +361,7 @@ private final static void _checkUUIDByteArray(byte[] bytes, int offset)
360361 *
361362 * @param uuid uuid timestamp to extract from
362363 *
363- * @return timestamp in milliseconds (since Epoch), or 0 if type does not support timestamps
364+ * @return Unix timestamp in milliseconds (since Epoch), or 0 if type does not support timestamps
364365 *
365366 * @since 5.0
366367 */
@@ -380,17 +381,25 @@ public static long extractTimestamp(UUID uuid)
380381 case NAME_BASED_MD5 :
381382 return 0L ;
382383 case TIME_BASED :
383- return _getTimestampFromUuidV1 ( uuid );
384+ return UUIDTimer . timestampToEpoch ( _getRawTimestampFromUuidV1 ( uuid ) );
384385 case TIME_BASED_REORDERED :
385- return _getTimestampFromUuidV6 ( uuid );
386+ return UUIDTimer . timestampToEpoch ( _getRawTimestampFromUuidV6 ( uuid ) );
386387 case TIME_BASED_EPOCH :
387- return _getTimestampFromUuidV7 (uuid );
388+ return _getRawTimestampFromUuidV7 (uuid );
388389 default :
389390 throw new IllegalArgumentException ("Invalid `UUID`: unexpected type " + type );
390391 }
391392 }
392393
393- private static long _getTimestampFromUuidV1 (UUID uuid ) {
394+ /**
395+ * Get raw timestamp, used to create the UUID v1
396+ *<p>
397+ * NOTE: no verification is done to ensure UUID given is of version 1.
398+ *
399+ * @param uuid uuid, to extract timestamp from
400+ * @return timestamp, used to create uuid v1
401+ */
402+ static long _getRawTimestampFromUuidV1 (UUID uuid ) {
394403 long mostSignificantBits = uuid .getMostSignificantBits ();
395404 mostSignificantBits = mostSignificantBits & 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1110_1111_1111_1111L;
396405 long low = mostSignificantBits >>> 32 ;
@@ -400,7 +409,15 @@ private static long _getTimestampFromUuidV1(UUID uuid) {
400409 return highOfHigher << 48 | lowOfHigher << 32 | low ;
401410 }
402411
403- private static long _getTimestampFromUuidV6 (UUID uuid ) {
412+ /**
413+ * Get raw timestamp, used to create the UUID v6.
414+ *<p>
415+ * NOTE: no verification is done to ensure UUID given is of version 6.
416+ *
417+ * @param uuid uuid, to extract timestamp from
418+ * @return timestamp, used to create uuid v6
419+ */
420+ static long _getRawTimestampFromUuidV6 (UUID uuid ) {
404421 long mostSignificantBits = uuid .getMostSignificantBits ();
405422 mostSignificantBits = mostSignificantBits & 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1001_1111_1111_1111L;
406423 long lowL = mostSignificantBits & 0xFFFL ;
@@ -410,7 +427,7 @@ private static long _getTimestampFromUuidV6(UUID uuid) {
410427 return high >>> 4 | lowH << 12 | lowL ;
411428 }
412429
413- private static long _getTimestampFromUuidV7 (UUID uuid ) {
430+ static long _getRawTimestampFromUuidV7 (UUID uuid ) {
414431 long mostSignificantBits = uuid .getMostSignificantBits ();
415432 mostSignificantBits = mostSignificantBits & 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1001_1111_1111_1111L;
416433 return mostSignificantBits >>> 16 ;
0 commit comments