我如何将时间uuid(存储在boost uuid中)转换为自时期以来的时间戳/时间?

问题描述 投票:0回答:1

由于基于on the specs以及基于Cassandra's C++ driver source codeits struct definition,EPOCH似乎非常容易,因此从UUID时间戳转换为秒。

但是,当我尝试这样做时,我总是会得到错误的值。我做错了事,无法弄清楚是什么。

为此,我使用了herehere提供的样本UUID值。

所有要做的就是从UUID原始数据中获取第一个uint64_t,掩盖其前四个MSB,减去差值并除以一个数字。

这是我最小的完整示例:

#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;
}

此程序的输出是:

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

您可以使用此源代码here播放。

为什么我的结果甚至不接近当前时间戳?我在做什么错?

c++ boost cassandra uuid boost-date-time
1个回答
0
投票

注意,您要掩盖4 MSb而不是MSB:

uint64_t timestamp = contents.n1 & UINT64_C(0x0FFFFFFFFFFFFFFF);

这给出了更接近的结果,尽管我不确定它们是否正确:

uint64_t timestamp = contents.n1 & UINT64_C(0x00FFFFFFFFFFFFFF);
© www.soinside.com 2019 - 2024. All rights reserved.