Mercurial > libpst
annotate src/timeconv.c @ 93:cb14583c119a
iconv changes for Mac
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 28 Aug 2008 15:25:53 -0700 |
parents | 987aa872294e |
children | 0f1492b7fe8b |
rev | line source |
---|---|
16 | 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 **** | |
59
7d5c637aaafb
General cleanup and code fixes.
Carl Byington <carl@five-ten-sg.com>
parents:
46
diff
changeset
|
8 * This file is not to be included in a Visual C++ Project |
16 | 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 "timeconv.h" | |
16 | |
17 char * fileTimeToAscii (const FILETIME *filetime) { | |
18 time_t t1; | |
19 | |
63
cfd6175f9334
Start work on pst2dii to convert to Summation dii load file format.
Carl Byington <carl@five-ten-sg.com>
parents:
59
diff
changeset
|
20 t1 = fileTimeToUnixTime(filetime, NULL); |
16 | 21 return ctime(&t1); |
22 } | |
23 | |
24 struct tm * fileTimeToStructTM (const FILETIME *filetime) { | |
25 time_t t1; | |
63
cfd6175f9334
Start work on pst2dii to convert to Summation dii load file format.
Carl Byington <carl@five-ten-sg.com>
parents:
59
diff
changeset
|
26 t1 = fileTimeToUnixTime(filetime, NULL); |
16 | 27 return gmtime(&t1); |
28 } | |
29 | |
30 /*********************************************************************** | |
31 * DOSFS_FileTimeToUnixTime | |
32 * | |
33 * Convert a FILETIME format to Unix time. | |
34 * If not NULL, 'remainder' contains the fractional part of the filetime, | |
35 * in the range of [0..9999999] (even if time_t is negative). | |
36 */ | |
37 time_t fileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder ) | |
38 { | |
39 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */ | |
40 #if USE_LONG_LONG | |
41 | |
42 long long int t = filetime->dwHighDateTime; | |
43 t <<= 32; | |
44 t += (UINT32)filetime->dwLowDateTime; | |
45 t -= 116444736000000000LL; | |
46 if (t < 0) | |
47 { | |
48 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000; | |
49 return -1 - ((-t - 1) / 10000000); | |
50 } | |
51 else | |
52 { | |
53 if (remainder) *remainder = t % 10000000; | |
54 return t / 10000000; | |
55 } | |
56 | |
57 #else /* ISO version */ | |
58 | |
59 UINT32 a0; /* 16 bit, low bits */ | |
60 UINT32 a1; /* 16 bit, medium bits */ | |
61 UINT32 a2; /* 32 bit, high bits */ | |
62 UINT32 r; /* remainder of division */ | |
63 unsigned int carry; /* carry bit for subtraction */ | |
64 int negative; /* whether a represents a negative value */ | |
65 | |
66 /* Copy the time values to a2/a1/a0 */ | |
67 a2 = (UINT32)filetime->dwHighDateTime; | |
68 a1 = ((UINT32)filetime->dwLowDateTime ) >> 16; | |
69 a0 = ((UINT32)filetime->dwLowDateTime ) & 0xffff; | |
70 | |
71 /* Subtract the time difference */ | |
72 if (a0 >= 32768 ) a0 -= 32768 , carry = 0; | |
73 else a0 += (1 << 16) - 32768 , carry = 1; | |
74 | |
75 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0; | |
76 else a1 += (1 << 16) - 54590 - carry, carry = 1; | |
77 | |
78 a2 -= 27111902 + carry; | |
46 | 79 |
16 | 80 /* If a is negative, replace a by (-1-a) */ |
81 negative = (a2 >= ((UINT32)1) << 31); | |
82 if (negative) | |
83 { | |
84 /* Set a to -a - 1 (a is a2/a1/a0) */ | |
85 a0 = 0xffff - a0; | |
86 a1 = 0xffff - a1; | |
87 a2 = ~a2; | |
88 } | |
89 | |
90 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r. | |
91 Split the divisor into 10000 * 1000 which are both less than 0xffff. */ | |
92 a1 += (a2 % 10000) << 16; | |
93 a2 /= 10000; | |
94 a0 += (a1 % 10000) << 16; | |
95 a1 /= 10000; | |
96 r = a0 % 10000; | |
97 a0 /= 10000; | |
98 | |
99 a1 += (a2 % 1000) << 16; | |
100 a2 /= 1000; | |
101 a0 += (a1 % 1000) << 16; | |
102 a1 /= 1000; | |
103 r += (a0 % 1000) * 10000; | |
104 a0 /= 1000; | |
105 | |
106 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */ | |
107 if (negative) | |
108 { | |
109 /* Set a to -a - 1 (a is a2/a1/a0) */ | |
110 a0 = 0xffff - a0; | |
111 a1 = 0xffff - a1; | |
112 a2 = ~a2; | |
113 | |
114 r = 9999999 - r; | |
115 } | |
116 | |
117 if (remainder) *remainder = r; | |
118 | |
119 /* Do not replace this by << 32, it gives a compiler warning and it does | |
120 not work. */ | |
121 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0; | |
122 #endif | |
123 } |