15

I use Cassandra DB and Helenus module for nodejs to operate with this. I have some rows which contains TimeUUID columns. How to get timestamp from TimeUUID in javascript?

Vadim
  • 538
  • 2
  • 5
  • 23
  • 1
    I guess you can get the timestamp directly from [row.forEach](https://github.com/simplereach/helenus#rowforeach). – adrianp Jul 10 '13 at 13:37
  • Unfortunately I can't in this case. I have structure in CF like this: timestamp_[user_key] : {timeUUID : 'JSON_DATA', timeUUID : 'JSON_DATA'...}. So ts var in row.forEach(function(name,value,ts,ttl) contains timestamp of creation date of timestamp_[user_key] row. – Vadim Jul 10 '13 at 13:48

4 Answers4

25

this lib ( UUID_to_Date ) is very simple and fast!! only used native String function. maybe this Javascript API can help you to convert the UUID to date format, Javascript is simple language and this simple code can help to writing API for every language.

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



all of 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'
Sam
  • 269
  • 3
  • 3
  • Thanks. For this specific uuid, I get `"13.11.2014, 18:36:06"` though. – Eric Duminil May 03 '17 at 08:20
  • note, that uuid must be version 1; test with function get_uuid_version(uuid_str) { var uuid_arr = uuid_str.split( '-' ) var timeHiAndVersion = parseInt(uuid_arr[ 2 ], 16); var version = (timeHiAndVersion >> 12) & 0xF; return version; } – Steve Oh Nov 10 '17 at 06:49
9

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

The time is encoded into the top 64 bits of the UUID, but it's interleaved with some other pieces, so it's not super straight forward to extract a time.

If n is the integer representation of the TimeUUID then you can extract the UNIX epoch like this:

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 in full here: 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 the RFC to implement that code.

Theo
  • 131,503
  • 21
  • 160
  • 205
  • 2
    So, for convert TimeUUID (for example aa009299-0c22-47c0-9cd4-aa518f0274a7) to timestamp using JS I need to convert uuid to integer representation first... Right? How to do that? – Vadim Jul 11 '13 at 07:46
7

Use uuid-time module.

I asked maintainers of uuid module here https://github.com/kelektiv/node-uuid/issues/297 and they pointed me to the uuid-time module https://www.npmjs.com/package/uuid-time

Aalex Gabi
  • 1,525
  • 1
  • 18
  • 32
  • This is what should be the correct answer now. Thank you for actually releasing a node module that does it with no additional dependency. – zenbeni Aug 09 '21 at 13:54
4

node-uuid module for nodejs contains method for convert uuid v1 to timestamp

Commit with function for extract msecs from uuid v1

Vadim
  • 538
  • 2
  • 5
  • 23