00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <windows.h>
00034 #include <tchar.h>
00035 #include "wce_time.h"
00036
00037
00038
00039 const __int64 _onesec_in100ns = (__int64)10000000;
00040
00041 int altzone;
00042 int daylight;
00043 char *tzname[2];
00044
00045
00046 int ftime(struct timeb *tp) {
00047
00048 FILETIME ft;
00049 SYSTEMTIME st;
00050 GetSystemTime(&st);
00051 SystemTimeToFileTime(&st, &ft);
00052
00053 struct timeb t = Filetime2timeb(&ft);
00054
00055
00056
00057
00058 *tp = t;
00059 return 0;
00060 }
00061
00062
00063
00064
00065 static __int64 wce_FILETIME2int64(FILETIME f)
00066 {
00067 __int64 t;
00068
00069 t = f.dwHighDateTime;
00070 t <<= 32;
00071 t |= f.dwLowDateTime;
00072 return t;
00073 }
00074
00075 static FILETIME wce_int642FILETIME(__int64 t)
00076 {
00077 FILETIME f;
00078
00079 f.dwHighDateTime = (DWORD)((t >> 32) & 0x00000000FFFFFFFF);
00080 f.dwLowDateTime = (DWORD)( t & 0x00000000FFFFFFFF);
00081 return f;
00082 }
00083
00084
00085 static FILETIME wce_getFILETIMEFromYear(WORD year)
00086 {
00087 SYSTEMTIME s={0};
00088 FILETIME f;
00089
00090 s.wYear = year;
00091 s.wMonth = 1;
00092 s.wDayOfWeek = 1;
00093 s.wDay = 1;
00094
00095 SystemTimeToFileTime( &s, &f );
00096 return f;
00097 }
00098
00099 static time_t wce_getYdayFromSYSTEMTIME(const SYSTEMTIME* s)
00100 {
00101 __int64 t;
00102 FILETIME f1, f2;
00103
00104 f1 = wce_getFILETIMEFromYear( s->wYear );
00105 SystemTimeToFileTime( s, &f2 );
00106
00107 t = wce_FILETIME2int64(f2)-wce_FILETIME2int64(f1);
00108
00109 return (time_t)((t/_onesec_in100ns)/(60*60*24));
00110 }
00111
00112
00113 static SYSTEMTIME wce_tm2SYSTEMTIME(struct tm *t)
00114 {
00115 SYSTEMTIME s;
00116
00117 s.wYear = t->tm_year + 1900;
00118 s.wMonth = t->tm_mon + 1;
00119 s.wDayOfWeek = t->tm_wday;
00120 s.wDay = t->tm_mday;
00121 s.wHour = t->tm_hour;
00122 s.wMinute = t->tm_min;
00123 s.wSecond = t->tm_sec;
00124 s.wMilliseconds = 0;
00125
00126 return s;
00127 }
00128
00129 static struct tm wce_SYSTEMTIME2tm(SYSTEMTIME *s)
00130 {
00131 struct tm t;
00132
00133 t.tm_year = s->wYear - 1900;
00134 t.tm_mon = s->wMonth- 1;
00135 t.tm_wday = s->wDayOfWeek;
00136 t.tm_mday = s->wDay;
00137 t.tm_yday = wce_getYdayFromSYSTEMTIME(s);
00138 t.tm_hour = s->wHour;
00139 t.tm_min = s->wMinute;
00140 t.tm_sec = s->wSecond;
00141 t.tm_isdst = 0;
00142
00143 return t;
00144 }
00145
00146
00147 time_t wce_FILETIME2time_t(const FILETIME* f)
00148 {
00149 FILETIME f1601, f1970;
00150 __int64 t, offset;
00151
00152 f1601 = wce_getFILETIMEFromYear(1601);
00153 f1970 = wce_getFILETIMEFromYear(1970);
00154
00155 offset = wce_FILETIME2int64(f1970) - wce_FILETIME2int64(f1601);
00156
00157 t = wce_FILETIME2int64(*f);
00158
00159 t -= offset;
00160 return (time_t)(t / _onesec_in100ns);
00161 }
00162
00163 FILETIME wce_time_t2FILETIME(const time_t t)
00164 {
00165 FILETIME f, f1970;
00166 __int64 time;
00167
00168 f1970 = wce_getFILETIMEFromYear(1970);
00169
00170 time = t;
00171 time *= _onesec_in100ns;
00172 time += wce_FILETIME2int64(f1970);
00173
00174 f = wce_int642FILETIME(time);
00175
00176 return f;
00177 }
00178
00179
00180 time_t time( time_t *timer )
00181 {
00182 SYSTEMTIME s;
00183 FILETIME f;
00184
00185 if( timer==NULL ) return 0;
00186
00187 GetSystemTime( &s );
00188
00189 SystemTimeToFileTime( &s, &f );
00190
00191 *timer = wce_FILETIME2time_t(&f);
00192 return *timer;
00193 }
00194
00195 struct tm *localtime( const time_t *timer )
00196 {
00197 SYSTEMTIME ss, ls, s;
00198 FILETIME sf, lf, f;
00199 __int64 t, diff;
00200 static struct tm tms;
00201
00202 GetSystemTime(&ss);
00203 GetLocalTime(&ls);
00204
00205 SystemTimeToFileTime( &ss, &sf );
00206 SystemTimeToFileTime( &ls, &lf );
00207
00208 diff = wce_FILETIME2int64(sf) - wce_FILETIME2int64(lf);
00209
00210 f = wce_time_t2FILETIME(*timer);
00211 t = wce_FILETIME2int64(f) - diff;
00212 f = wce_int642FILETIME(t);
00213
00214 FileTimeToSystemTime( &f, &s );
00215
00216 tms = wce_SYSTEMTIME2tm(&s);
00217
00218 return &tms;
00219 }
00220
00221 time_t mktime(struct tm* pt)
00222 {
00223 SYSTEMTIME ss, ls, s;
00224 FILETIME sf, lf, f;
00225 __int64 diff;
00226
00227 GetSystemTime(&ss);
00228 GetLocalTime(&ls);
00229 SystemTimeToFileTime( &ss, &sf );
00230 SystemTimeToFileTime( &ls, &lf );
00231
00232 diff = (wce_FILETIME2int64(lf)-wce_FILETIME2int64(sf))/_onesec_in100ns;
00233
00234 s = wce_tm2SYSTEMTIME(pt);
00235 SystemTimeToFileTime( &s, &f );
00236 return wce_FILETIME2time_t(&f) - (time_t)diff;
00237 }
00238
00239 struct tm *gmtime(const time_t *t)
00240 {
00241 FILETIME f;
00242 SYSTEMTIME s;
00243 static struct tm tms;
00244
00245 f = wce_time_t2FILETIME(*t);
00246 FileTimeToSystemTime(&f, &s);
00247 tms = wce_SYSTEMTIME2tm(&s);
00248 return &tms;
00249 }
00250
00251 char* ctime( const time_t *t )
00252 {
00253
00254 static char buf[30]={0};
00255 char week[] = "Sun Mon Tue Wed Thr Fri Sat ";
00256 char month[]= "Jan Feb Mar Apl May Jun Jul Aug Sep Oct Nov Dec ";
00257 struct tm tms;
00258
00259 tms = *localtime(t);
00260
00261 strncpy( buf, week+tms.tm_wday*4, 4 );
00262 strncpy( buf+4, month+tms.tm_mon*4, 4 );
00263 sprintf( buf+8, "%02d ", tms.tm_mday );
00264 sprintf( buf+11, "%02d:%02d:%02d %d\n",
00265 tms.tm_hour, tms.tm_min, tms.tm_sec, tms.tm_year+1900 );
00266 return buf;
00267 }
00268
00269 char *asctime(const struct tm *pt)
00270 {
00271 static char buf[30]={0};
00272 char week[] = "Sun Mon Tue Wed Thr Fri Sat ";
00273 char month[]= "Jan Feb Mar Apl May Jun Jul Aug Sep Oct Nov Dec ";
00274
00275 strncpy( buf, week+pt->tm_wday*4, 4 );
00276 strncpy( buf+4, month+pt->tm_mon*4, 4 );
00277 sprintf( buf+8, "%02d ", pt->tm_mday );
00278 sprintf( buf+11, "%02d:%02d:%02d %d\n",
00279 pt->tm_hour, pt->tm_min, pt->tm_sec, pt->tm_year+1900 );
00280 return buf;
00281 }
00282
00283 void tzset()
00284 {
00285
00286
00287
00288 }
00289
00290
00291
00292
00293
00294
00295 #define NS_PER_SEC 10000000
00296 #define NS_PER_MS 10000
00297 #define _FACTOR (0x19db1ded53ea710)
00298
00299 struct timeb Filetime2timeb(FILETIME * ptr)
00300 {
00301
00302
00303
00304
00305 long rem;
00306 __int64 x = ((__int64) ptr->dwHighDateTime << 32) + ((unsigned) ptr->dwLowDateTime);
00307 x -= _FACTOR;
00308 rem = (long) (x % ((__int64) NS_PER_MS));
00309 rem += (NS_PER_MS / 2);
00310 x /= (__int64) NS_PER_MS;
00311 x += (__int64) (rem / NS_PER_MS);
00312
00313
00314 struct timeb t;
00315 t.millitm = (unsigned short) x % 1000;
00316 t.time = (int) (x / 1000);
00317
00318 t.dstflag = 0;
00319 t.timezone = 0;
00320
00321 return t;
00322 }