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 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, and the entire permission notice in its entirety,
14 * including the disclaimer of warranties.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
25 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
40 #include <sys/types.h>
46 time_t uuid_time(const uuid_t uu
, struct timeval
*ret_tv
)
53 uuid_unpack(uu
, &uuid
);
55 high
= uuid
.time_mid
| ((uuid
.time_hi_and_version
& 0xFFF) << 16);
56 clock_reg
= uuid
.time_low
| ((uint64_t) high
<< 32);
58 clock_reg
-= (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
59 tv
.tv_sec
= clock_reg
/ 10000000;
60 tv
.tv_usec
= (clock_reg
% 10000000) / 10;
68 int uuid_type(const uuid_t uu
)
72 uuid_unpack(uu
, &uuid
);
73 return ((uuid
.time_hi_and_version
>> 12) & 0xF);
76 int uuid_variant(const uuid_t uu
)
81 uuid_unpack(uu
, &uuid
);
84 if ((var
& 0x8000) == 0)
85 return UUID_VARIANT_NCS
;
86 if ((var
& 0x4000) == 0)
87 return UUID_VARIANT_DCE
;
88 if ((var
& 0x2000) == 0)
89 return UUID_VARIANT_MICROSOFT
;
90 return UUID_VARIANT_OTHER
;
94 static const char *variant_string(int variant
)
97 case UUID_VARIANT_NCS
:
99 case UUID_VARIANT_DCE
:
101 case UUID_VARIANT_MICROSOFT
:
110 main(int argc
, char **argv
)
118 fprintf(stderr
, "Usage: %s uuid\n", argv
[0]);
121 if (uuid_parse(argv
[1], buf
)) {
122 fprintf(stderr
, "Invalid UUID: %s\n", argv
[1]);
125 variant
= uuid_variant(buf
);
126 type
= uuid_type(buf
);
127 time_reg
= uuid_time(buf
, &tv
);
129 printf("UUID variant is %d (%s)\n", variant
, variant_string(variant
));
130 if (variant
!= UUID_VARIANT_DCE
) {
131 printf("Warning: This program only knows how to interpret "
132 "DCE UUIDs.\n\tThe rest of the output is likely "
133 "to be incorrect!!\n");
135 printf("UUID type is %d", type
);
138 printf(" (time based)\n");
144 printf(" (name-based)\n");
147 printf(" (random)\n");
153 printf("Warning: not a time-based UUID, so UUID time "
154 "decoding will likely not work!\n");
156 printf("UUID time is: (%ld, %ld): %s\n", tv
.tv_sec
, (long)tv
.tv_usec
,