【问题标题】:How do I convert time-uuid (stored in boost uuid) to a timestamp/time since epoch?如何将 time-uuid(存储在 boost uuid 中)转换为自纪元以来的时间戳/时间?
【发布时间】:2020-02-18 09:49:00
【问题描述】:

基于on the specs,基于Cassandra's C++ driver source code,基于its 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】:

    我认为通过将 UUID 处理为字符串并使用字符串操作来提取时间戳信息,然后将其转换为数值会更容易理解。诀窍是时间戳信息存储在 UUID 中的方式。来自规范:

    UUID 字符串表示的正式定义是 由以下 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"
    

    以下是 UUID 的字符串表示示例 作为 URN:

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

    即UUID 的第一部分(在“-”之前)是时间低,第二部分是时间中间,第三部分是时间高版本,第一个字符是 UUID 版本。所以我们需要拆分 UUID 并重新组合这些时间戳部分以创建完整的时间戳字符串,如下所示:{time-high minus-version}{time-mid}{time-low}

    这是修改后的代码。我把这个漂亮的 javascript 示例作为参考: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;
    }
    

    输出

    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
    

    【讨论】:

      【解决方案2】:

      恕我直言,您一直都做得不好。阅读您提供的文档,我尝试从 UUID 重新生成时间戳。这是我的代码:

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

      【讨论】:

      • 感谢您指出 MSb,您是对的。我纠正了这一点。但这种掩饰并不真正正确。你屏蔽了 8 位。虽然更接近了,但仍然下降了大约 10%(自 EPOCH 以来已经过去了几年)。
      • @TheQuantumPhysicist 对我的回答做了重大改变,我建议你看看;)
      猜你喜欢
      • 1970-01-01
      • 2017-04-28
      • 2013-08-19
      • 1970-01-01
      • 2022-01-13
      • 2020-06-24
      • 2021-05-10
      • 2021-07-09
      • 1970-01-01
      相关资源
      最近更新 更多