Add function to create custom v8 UUIDs that use date&time for the first 6 byte in a human-readable form
This commit is contained in:
49
openVulkanoCpp/Base/UUID.cpp
Normal file
49
openVulkanoCpp/Base/UUID.cpp
Normal file
@@ -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 <ctime>
|
||||
|
||||
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<uint16_t>(uuid.version()) << 12);
|
||||
uuid.time_hiv |= 8 << 12;
|
||||
|
||||
return uuid;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user