2
* uuid_time.c --- Interpret the time field from a uuid. This program
3
* violates the UUID abstraction barrier by reaching into the guts
4
* of a UUID and interpreting it.
6
* Copyright (C) 1998, 1999 Theodore Ts'o.
9
* This file may be redistributed under the terms of the GNU
10
* Library General Public License.
17
#include <sys/types.h>
23
time_t uuid_time(const uuid_t uu, struct timeval *ret_tv)
28
unsigned long long clock_reg;
30
uuid_unpack(uu, &uuid);
32
high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
33
clock_reg = uuid.time_low | ((unsigned long long) high << 32);
35
clock_reg -= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
36
tv.tv_sec = clock_reg / 10000000;
37
tv.tv_usec = (clock_reg % 10000000) / 10;
45
int uuid_type(const uuid_t uu)
49
uuid_unpack(uu, &uuid);
50
return ((uuid.time_hi_and_version >> 12) & 0xF);
53
int uuid_variant(const uuid_t uu)
58
uuid_unpack(uu, &uuid);
61
if ((var & 0x8000) == 0)
62
return UUID_VARIANT_NCS;
63
if ((var & 0x4000) == 0)
64
return UUID_VARIANT_DCE;
65
if ((var & 0x2000) == 0)
66
return UUID_VARIANT_MICROSOFT;
67
return UUID_VARIANT_OTHER;
71
static const char *variant_string(int variant)
74
case UUID_VARIANT_NCS:
76
case UUID_VARIANT_DCE:
78
case UUID_VARIANT_MICROSOFT:
87
main(int argc, char **argv)
95
fprintf(stderr, "Usage: %s uuid\n", argv[0]);
98
if (uuid_parse(argv[1], buf)) {
99
fprintf(stderr, "Invalid UUID: %s\n", argv[1]);
102
variant = uuid_variant(buf);
103
type = uuid_type(buf);
104
time_reg = uuid_time(buf, &tv);
106
printf("UUID variant is %d (%s)\n", variant, variant_string(variant));
107
if (variant != UUID_VARIANT_DCE) {
108
printf("Warning: This program only knows how to interpret "
109
"DCE UUIDs.\n\tThe rest of the output is likely "
110
"to be incorrect!!\n");
112
printf("UUID type is %d", type);
115
printf(" (time based)\n");
121
printf(" (name-based)\n");
124
printf(" (random)\n");
130
printf("Warning: not a time-based UUID, so UUID time "
131
"decoding will likely not work!\n");
133
printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,