Whamcloud - gitweb
ChangeLog, Makefile.in, tst_uuid.c, uuid.h, uuid_time.c:
[tools/e2fsprogs.git] / lib / uuid / uuid_time.c
1 /*
2  * uuid_time.c --- Interpret the time field from a uuid
3  *
4  * Copyright (C) 1998 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <time.h>
18 #include <linux/ext2_fs.h>
19
20 #include "uuidP.h"
21
22 time_t uuid_time(uuid_t uu, struct timeval *ret_tv)
23 {
24         struct uuid             uuid;
25         __u32                   high;
26         struct timeval          tv;
27         unsigned long long      clock_reg;
28
29         uuid_unpack(uu, &uuid);
30         
31         high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
32         clock_reg = uuid.time_low | ((unsigned long long) high << 32);
33
34         clock_reg -= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
35         tv.tv_sec = clock_reg / 10000000;
36         tv.tv_usec = (clock_reg % 10000000) / 10;
37
38         if (ret_tv)
39                 *ret_tv = tv;
40
41         return tv.tv_sec;
42 }
43
44 #ifdef DEBUG
45 int
46 main(int argc, char **argv)
47 {
48         uuid_t          buf;
49         time_t          time_reg;
50         struct timeval  tv;
51
52         if (argc != 2) {
53                 fprintf(stderr, "Usage: %s uuid\n", argv[0]);
54                 exit(1);
55         }
56         if (uuid_parse(argv[1], buf)) {
57                 fprintf(stderr, "Invalid UUID: %s\n", argv[1]);
58                 exit(1);
59         }
60         time_reg = uuid_time(buf, &tv);
61
62         printf("UUID time is: (%d, %d): %s\n", tv.tv_sec, tv.tv_usec,
63                ctime(&time_reg));
64         
65         return 0;
66 }
67 #endif