Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert timeuuid to timestamp in ruby

Tags:

ruby

logstash

I am a Java developer & have no idea on Ruby - but I am trying to write a logstash plugin that can help me to convert timeuuid to timestamp(MM/dd/yyyy-HH:mm:ss.SSS).

Below is the java version of it.

import java.util.Date;    
import java.util.UUID;   

public class TimeUUID {  

  static final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L; 

  public static long getTimeFromUUID(UUID uuid) {   

    return (uuid.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) / 10000; 

  }  

  public static void main(String[] args) {   

    String uuidString = "28442f00-2e98-11e2-0000-89a3a6fab5ef"; 

    UUID uuid = UUID.fromString(uuidString);  

    long time = getTimeFromUUID(uuid);  

    Date date = new Date(time); 

    System.out.println(date);  

  }  

}  
like image 899
Suraj Avatar asked Dec 02 '25 14:12

Suraj


1 Answers

From a quick glance at the answers in other languages, this might help you:

uuid = "28442f00-2e98-11e2-0000-89a3a6fab5ef"

NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000
low, mid, version_high = uuid.split('-')
high = version_high[1,3]
hex = [high, mid, low].join
puts Time.at((hex.to_i(16) - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) / 10000000)
# 2012-11-14 21:16:22 +0100

You could also use a gem (simple_uuid) for this.

require 'simple_uuid'
puts Time.at(SimpleUUID::UUID.new(uuid).seconds)
# 2012-11-14 21:16:22 +0100
like image 165
Eric Duminil Avatar answered Dec 04 '25 18:12

Eric Duminil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!