How to extract timestamp from UUID v1 (TimeUUID) using javascript?

I am using Cassandra DB and the Helenus module for nodejs to work with this. I have some rows that contain TimeUUID columns. How to get timestamp from TimeUUID in javascript?

+4
source share
3 answers

node -uuid module for nodejs contains a method for converting uuid v1 to timestamp

Commit function to extract msecs from uuid v1

0
source

You can use the unixTimestampOf or dateOf in CQL3, or you can do it yourself, the hard way:

Time is encoded in the upper 64 bits of the UUID, but it alternates with some other parts, so it is not super straight forward to extract the time.

If n is an integer representation of TimeUUID , you can extract the UNIX era as follows:

 n = (value >> 64) t = 0 t |= (n & 0x0000000000000fff) << 48 t |= (n & 0x00000000ffff0000) << 16 t |= (n & 0xffffffff00000000) >> 32 t -= 122192928000000000 seconds = t/10_000_000 microseconds = (t - seconds * 10_000_000)/10.0 

this code is from my Ruby CQL3 driver, cql-rb, and can be found here completely: https://github.com/iconara/cql-rb/blob/master/lib/cql/time_uuid.rb

I used this resource: http://www.famkruithof.net/guid-uuid-timebased.html and RFC to implement this code.

+8
source

this lib ( UUID_to_Date ) is very simple and fast! only the built-in String function is used. perhaps this Javascript API can help you convert the UUID to a date format, Javascript is a simple language, and this simple code can help you write an API for each language.

this API converts UUID v1 to sec from 1970-01-01



all you need:

  get_time_int = function (uuid_str) { var uuid_arr = uuid_str.split( '-' ), time_str = [ uuid_arr[ 2 ].substring( 1 ), uuid_arr[ 1 ], uuid_arr[ 0 ] ].join( '' ); return parseInt( time_str, 16 ); }; get_date_obj = function (uuid_str) { var int_time = this.get_time_int( uuid_str ) - 122192928000000000, int_millisec = Math.floor( int_time / 10000 ); return new Date( int_millisec ); }; 


Example:

  var date_obj = get_date_obj( '8bf1aeb8-6b5b-11e4-95c0-001dba68c1f2' ); date_obj.toLocaleString( );// '11/13/2014, 9:06:06 PM' 
+7
source

Source: https://habr.com/ru/post/1490708/


All Articles