annotate src/timeconv.c @ 88:c664302d1606 stable-0-6-16

release 0.6.16
author Carl Byington <carl@five-ten-sg.com>
date Tue, 05 Aug 2008 10:04:23 -0700
parents 987aa872294e
children 0f1492b7fe8b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
1 /***********************************************************************
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
2 *
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
3 * Borrowed from WINE sources!! (http://www.winehq.com)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
4 * Converts a Win32 FILETIME structure to a UNIX time_t value
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
5 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
6
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
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
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
9 * It will make the whole project fail to compile
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10 * There are functions in libpst.c to handle the dates
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11 * Do not use this one
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
12 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
13
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
14 #include <time.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
15 #include "timeconv.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
17 char * fileTimeToAscii (const FILETIME *filetime) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18 time_t t1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
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
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
21 return ctime(&t1);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
22 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
23
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
24 struct tm * fileTimeToStructTM (const FILETIME *filetime) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
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
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
27 return gmtime(&t1);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
28 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
29
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
30 /***********************************************************************
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
31 * DOSFS_FileTimeToUnixTime
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
32 *
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
33 * Convert a FILETIME format to Unix time.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
34 * If not NULL, 'remainder' contains the fractional part of the filetime,
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
35 * in the range of [0..9999999] (even if time_t is negative).
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
36 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
37 time_t fileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
38 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
39 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
40 #if USE_LONG_LONG
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
41
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
42 long long int t = filetime->dwHighDateTime;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
43 t <<= 32;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
44 t += (UINT32)filetime->dwLowDateTime;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
45 t -= 116444736000000000LL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
46 if (t < 0)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
47 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
48 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
49 return -1 - ((-t - 1) / 10000000);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
50 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
51 else
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
52 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
53 if (remainder) *remainder = t % 10000000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
54 return t / 10000000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
55 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
56
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
57 #else /* ISO version */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
58
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
59 UINT32 a0; /* 16 bit, low bits */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
60 UINT32 a1; /* 16 bit, medium bits */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
61 UINT32 a2; /* 32 bit, high bits */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
62 UINT32 r; /* remainder of division */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
63 unsigned int carry; /* carry bit for subtraction */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
64 int negative; /* whether a represents a negative value */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
65
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
66 /* Copy the time values to a2/a1/a0 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
67 a2 = (UINT32)filetime->dwHighDateTime;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
68 a1 = ((UINT32)filetime->dwLowDateTime ) >> 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
69 a0 = ((UINT32)filetime->dwLowDateTime ) & 0xffff;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
70
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
71 /* Subtract the time difference */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
72 if (a0 >= 32768 ) a0 -= 32768 , carry = 0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
73 else a0 += (1 << 16) - 32768 , carry = 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
74
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
75 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
76 else a1 += (1 << 16) - 54590 - carry, carry = 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
77
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
78 a2 -= 27111902 + carry;
46
b2a7f2e0926a more fixes for 64 bit format
carl
parents: 16
diff changeset
79
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
80 /* If a is negative, replace a by (-1-a) */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
81 negative = (a2 >= ((UINT32)1) << 31);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
82 if (negative)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
83 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
84 /* Set a to -a - 1 (a is a2/a1/a0) */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
85 a0 = 0xffff - a0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
86 a1 = 0xffff - a1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
87 a2 = ~a2;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
88 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
89
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
90 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
91 Split the divisor into 10000 * 1000 which are both less than 0xffff. */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
92 a1 += (a2 % 10000) << 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
93 a2 /= 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
94 a0 += (a1 % 10000) << 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
95 a1 /= 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
96 r = a0 % 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
97 a0 /= 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
98
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
99 a1 += (a2 % 1000) << 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
100 a2 /= 1000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
101 a0 += (a1 % 1000) << 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
102 a1 /= 1000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
103 r += (a0 % 1000) * 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
104 a0 /= 1000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
105
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
106 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
107 if (negative)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
108 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
109 /* Set a to -a - 1 (a is a2/a1/a0) */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
110 a0 = 0xffff - a0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
111 a1 = 0xffff - a1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
112 a2 = ~a2;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
113
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
114 r = 9999999 - r;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
115 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
116
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
117 if (remainder) *remainder = r;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
118
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
119 /* Do not replace this by << 32, it gives a compiler warning and it does
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
120 not work. */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
121 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
122 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
123 }