CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
uuid.cc
Go to the documentation of this file.
1// -*- C++ -*-
12#include <random>
13#include <ctime>
14#include <chrono>
15#include "base/uuid.h"
16
17#include <iostream>
18
19namespace CNORXZ
20{
21
22 constexpr std::tm mkTRef()
23 {
24 std::tm o = {};
25 o.tm_sec = 0;
26 o.tm_min = 0;
27 o.tm_hour = 0;
28 o.tm_mday = 1;
29 o.tm_mon = 0;
30 o.tm_year = 70;
31 o.tm_wday = 4;
32 o.tm_yday = 0;
33 o.tm_isdst = 0;
34 return o;
35 }
36
37 // the node bits are created randomly (no search for mac or similar)
39 {
40 static constexpr uint64_t diff_1970_1582_ns =
41 141427ull * 86400ull * 1000ull * 1000ull * 1000ull;
42 static std::tm tref_1970 = mkTRef();
43 static uint16_t version = 1;
44 std::tm tref_1970_tmp = tref_1970;
45 const auto ref = std::chrono::system_clock::from_time_t(std::mktime(&tref_1970_tmp));
46 const auto now = std::chrono::system_clock::now();
47 const uint64_t diff_ns = std::chrono::duration_cast<std::chrono::duration<uint64_t,std::nano>>
49
50 // following nomenclature of wikipedia
51 const uint32_t time_low = diff_ns & 0xffffffff;
52 const uint16_t time_mid = (diff_ns >> 32) & 0xffff;
53 const uint16_t time_hi_and_version = ((diff_ns >> 48) & 0x0fff) | ( version << 12 );
54 const uint8_t clock_seq_high_and_reserved = ((diff_ns >> 8) & 0x3f) | ( 0x02 << 6 );
55 const uint8_t clock_seq_low = diff_ns & 0xff;
56
57 static std::mt19937_64 gen {diff_ns};
58 const uint64_t node = gen();
59
60 const Uuid id =
61 { ( static_cast<uint64_t>(time_low) << 32 ) |
62 ( static_cast<uint64_t>(time_mid) << 16 ) |
63 ( static_cast<uint64_t>(time_hi_and_version) ),
64 ( static_cast<uint64_t>(clock_seq_high_and_reserved) << 56 ) |
65 ( static_cast<uint64_t>(clock_seq_low) << 48 ) |
66 ( node & 0xffffffffffff )
67 };
68 return id;
69 }
70}
Sptr< Range > rangeCast(const RangePtr r)
constexpr std::tm mkTRef()
Definition uuid.cc:22
Uuid mkUuid()
Definition uuid.cc:38
cnorxz uuid declaration.