The Quantum Physicist
The Quantum Physicist

Reputation: 26356

How do I convert time-uuid (stored in boost uuid) to a timestamp/time since epoch?

Converting from UUID timestamp to seconds since EPOCH seems quite easy based on the specs, also based on Cassandra's C++ driver source code based on its struct definition.

However, when I try to do it, I always get the wrong value. I'm doing something wrong and I'm unable to figure out what it's.

For that, I used sample UUID values provided from here and here.

All one has to do is take the first uint64_t from the UUID raw data, mask its first four MSb, subtract a difference and divide by a number.

Here's my minimum complete example:

#include <boost/date_time.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <cstdint>
#include <iostream>

uint64_t TimestampFromUUID(const boost::uuids::uuid& uuid) {
  static constexpr const int UUID_SIZE = 16;
  static_assert(sizeof(uuid) == UUID_SIZE, "Invalid size of uuid");

  static constexpr const int MS_FROM_100NS_FACTOR = 10000;
  static constexpr const uint64_t OFFSET_FROM_15_10_1582_TO_EPOCH = 122192928000000000;

  struct two64s {
    uint64_t n1;
    uint64_t n2;
  } contents;
  std::memcpy(&contents, uuid.data, UUID_SIZE);
  //    contents.n1 = __builtin_bswap64(contents.n1);
  uint64_t timestamp = contents.n1 & UINT64_C(0x0FFFFFFFFFFFFFFF);
  return (timestamp - OFFSET_FROM_15_10_1582_TO_EPOCH) / MS_FROM_100NS_FACTOR;
}

