Mercurial > libpst
annotate src/timeconv.c @ 122:bdb38b434c0a
more changes from Fridrich Strba to avoid installing our config.h
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 02 Feb 2009 21:55:48 -0800 |
parents | 0f1492b7fe8b |
children | ab2a11e72250 |
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 | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
75
diff
changeset
|
14 #ifndef _WIN32 |
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
75
diff
changeset
|
15 |
16 | 16 #include <time.h> |
122
bdb38b434c0a
more changes from Fridrich Strba to avoid installing our config.h
Carl Byington <carl@five-ten-sg.com>
parents:
118
diff
changeset
|
17 |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
75
diff
changeset
|
18 #include "common.h" |
16 | 19 #include "timeconv.h" |
20 | |
122
bdb38b434c0a
more changes from Fridrich Strba to avoid installing our config.h
Carl Byington <carl@five-ten-sg.com>
parents:
118
diff
changeset
|
21 #include "define.h" |
bdb38b434c0a
more changes from Fridrich Strba to avoid installing our config.h
Carl Byington <carl@five-ten-sg.com>
parents:
118
diff
changeset
|
22 |
16 | 23 char * fileTimeToAscii (const FILETIME *filetime) { |
24 time_t t1; | |
25 | |
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 ctime(&t1); |
28 } | |
29 | |
30 struct tm * fileTimeToStructTM (const FILETIME *filetime) { | |
31 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
|
32 t1 = fileTimeToUnixTime(filetime, NULL); |
16 | 33 return gmtime(&t1); |
34 } | |
35 | |
36 /*********************************************************************** | |
37 * DOSFS_FileTimeToUnixTime | |
38 * | |
39 * Convert a FILETIME format to Unix time. | |
40 * If not NULL, 'remainder' contains the fractional part of the filetime, | |
41 * in the range of [0..9999999] (even if time_t is negative). | |
42 */ | |
43 time_t fileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder ) | |
44 { | |
45 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */ | |
46 #if USE_LONG_LONG | |
47 | |
48 long long int t = filetime->dwHighDateTime; | |
49 t <<= 32; | |
50 t += (UINT32)filetime->dwLowDateTime; | |
51 t -= 116444736000000000LL; | |
52 if (t < 0) | |
53 { | |
54 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000; | |
55 return -1 - ((-t - 1) / 10000000); | |
56 } | |
57 else | |
58 { | |
59 if (remainder) *remainder = t % 10000000; | |
60 return t / 10000000; | |
61 } | |
62 | |
63 #else /* ISO version */ | |
64 | |
65 UINT32 a0; /* 16 bit, low bits */ | |
66 UINT32 a1; /* 16 bit, medium bits */ | |
67 UINT32 a2; /* 32 bit, high bits */ | |
68 UINT32 r; /* remainder of division */ | |
69 unsigned int carry; /* carry bit for subtraction */ | |
70 int negative; /* whether a represents a negative value */ | |
71 | |
72 /* Copy the time values to a2/a1/a0 */ | |
73 a2 = (UINT32)filetime->dwHighDateTime; | |
74 a1 = ((UINT32)filetime->dwLowDateTime ) >> 16; | |
75 a0 = ((UINT32)filetime->dwLowDateTime ) & 0xffff; | |
76 | |
77 /* Subtract the time difference */ | |
78 if (a0 >= 32768 ) a0 -= 32768 , carry = 0; | |
79 else a0 += (1 << 16) - 32768 , carry = 1; | |
80 | |
81 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0; | |
82 else a1 += (1 << 16) - 54590 - carry, carry = 1; | |
83 | |
84 a2 -= 27111902 + carry; | |
46 | 85 |
16 | 86 /* If a is negative, replace a by (-1-a) */ |
87 negative = (a2 >= ((UINT32)1) << 31); | |
88 if (negative) | |
89 { | |
90 /* Set a to -a - 1 (a is a2/a1/a0) */ | |
91 a0 = 0xffff - a0; | |
92 a1 = 0xffff - a1; | |
93 a2 = ~a2; | |
94 } | |
95 | |
96 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r. | |
97 Split the divisor into 10000 * 1000 which are both less than 0xffff. */ | |
98 a1 += (a2 % 10000) << 16; | |
99 a2 /= 10000; | |
100 a0 += (a1 % 10000) << 16; | |
101 a1 /= 10000; | |
102 r = a0 % 10000; | |
103 a0 /= 10000; | |
104 | |
105 a1 += (a2 % 1000) << 16; | |
106 a2 /= 1000; | |
107 a0 += (a1 % 1000) << 16; | |
108 a1 /= 1000; | |
109 r += (a0 % 1000) * 10000; | |
110 a0 /= 1000; | |
111 | |
112 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */ | |
113 if (negative) | |
114 { | |
115 /* Set a to -a - 1 (a is a2/a1/a0) */ | |
116 a0 = 0xffff - a0; | |
117 a1 = 0xffff - a1; | |
118 a2 = ~a2; | |
119 | |
120 r = 9999999 - r; | |
121 } | |
122 | |
123 if (remainder) *remainder = r; | |
124 | |
125 /* Do not replace this by << 32, it gives a compiler warning and it does | |
126 not work. */ | |
127 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0; | |
128 #endif | |
129 } | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
75
diff
changeset
|
130 |
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
75
diff
changeset
|
131 #endif /* !_WIN32 */ |