1// Copyright (C) 2020 The Qt Company Ltd.
2// Copyright (C) 2021 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QUUID_P_H
6#define QUUID_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of the QLibrary class. This header file may change from
14// version to version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include "quuid.h"
20
21#include <QtCore/qrandom.h>
22
23#include <chrono>
24
25QT_BEGIN_NAMESPACE
26
27#ifndef QT_BOOTSTRAPPED
28static inline QUuid createUuidV7_internal(
29 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> tp)
30{
31 QUuid result;
32
33 using namespace std::chrono;
34 const nanoseconds nsecSinceEpoch = tp.time_since_epoch();
35 const auto msecSinceEpoch = floor<milliseconds>(d: nsecSinceEpoch);
36 const quint64 frac = (nsecSinceEpoch - msecSinceEpoch).count();
37 // Lower 48 bits of the timestamp
38 const quint64 msecs = quint64(msecSinceEpoch.count()) & 0xffffffffffff;
39 result.data1 = uint(msecs >> 16);
40 result.data2 = ushort(msecs);
41 // rand_a: use a 12-bit sub-millisecond timestamp for additional monotonicity
42 // https://datatracker.ietf.org/doc/html/rfc9562#monotonicity_counters (Method 3)
43
44 // "frac" is a number between 0 and 999,999, so the lowest 20 bits
45 // should be roughly random. Use the high 12 of those for additional
46 // monotonicity.
47 result.data3 = frac >> 8;
48 result.data3 &= 0x0FFF;
49 result.data3 |= ushort(7) << 12;
50
51 // rand_b: 62 bits of random data (64 - 2 bits for the variant)
52 const quint64 random = QRandomGenerator::system()->generate64();
53 memcpy(dest: result.data4, src: &random, n: sizeof(quint64));
54 result.data4[0] = (result.data4[0] & 0x3F) | 0x80; // UV_DCE
55 return result;
56}
57#endif
58
59QT_END_NAMESPACE
60
61#endif // QUUID_P_H
62

source code of qtbase/src/corelib/plugin/quuid_p.h