comparison timeconv.c @ 0:6b1b602514db libpst_0_5

Initial revision
author carl
date Fri, 09 Jul 2004 07:26:16 -0700
parents
children 43e8802f08c5
comparison
equal deleted inserted replaced
-1:000000000000 0:6b1b602514db
1 /***********************************************************************
2 *
3 * Borrowed from WINE sources!! (http://www.winehq.com)
4 * Converts a Win32 FILETIME structure to a UNIX time_t value
5 */
6
7 /*** WARNING ****
8 * This file is not to be incluided in a Visual C++ Project
9 * It will make the whole project fail to compile
10 * There are functions in libpst.c to handle the dates
11 * Do not use this one
12 */
13
14 #include <time.h>
15 #include "common.h"
16
17 time_t fileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder );
18 char * fileTimeToAscii (const FILETIME *filetime);
19
20 char * fileTimeToAscii (const FILETIME *filetime) {
21 time_t t1;
22
23 t1 = fileTimeToUnixTime(filetime,0);
24 return ctime(&t1);
25 }
26
27 struct tm * fileTimeToStructTM (const FILETIME *filetime) {
28 time_t t1;
29 t1 = fileTimeToUnixTime(filetime, 0);
30 return gmtime(&t1);
31 }
32
33 /***********************************************************************
34 * DOSFS_FileTimeToUnixTime
35 *
36 * Convert a FILETIME format to Unix time.
37 * If not NULL, 'remainder' contains the fractional part of the filetime,
38 * in the range of [0..9999999] (even if time_t is negative).
39 */
40 time_t fileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
41 {
42 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
43 #if USE_LONG_LONG
44
45 long long int t = filetime->dwHighDateTime;
46 t <<= 32;
47 t += (UINT32)filetime->dwLowDateTime;
48 t -= 116444736000000000LL;
49 if (t < 0)
50 {
51 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
52 return -1 - ((-t - 1) / 10000000);
53 }
54 else
55 {
56 if (remainder) *remainder = t % 10000000;
57 return t / 10000000;
58 }
59
60 #else /* ISO version */
61
62 UINT32 a0; /* 16 bit, low bits */
63 UINT32 a1; /* 16 bit, medium bits */
64 UINT32 a2; /* 32 bit, high bits */
65 UINT32 r; /* remainder of division */
66 unsigned int carry; /* carry bit for subtraction */
67 int negative; /* whether a represents a negative value */
68
69 /* Copy the time values to a2/a1/a0 */
70 a2 = (UINT32)filetime->dwHighDateTime;
71 a1 = ((UINT32)filetime->dwLowDateTime ) >> 16;
72 a0 = ((UINT32)filetime->dwLowDateTime ) & 0xffff;
73
74 /* Subtract the time difference */
75 if (a0 >= 32768 ) a0 -= 32768 , carry = 0;
76 else a0 += (1 << 16) - 32768 , carry = 1;
77
78 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0;
79 else a1 += (1 << 16) - 54590 - carry, carry = 1;
80
81 a2 -= 27111902 + carry;
82
83 /* If a is negative, replace a by (-1-a) */
84 negative = (a2 >= ((UINT32)1) << 31);
85 if (negative)
86 {
87 /* Set a to -a - 1 (a is a2/a1/a0) */
88 a0 = 0xffff - a0;
89 a1 = 0xffff - a1;
90 a2 = ~a2;
91 }
92
93 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
94 Split the divisor into 10000 * 1000 which are both less than 0xffff. */
95 a1 += (a2 % 10000) << 16;
96 a2 /= 10000;
97 a0 += (a1 % 10000) << 16;
98 a1 /= 10000;
99 r = a0 % 10000;
100 a0 /= 10000;
101
102 a1 += (a2 % 1000) << 16;
103 a2 /= 1000;
104 a0 += (a1 % 1000) << 16;
105 a1 /= 1000;
106 r += (a0 % 1000) * 10000;
107 a0 /= 1000;
108
109 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
110 if (negative)
111 {
112 /* Set a to -a - 1 (a is a2/a1/a0) */
113 a0 = 0xffff - a0;
114 a1 = 0xffff - a1;
115 a2 = ~a2;
116
117 r = 9999999 - r;
118 }
119
120 if (remainder) *remainder = r;
121
122 /* Do not replace this by << 32, it gives a compiler warning and it does
123 not work. */
124 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
125 #endif
126 }