1
/***************************************************************
7
You can freely use, copy, modify, and redistribute
9
***************************************************************/
11
/*#define __SCRATCH_TIMEC_DEBUG__ */
18
const __int64 _onesec_in100ns = (__int64)10000000;
19
int timezone, _timezone, altzone;
24
/* __int64 <--> FILETIME */
25
static __int64 wce_FILETIME2int64(FILETIME f)
35
static FILETIME wce_int642FILETIME(__int64 t)
39
f.dwHighDateTime = (DWORD)((t >> 32) & 0x00000000FFFFFFFF);
40
f.dwLowDateTime = (DWORD)( t & 0x00000000FFFFFFFF);
44
/* FILETIME utility */
45
static FILETIME wce_getFILETIMEFromYear(WORD year)
55
SystemTimeToFileTime( &s, &f );
59
static time_t wce_getYdayFromSYSTEMTIME(const SYSTEMTIME* s)
64
f1 = wce_getFILETIMEFromYear( s->wYear );
65
SystemTimeToFileTime( s, &f2 );
67
t = wce_FILETIME2int64(f2)-wce_FILETIME2int64(f1);
69
return (time_t)((t/_onesec_in100ns)/(60*60*24));
72
/* tm <--> SYSTEMTIME */
73
static SYSTEMTIME wce_tm2SYSTEMTIME(struct tm *t)
77
s.wYear = t->tm_year + 1900;
78
s.wMonth = t->tm_mon + 1;
79
s.wDayOfWeek = t->tm_wday;
82
s.wMinute = t->tm_min;
83
s.wSecond = t->tm_sec;
89
static struct tm wce_SYSTEMTIME2tm(SYSTEMTIME *s)
93
t.tm_year = s->wYear - 1900;
94
t.tm_mon = s->wMonth- 1;
95
t.tm_wday = s->wDayOfWeek;
97
t.tm_yday = wce_getYdayFromSYSTEMTIME(s);
99
t.tm_min = s->wMinute;
100
t.tm_sec = s->wSecond;
106
/* FILETIME <--> time_t */
107
time_t wce_FILETIME2time_t(const FILETIME* f)
109
FILETIME f1601, f1970;
112
f1601 = wce_getFILETIMEFromYear(1601);
113
f1970 = wce_getFILETIMEFromYear(1970);
115
offset = wce_FILETIME2int64(f1970) - wce_FILETIME2int64(f1601);
117
t = wce_FILETIME2int64(*f);
120
return (time_t)(t / _onesec_in100ns);
123
FILETIME wce_time_t2FILETIME(const time_t t)
128
f1970 = wce_getFILETIMEFromYear(1970);
131
time *= _onesec_in100ns;
132
time += wce_FILETIME2int64(f1970);
134
f = wce_int642FILETIME(time);
139
/* time.h difinition */
140
time_t time( time_t *timer )
145
if( timer==NULL ) return 0;
149
SystemTimeToFileTime( &s, &f );
151
*timer = wce_FILETIME2time_t(&f);
155
struct tm *localtime( const time_t *timer )
157
SYSTEMTIME ss, ls, s;
160
static struct tm tms;
165
SystemTimeToFileTime( &ss, &sf );
166
SystemTimeToFileTime( &ls, &lf );
168
diff = wce_FILETIME2int64(sf) - wce_FILETIME2int64(lf);
170
f = wce_time_t2FILETIME(*timer);
171
t = wce_FILETIME2int64(f) - diff;
172
f = wce_int642FILETIME(t);
174
FileTimeToSystemTime( &f, &s );
176
tms = wce_SYSTEMTIME2tm(&s);
181
time_t mktime(struct tm* pt)
183
SYSTEMTIME ss, ls, s;
189
SystemTimeToFileTime( &ss, &sf );
190
SystemTimeToFileTime( &ls, &lf );
192
diff = (wce_FILETIME2int64(lf)-wce_FILETIME2int64(sf))/_onesec_in100ns;
194
s = wce_tm2SYSTEMTIME(pt);
195
SystemTimeToFileTime( &s, &f );
196
return wce_FILETIME2time_t(&f) - (time_t)diff;
199
struct tm *gmtime(const time_t *t)
203
static struct tm tms;
205
f = wce_time_t2FILETIME(*t);
206
FileTimeToSystemTime(&f, &s);
207
tms = wce_SYSTEMTIME2tm(&s);
211
char* ctime( const time_t *t )
213
// Wed Jan 02 02:03:55 1980\n\0
214
static char buf[30]={0};
215
char week[] = "Sun Mon Tue Wed Thr Fri Sat ";
216
char month[]= "Jan Feb Mar Apl May Jun Jul Aug Sep Oct Nov Dec ";
221
strncpy( buf, week+tms.tm_wday*4, 4 );
222
strncpy( buf+4, month+tms.tm_mon*4, 4 );
223
sprintf( buf+8, "%02d ", tms.tm_mday );
224
sprintf( buf+11, "%02d:%02d:%02d %d\n",
225
tms.tm_hour, tms.tm_min, tms.tm_sec, tms.tm_year+1900 );
229
char *asctime(const struct tm *pt)
231
static char buf[30]={0};
232
char week[] = "Sun Mon Tue Wed Thr Fri Sat ";
233
char month[]= "Jan Feb Mar Apl May Jun Jul Aug Sep Oct Nov Dec ";
235
strncpy( buf, week+pt->tm_wday*4, 4 );
236
strncpy( buf+4, month+pt->tm_mon*4, 4 );
237
sprintf( buf+8, "%02d ", pt->tm_mday );
238
sprintf( buf+11, "%02d:%02d:%02d %d\n",
239
pt->tm_hour, pt->tm_min, pt->tm_sec, pt->tm_year+1900 );
255
//---------------------------------------------------------------
256
#ifdef __SCRATCH_TIMEC_DEBUG__
264
tm1 = *localtime(&t1);
269
tm2 = *_localtime(&t2);
275
OutputDebugString( "ok\n" );
278
static char buf[128];
279
wsprintf( buf, "ng : %d, %d\n", t1, t2 );
280
OutputDebugString( buf );
284
if( 0==memcmp( &tm1, &tm2, sizeof(struct tm) ) )
285
OutputDebugString( "ok\n" );
287
OutputDebugString( "ng\n" );
290
OutputDebugString( ctime(&t1) );
291
OutputDebugString( _ctime(&t2) );
294
OutputDebugString( asctime(&tm1) );
295
OutputDebugString( _asctime(&tm2) );