Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jesterjunk/191900d9c7d7715280b8faa0dc1a7e76 to your computer and use it in GitHub Desktop.

Select an option

Save jesterjunk/191900d9c7d7715280b8faa0dc1a7e76 to your computer and use it in GitHub Desktop.
iso8601basic nanosecond precision timestamp format + uuid

iso8601basic nanosecond precision timestamp format + uuid

iso8601basic + uuid

YYYYMMDDTHHMMSSnnnnnnnnnZuuid

4     YYYY         Year (4 digits)
2     MM           Month (2 digits, 01-12)
2     DD           Day of the month (2 digits, 01-31)
1     T            Separator between date and time
2     HH           Hour (2 digits, 00-24)
2     MM           Minutes (2 digits, 00-59)
2     SS           Seconds (2 digits, 00-60)
9     nnnnnnnnn    Nanoseconds (9 digits)
1     Z            Time zone designator (Z for UTC, +/-HHMM for offset from UTC)
32    uuid         128-bit UUID (Universal Unique Identifier)

Example output:
20231220T092700921579500Z6a598740ac3e4f75b32ed23f096bcbc2

╭─    iso8601basic      ╮
YYYYMMDDTHHMMSSnnnnnnnnnZ              uuid
╭──╮╭╮╭╮│╭╮╭╮╭╮╭───────╮│╭──────────────────────────────╮
20231220T092700921579500Z6a598740ac3e4f75b32ed23f096bcbc2

01__timestamp_with_comments.c raw
02__timestamp.c raw
/*
Filename:
timestamp.c
Description:
This program generates a timestamp in ISO8601 format, appends a hyphen-free UUID to it,
and prints the result. The timestamp includes the date, time, and nanoseconds.
Compile Command:
gcc -o timestamp.exe timestamp.c -lrpcrt4
Example Output:
20231220T092700921579500Z6a598740ac3e4f75b32ed23f096bcbc2
╭─ iso8601basic ╮
YYYYMMDDTHHMMSSnnnnnnnnnZ uuid
╭──╮╭╮╭╮│╭╮╭╮╭╮╭───────╮│╭──────────────────────────────╮
20231220T092700921579500Z6a598740ac3e4f75b32ed23f096bcbc2
*/
// Include the Standard Input/Output library
#include <stdio.h>
// Include the Time library
#include <time.h>
// Include the Windows-specific headers
#include <windows.h>
// Include the RPC library for UUID functions
#include <rpc.h>
// Entry point of the program
int main() {
// Declare a timespec structure to hold high-precision time
struct timespec res;
// Check the resolution of CLOCK_REALTIME and handle errors
if (clock_getres(CLOCK_REALTIME, &res) != 0) {
// Print an error message and return 1 if clock resolution fetching fails
printf("Failed to get clock resolution\n");
// Return 1 to indicate an error occurred
return 1;
}
// Declare a pointer to a struct tm for broken-down time
struct tm *tm;
// Declare a buffer to hold the formatted date-time string
char buf[30];
// Declare a string to hold the UUID including hyphens
char uuid_str[37];
// Declare a string to hold the UUID without hyphens
char uuid_no_hyphens[33];
// Fetch the current time and store it in res
clock_gettime(CLOCK_REALTIME, &res);
// Convert the time in seconds to GMT/UTC
tm = gmtime(&(res.tv_sec));
// Format the time into a string in the specified format
strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", tm);
// Declare a UUID variable
UUID uuid;
// Generate a new UUID
UuidCreate(&uuid);
// Declare a variable to hold the RPC string
RPC_CSTR uuid_rpc_str;
// Convert the UUID to a string
UuidToStringA(&uuid, &uuid_rpc_str);
// Copy the UUID string to uuid_str
strncpy(uuid_str, (char*)uuid_rpc_str, 36);
// Ensure null termination of the UUID string
uuid_str[36] = '\0';
// Initialize a variable for indexing through uuid_no_hyphens
int j = 0;
// Iterate over the UUID string
for (int i = 0; i < 36; i++) {
// Check if the current character is not a hyphen
if (uuid_str[i] != '-') {
// Copy non-hyphen characters to uuid_no_hyphens
uuid_no_hyphens[j++] = uuid_str[i];
}
}
// Null-terminate the new UUID string
uuid_no_hyphens[j] = '\0';
// Print the formatted date-time string with nanoseconds and the hyphen-free UUID
printf("%s%09ldZ%s\n", buf, res.tv_nsec, uuid_no_hyphens);
// Free the memory allocated for the UUID string
RpcStringFreeA(&uuid_rpc_str);
// Return 0 indicating successful execution
return 0;
}
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <rpc.h>
int main() {
struct timespec res;
if (clock_getres(CLOCK_REALTIME, &res) != 0) {
printf("Failed to get clock resolution\n");
return 1;
}
struct tm *tm;
char buf[30];
char uuid_str[37];
char uuid_no_hyphens[33];
clock_gettime(CLOCK_REALTIME, &res);
tm = gmtime(&(res.tv_sec));
strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", tm);
UUID uuid;
UuidCreate(&uuid);
RPC_CSTR uuid_rpc_str;
UuidToStringA(&uuid, &uuid_rpc_str);
strncpy(uuid_str, (char*)uuid_rpc_str, 36);
uuid_str[36] = '\0';
int j = 0;
for (int i = 0; i < 36; i++) {
if (uuid_str[i] != '-') {
uuid_no_hyphens[j++] = uuid_str[i];
}
}
uuid_no_hyphens[j] = '\0';
printf("%s%09ldZ%s\n", buf, res.tv_nsec, uuid_no_hyphens);
RpcStringFreeA(&uuid_rpc_str);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment