msvcrt: Route mktime and localtime to glibc.

This commit is contained in:
Dmitry Timoshkov 2007-08-21 14:25:28 +09:00 committed by Alexandre Julliard
parent 87f119cfe0
commit 1e5b131d7a
2 changed files with 61 additions and 128 deletions

View File

@ -48,6 +48,7 @@ static void test_gmtime(void)
gmt_tm->tm_hour, gmt_tm->tm_min, gmt_tm->tm_sec, gmt_tm->tm_isdst);
}
static void test_mktime(void)
{
TIME_ZONE_INFORMATION tzinfo;
@ -75,17 +76,19 @@ static void test_mktime(void)
sav_tm = my_tm;
local_time = mktime(&my_tm);
ok(((DWORD)local_time == SECSPERDAY), "mktime returned 0x%08x\n",(DWORD)local_time);
ok(((DWORD)local_time == SECSPERDAY), "mktime returned %u, expected %u\n",
(DWORD)local_time, SECSPERDAY);
/* now test some unnormalized struct tm's */
my_tm = sav_tm;
my_tm.tm_sec += 60;
my_tm.tm_min -= 1;
local_time = mktime(&my_tm);
ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned 0x%08x\n",(DWORD)local_time);
ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned %u, expected %u\n",
(DWORD)local_time, SECSPERDAY);
ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
my_tm.tm_sec == sav_tm.tm_sec
, "mktime returned %3d-%02d-%02d %02d:%02d expected %3d-%02d-%02d %02d:%02d.\n",
my_tm.tm_sec == sav_tm.tm_sec,
"mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
my_tm.tm_hour,my_tm.tm_sec,
sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
@ -94,11 +97,12 @@ static void test_mktime(void)
my_tm.tm_min -= 60;
my_tm.tm_hour += 1;
local_time = mktime(&my_tm);
ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned 0x%08x\n",(DWORD)local_time);
ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned %u, expected %u\n",
(DWORD)local_time, SECSPERDAY);
ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
my_tm.tm_sec == sav_tm.tm_sec
, "mktime returned %3d-%02d-%02d %02d:%02d expected %3d-%02d-%02d %02d:%02d.\n",
my_tm.tm_sec == sav_tm.tm_sec,
"mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
my_tm.tm_hour,my_tm.tm_sec,
sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
@ -107,11 +111,12 @@ static void test_mktime(void)
my_tm.tm_mon -= 12;
my_tm.tm_year += 1;
local_time = mktime(&my_tm);
ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned 0x%08x\n",(DWORD)local_time);
ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned %u, expected %u\n",
(DWORD)local_time, SECSPERDAY);
ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
my_tm.tm_sec == sav_tm.tm_sec
, "mktime returned %3d-%02d-%02d %02d:%02d expected %3d-%02d-%02d %02d:%02d.\n",
my_tm.tm_sec == sav_tm.tm_sec,
"mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
my_tm.tm_hour,my_tm.tm_sec,
sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
@ -120,11 +125,12 @@ static void test_mktime(void)
my_tm.tm_mon += 12;
my_tm.tm_year -= 1;
local_time = mktime(&my_tm);
ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned 0x%08x\n",(DWORD)local_time);
ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned %u, expected %u\n",
(DWORD)local_time, SECSPERDAY);
ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
my_tm.tm_sec == sav_tm.tm_sec
, "mktime returned %3d-%02d-%02d %02d:%02d expected %3d-%02d-%02d %02d:%02d.\n",
my_tm.tm_sec == sav_tm.tm_sec,
"mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
my_tm.tm_hour,my_tm.tm_sec,
sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
@ -133,7 +139,7 @@ static void test_mktime(void)
my_tm = sav_tm;
my_tm.tm_year -= 1;
local_time = mktime(&my_tm);
ok((local_time == -1), "(bad time) mktime returned 0x%08x\n",(DWORD)local_time);
ok((local_time == -1), "(bad time) mktime returned %d, expected -1\n", (int)local_time);
my_tm = sav_tm;
/* TEST that we are independent from the TZ variable */
@ -144,6 +150,7 @@ static void test_mktime(void)
ok(((DWORD)nulltime == SECSPERDAY),"mktime returned 0x%08x\n",(DWORD)nulltime);
putenv(TZ_env);
}
static void test_localtime(void)
{
TIME_ZONE_INFORMATION tzinfo;
@ -158,7 +165,7 @@ static void test_localtime(void)
ok(((lt->tm_year == 70) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
(lt->tm_mday == 2) && (lt->tm_wday == 5) && (lt->tm_hour == 0) &&
(lt->tm_min == 0) && (lt->tm_sec == 0) && (lt->tm_isdst == 0)),
"Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour %2d min %2d sec %2d dst %2d\n",
"Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
lt->tm_min, lt->tm_sec, lt->tm_isdst);
@ -168,24 +175,24 @@ static void test_localtime(void)
ok(((lt->tm_year == 70) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
(lt->tm_mday == 2) && (lt->tm_wday == 5) && (lt->tm_hour == 0) &&
(lt->tm_min == 0) && (lt->tm_sec == 0) && (lt->tm_isdst == 0)),
"Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour %2d min %2d sec %2d dst %2d\n",
"Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
lt->tm_min, lt->tm_sec, lt->tm_isdst);
putenv(TZ_env);
/* June 22 */
gmt += 201 * SECSPERDAY +
( res == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
( res == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 )) * SECSPERMIN;
gmt += 201 * SECSPERDAY;
lt = localtime(&gmt);
gmt += (lt->tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
lt = localtime(&gmt);
ok(((lt->tm_year == 70) && (lt->tm_mon == 6) && (lt->tm_yday == 202) &&
(lt->tm_mday == 22) && (lt->tm_wday == 3) && (lt->tm_hour == 0) &&
(lt->tm_min == 0) && (lt->tm_sec == 0) && (lt->tm_isdst ==
(res == TIME_ZONE_ID_DAYLIGHT))),
"Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour %2d min %2d sec %2d dst %2d\n",
(lt->tm_min == 0) && (lt->tm_sec == 0)),
"Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
lt->tm_min, lt->tm_sec, lt->tm_isdst);
}
static void test_strdate(void)
{
char date[16], * result;
@ -198,6 +205,7 @@ static void test_strdate(void)
count = sscanf(date, "%02d/%02d/%02d", &month, &day, &year);
ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
}
static void test_strtime(void)
{
char time[16], * result;
@ -210,6 +218,7 @@ static void test_strtime(void)
count = sscanf(time, "%02d:%02d:%02d", &hour, &minute, &second);
ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
}
static void test_wstrdate(void)
{
wchar_t date[16], * result;
@ -223,6 +232,7 @@ static void test_wstrdate(void)
count = swscanf(date, format, &month, &day, &year);
ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
}
static void test_wstrtime(void)
{
wchar_t time[16], * result;

View File

@ -63,6 +63,20 @@ static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *s
dest->tm_isdst = src->tm_isdst;
}
static inline void unix_tm_to_msvcrt( struct MSVCRT_tm *dest, const struct tm *src )
{
memset( dest, 0, sizeof(*dest) );
dest->tm_sec = src->tm_sec;
dest->tm_min = src->tm_min;
dest->tm_hour = src->tm_hour;
dest->tm_mday = src->tm_mday;
dest->tm_mon = src->tm_mon;
dest->tm_year = src->tm_year;
dest->tm_wday = src->tm_wday;
dest->tm_yday = src->tm_yday;
dest->tm_isdst = src->tm_isdst;
}
#define SECSPERDAY 86400
/* 1601 to 1970 is 369 years plus 89 leap days */
#define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
@ -73,74 +87,16 @@ static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *s
/**********************************************************************
* mktime (MSVCRT.@)
*/
MSVCRT_time_t CDECL MSVCRT_mktime(struct MSVCRT_tm *t)
MSVCRT_time_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
{
MSVCRT_time_t secs;
FILETIME lft, uft;
ULONGLONG time;
struct MSVCRT_tm ts, *ptm;
int cleaps, day;
time_t secs;
struct tm tm;
ts=*t;
/* to prevent arithmetic overflows put constraints on some fields */
/* whether the effective date falls in the 1970-2038 time period */
/* will be tested later */
/* BTW, I have no idea what limits native msvcrt has. */
if ( ts.tm_year < 0 || ts.tm_year > 140 ||
ts.tm_mon < -840 || ts.tm_mon > 840 ||
ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
ts.tm_min < -29000000 || ts.tm_min > 29000000 )
return -1;
/* normalize the tm month fields */
if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
if( ts.tm_mon < 0) {
int dy = (11 - ts.tm_mon) / 12;
ts.tm_year -= dy;
ts.tm_mon += dy * 12;
}
/* now calculate a day count from the date
* First start counting years from March. This way the leap days
* are added at the end of the year, not somewhere in the middle.
* Formula's become so much less complicate that way.
* To convert: add 12 to the month numbers of Jan and Feb, and
* take 1 from the year */
if(ts.tm_mon < 2) {
ts.tm_mon += 14;
ts.tm_year += 1899;
} else {
ts.tm_mon += 2;
ts.tm_year += 1900;
}
cleaps = (3 * (ts.tm_year / 100) + 3) / 4; /* nr of "century leap years"*/
day = (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
(1959 * ts.tm_mon) / 64 + /* months * daypermonth */
ts.tm_mday - /* day of the month */
584817 ; /* zero that on 1601-01-01 */
/* done */
msvcrt_tm_to_unix( &tm, mstm );
secs = mktime( &tm );
unix_tm_to_msvcrt( mstm, &tm );
/* convert to 100 ns ticks */
time = ((((ULONGLONG) day * 24 +
ts.tm_hour) * 60 +
ts.tm_min) * 60 +
ts.tm_sec ) * TICKSPERSEC;
lft.dwHighDateTime = (DWORD) (time >> 32);
lft.dwLowDateTime = (DWORD) time;
LocalFileTimeToFileTime(&lft, &uft);
time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
time /= TICKSPERSEC;
if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
return -1;
secs = time - SECS_1601_TO_1970;
/* compute tm_wday, tm_yday and renormalize the other fields of the
* tm structure */
if ((ptm = MSVCRT_localtime( &secs ))) *t = *ptm;
return secs;
return secs < 0 ? -1 : secs;
}
/*********************************************************************
@ -148,47 +104,14 @@ MSVCRT_time_t CDECL MSVCRT_mktime(struct MSVCRT_tm *t)
*/
struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT_time_t* secs)
{
thread_data_t * const data = msvcrt_get_thread_data();
int i;
FILETIME ft, lft;
SYSTEMTIME st;
DWORD tzid;
TIME_ZONE_INFORMATION tzinfo;
ULONGLONG time;
struct tm tm;
thread_data_t *data = msvcrt_get_thread_data();
time_t seconds = *secs;
/* time < 0 means a date before midnight of January 1, 1970 */
if (*secs < 0) return NULL;
localtime_r( &seconds, &tm );
unix_tm_to_msvcrt( &data->time_buffer, &tm );
time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
ft.dwHighDateTime = (UINT)(time >> 32);
ft.dwLowDateTime = (UINT)time;
FileTimeToLocalFileTime(&ft, &lft);
FileTimeToSystemTime(&lft, &st);
data->time_buffer.tm_sec = st.wSecond;
data->time_buffer.tm_min = st.wMinute;
data->time_buffer.tm_hour = st.wHour;
data->time_buffer.tm_mday = st.wDay;
data->time_buffer.tm_year = st.wYear - 1900;
data->time_buffer.tm_mon = st.wMonth - 1;
data->time_buffer.tm_wday = st.wDayOfWeek;
for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
}
data->time_buffer.tm_yday += st.wDay - 1;
tzid = GetTimeZoneInformation(&tzinfo);
if (tzid == TIME_ZONE_ID_INVALID)
data->time_buffer.tm_isdst = -1;
else
data->time_buffer.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
return &data->time_buffer;
return &data->time_buffer;
}
/*********************************************************************
@ -232,7 +155,7 @@ struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT_time_t* secs)
*/
char* CDECL _strdate(char* date)
{
LPCSTR format = "MM'/'dd'/'yy";
static const char format[] = "MM'/'dd'/'yy";
GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
@ -256,7 +179,7 @@ MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
*/
char* CDECL _strtime(char* time)
{
LPCSTR format = "HH':'mm':'ss";
static const char format[] = "HH':'mm':'ss";
GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);