00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <time.h>
00015 #include "timeconv.h"
00016
00017 char * fileTimeToAscii (const FILETIME *filetime) {
00018 time_t t1;
00019
00020 t1 = fileTimeToUnixTime(filetime, NULL);
00021 return ctime(&t1);
00022 }
00023
00024 struct tm * fileTimeToStructTM (const FILETIME *filetime) {
00025 time_t t1;
00026 t1 = fileTimeToUnixTime(filetime, NULL);
00027 return gmtime(&t1);
00028 }
00029
00030
00031
00032
00033
00034
00035
00036
00037 time_t fileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
00038 {
00039
00040 #if USE_LONG_LONG
00041
00042 long long int t = filetime->dwHighDateTime;
00043 t <<= 32;
00044 t += (UINT32)filetime->dwLowDateTime;
00045 t -= 116444736000000000LL;
00046 if (t < 0)
00047 {
00048 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
00049 return -1 - ((-t - 1) / 10000000);
00050 }
00051 else
00052 {
00053 if (remainder) *remainder = t % 10000000;
00054 return t / 10000000;
00055 }
00056
00057 #else
00058
00059 UINT32 a0;
00060 UINT32 a1;
00061 UINT32 a2;
00062 UINT32 r;
00063 unsigned int carry;
00064 int negative;
00065
00066
00067 a2 = (UINT32)filetime->dwHighDateTime;
00068 a1 = ((UINT32)filetime->dwLowDateTime ) >> 16;
00069 a0 = ((UINT32)filetime->dwLowDateTime ) & 0xffff;
00070
00071
00072 if (a0 >= 32768 ) a0 -= 32768 , carry = 0;
00073 else a0 += (1 << 16) - 32768 , carry = 1;
00074
00075 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0;
00076 else a1 += (1 << 16) - 54590 - carry, carry = 1;
00077
00078 a2 -= 27111902 + carry;
00079
00080
00081 negative = (a2 >= ((UINT32)1) << 31);
00082 if (negative)
00083 {
00084
00085 a0 = 0xffff - a0;
00086 a1 = 0xffff - a1;
00087 a2 = ~a2;
00088 }
00089
00090
00091
00092 a1 += (a2 % 10000) << 16;
00093 a2 /= 10000;
00094 a0 += (a1 % 10000) << 16;
00095 a1 /= 10000;
00096 r = a0 % 10000;
00097 a0 /= 10000;
00098
00099 a1 += (a2 % 1000) << 16;
00100 a2 /= 1000;
00101 a0 += (a1 % 1000) << 16;
00102 a1 /= 1000;
00103 r += (a0 % 1000) * 10000;
00104 a0 /= 1000;
00105
00106
00107 if (negative)
00108 {
00109
00110 a0 = 0xffff - a0;
00111 a1 = 0xffff - a1;
00112 a2 = ~a2;
00113
00114 r = 9999999 - r;
00115 }
00116
00117 if (remainder) *remainder = r;
00118
00119
00120
00121 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
00122 #endif
00123 }