How to convert timeuuid to timestamp in ruby
I'm a Java developer and have no idea about Ruby, but I am trying to write a logstash plugin that can help me convert timeuuid to timestamp (MM / dd / yyyy-HH: mm: ss.SSS).
Below is its java version.
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);
}
}
+3
source to share
1 answer
With a quick look 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 can also use gem ( simple_uuid
).
require 'simple_uuid'
puts Time.at(SimpleUUID::UUID.new(uuid).seconds)
# 2012-11-14 21:16:22 +0100
+4
source to share