From 2334d48856bfeea37875bd4d02881179cf3cda32 Mon Sep 17 00:00:00 2001 From: Georg Hagen Date: Wed, 18 Dec 2024 00:08:57 +0100 Subject: [PATCH] Add function to create custom v8 UUIDs that use date&time for the first 6 byte in a human-readable form --- 3rdParty/libstud-uuid/CMakeLists.txt | 2 +- openVulkanoCpp/Base/UUID.cpp | 49 ++++++++++++++++++++++++++++ openVulkanoCpp/Base/UUID.hpp | 2 ++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 openVulkanoCpp/Base/UUID.cpp diff --git a/3rdParty/libstud-uuid/CMakeLists.txt b/3rdParty/libstud-uuid/CMakeLists.txt index 1e9aae3..c0569a5 100644 --- a/3rdParty/libstud-uuid/CMakeLists.txt +++ b/3rdParty/libstud-uuid/CMakeLists.txt @@ -1,7 +1,7 @@ include(FetchContent) if(NOT DEFINED LIBSTUD_REPO) - set(LIBSTUD_REPO https://github.com/GeorgH93/libstud-uuid.git) + set(LIBSTUD_REPO https://git.madvoxel.net/Mirrors/libstud-uuid.git) endif () FetchContent_Declare( diff --git a/openVulkanoCpp/Base/UUID.cpp b/openVulkanoCpp/Base/UUID.cpp new file mode 100644 index 0000000..ca70295 --- /dev/null +++ b/openVulkanoCpp/Base/UUID.cpp @@ -0,0 +1,49 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "UUID.hpp" +#include + +namespace OpenVulkano +{ + UUID GenerateTimePrefixedCustomUUID() + { + UUID uuid = UUID::generate(); + + uuid.time_low = 0; + uuid.time_mid = 0; + time_t now = time(nullptr); + struct tm* timeinfo = localtime(&now); + + // Extract time components + uint32_t year = timeinfo->tm_year % 100; // Get last two digits of year + uint32_t month = timeinfo->tm_mon + 1; // Month is 0-based + uint32_t day = timeinfo->tm_mday; + uint32_t hour = timeinfo->tm_hour; + uint16_t minute = timeinfo->tm_min; + uint16_t second = timeinfo->tm_sec; + + // Each decimal digit takes 4 bits in the final hex number + uuid.time_low |= (year / 10) << 28; + uuid.time_low |= (year % 10) << 24; + uuid.time_low |= (month / 10) << 20; + uuid.time_low |= (month % 10) << 16; + uuid.time_low |= (day / 10) << 12; + uuid.time_low |= (day % 10) << 8; + uuid.time_low |= (hour / 10) << 4; + uuid.time_low |= (hour % 10) << 0; + uuid.time_mid |= (minute / 10) << 12; + uuid.time_mid |= (minute % 10) << 8; + uuid.time_mid |= (second / 10) << 4; + uuid.time_mid |= (second % 10) << 0; + + // Set version to custom + uuid.time_hiv &= ~(static_cast(uuid.version()) << 12); + uuid.time_hiv |= 8 << 12; + + return uuid; + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Base/UUID.hpp b/openVulkanoCpp/Base/UUID.hpp index 0fa9288..3e02aed 100644 --- a/openVulkanoCpp/Base/UUID.hpp +++ b/openVulkanoCpp/Base/UUID.hpp @@ -11,4 +11,6 @@ namespace OpenVulkano { typedef stud::uuid UUID; + + UUID GenerateTimePrefixedCustomUUID(); }