int main() {
  std::cout << "Time now: " << (boost::posix_time::second_clock::universal_time() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_milliseconds() << std::endl;
  auto gen = boost::uuids::string_generator();
  std::cout << "UUID: " << gen("49cbda60-961b-11e8-9854-134d5b3f9cf8") << std::endl;
  std::cout << "Time from UUID: " << TimestampFromUUID(gen("49cbda60-961b-11e8-9854-134d5b3f9cf8")) << std::endl;
  std::cout << "UUID: " << gen("58e0a7d7-eebc-11d8-9669-0800200c9a66") << std::endl;
  std::cout << "Time from UUID: " << TimestampFromUUID(gen("58e0a7d7-eebc-11d8-9669-0800200c9a66")) << std::endl;

  return 0;
}

The output of this program is:

Time now: 1571735685000
UUID: 49cbda60-961b-11e8-9854-134d5b3f9cf8
Time from UUID: 45908323159150
UUID: 58e0a7d7-eebc-11d8-9669-0800200c9a66
Time from UUID: 45926063291384

You can play with this source code here.

Why are my results not even close to current timestamp? What am I doing wrong?

Upvotes: 1

Views: 1807

Answers (2)

Yasir Khan
Yasir Khan

Reputation: 693

I think it would be easier to understand by processing the UUID as a string and using string manipulation to extract the timestamp information, which is then converted to a numerical value. The trick is the way timestamp information is stored in the UUID. From the specification:

The formal definition of the UUID string representation is provided by the following ABNF [7]:

  UUID                   = time-low "-" time-mid "-"
                           time-high-and-version "-"
                           clock-seq-and-reserved
                           clock-seq-low "-" node
  time-low               = 4hexOctet
  time-mid               = 2hexOctet
  time-high-and-version  = 2hexOctet
  clock-seq-and-reserved = hexOctet
  clock-seq-low          = hexOctet
  node                   = 6hexOctet
  hexOctet               = hexDigit hexDigit
  hexDigit =
        "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
        "a" / "b" / "c" / "d" / "e" / "f" /
        "A" / "B" / "C" / "D" / "E" / "F"

The following is an example of the string representation of a UUID as a URN:

urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6

i.e. the first part of the UUID (before '-') is the time-low, the second the time-mid and the third is the time-high-version, with the first character being the UUID version. So we need to split the UUID and recombine these timestamp parts to create the full timestamp string like this: {time-high minus-version}{time-mid}{time-low}

Here's the modified piece of code. I have taken this nice javascript example as a reference: https://stackoverflow.com/a/26915856/3694234

#include <boost/date_time.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <cstdint>
#include <iostream>

uint64_t TimestampFromUUID(const boost::uuids::uuid& uuid) {
  static constexpr const int UUID_SIZE = 16;
  static_assert(sizeof(uuid) == UUID_SIZE, "Invalid size of uuid");

  static constexpr const int MS_FROM_100NS_FACTOR = 10000;
  static constexpr const uint64_t OFFSET_FROM_15_10_1582_TO_EPOCH = 122192928000000000;

  /* convert uuid to string for manipulation */
  std::string uuid_str = boost::uuids::to_string(uuid);
  /* store uuid parts in a vector */
  std::vector<std::string> uuid_parts;

  /* split uuid with '-' as delimiter */
  boost::split(uuid_parts, uuid_str, [](char c){return c == '-';});

  /* first part of uuid is time-low
     second part is time-mid
     third part is time high with most significant 4 bits as uuid version
  */
  std::string uuid_timestamp = uuid_parts[2].substr(1) + uuid_parts[1] + uuid_parts[0];
  std::cout << std::endl << "UUID Timestamp : " << uuid_timestamp << std::endl;

  uint64_t timestamp = std::stoul(uuid_timestamp, nullptr, 16);

  return (timestamp - OFFSET_FROM_15_10_1582_TO_EPOCH) / MS_FROM_100NS_FACTOR;
}

int main() {
  std::cout << "Time now: " << (boost::posix_time::second_clock::universal_time() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_milliseconds() << std::endl;
  auto gen = boost::uuids::string_generator();
  std::cout << "UUID: " << gen("49cbda60-961b-11e8-9854-134d5b3f9cf8") << std::endl;
  std::cout << "Time from UUID: " << TimestampFromUUID(gen("49cbda60-961b-11e8-9854-134d5b3f9cf8")) << std::endl;
  std::cout << "UUID: " << gen("58e0a7d7-eebc-11d8-9669-0800200c9a66") << std::endl;
  std::cout << "Time from UUID: " << TimestampFromUUID(gen("58e0a7d7-eebc-11d8-9669-0800200c9a66")) << std::endl;

  return 0;
}

Output

Time now: 1571838175000
UUID: 49cbda60-961b-11e8-9854-134d5b3f9cf8
Time from UUID: 
UUID Timestamp : 1e8961b49cbda60
1533190458118
UUID: 58e0a7d7-eebc-11d8-9669-0800200c9a66
Time from UUID: 
UUID Timestamp : 1d8eebc58e0a7d7
1092575371981

Upvotes: 1

bartop
bartop

Reputation: 10315

IMHO you were not doing it properly all along. Reading the documentation you provided I tried to regenerate the timestamp from UUID. Here's my code to do it:

uint64_t TimestampFromUUID(const boost::uuids::uuid& uuid) {
  static constexpr const int UUID_SIZE = 16;
  static_assert(sizeof(uuid) == UUID_SIZE, "Invalid size of uuid");

  static constexpr const int MS_FROM_100NS_FACTOR = 10000;
  static constexpr const uint64_t OFFSET_FROM_15_10_1582_TO_EPOCH = 122192928000000000;

  uint64_t timestamp = uuid.data[3] + (uuid.data[2] << 8) + (uuid.data[1] << 16) + (uuid.data[0] << 24);
  timestamp += ((uint64_t)uuid.data[4] << 40) + ((uint64_t)uuid.data[5] << 32);
  timestamp += ((uint64_t)uuid.data[7] << 48) + ((uint64_t)(uuid.data[6] & 0x0F) << 56);
  return (timestamp - OFFSET_FROM_15_10_1582_TO_EPOCH) / MS_FROM_100NS_FACTOR;
}

Upvotes: 0

Related Questions