📄 time.c
字号:
/*
* Unit test suite for time functions
*
* Copyright 2004 Uwe Bonnes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "wine/test.h"
#include "winbase.h"
#define SECSPERMIN 60
#define SECSPERDAY 86400
/* 1601 to 1970 is 369 years plus 89 leap days */
#define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
#define TICKSPERSEC 10000000
#define TICKSPERMSEC 10000
#define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
#define NEWYEAR_1980_HI 0x01a8e79f
#define NEWYEAR_1980_LO 0xe1d58000
#define MAYDAY_2002_HI 0x01c1f107
#define MAYDAY_2002_LO 0xb82b6000
#define ATIME_HI 0x1c2349b
#define ATIME_LOW 0x580716b0
#define LOCAL_ATIME_HI 0x01c23471
#define LOCAL_ATIME_LOW 0x6f310eb0
#define DOS_DATE(y,m,d) ( (((y)-1980)<<9) | ((m)<<5) | (d) )
#define DOS_TIME(h,m,s) ( ((h)<<11) | ((m)<<5) | ((s)>>1) )
#define SETUP_1980(st) \
(st).wYear = 1980; \
(st).wMonth = 1; \
(st).wDay = 1; \
(st).wHour = 0; \
(st).wMinute = 0; \
(st).wSecond = 0; \
(st).wMilliseconds = 0;
#define SETUP_2002(st) \
(st).wYear = 2002; \
(st).wMonth = 5; \
(st).wDay = 1; \
(st).wHour = 12; \
(st).wMinute = 0; \
(st).wSecond = 0; \
(st).wMilliseconds = 0;
#define SETUP_ATIME(st) \
(st).wYear = 2002; \
(st).wMonth = 7; \
(st).wDay = 26; \
(st).wHour = 11; \
(st).wMinute = 55; \
(st).wSecond = 32; \
(st).wMilliseconds = 123;
static void test_conversions(void)
{
FILETIME ft;
SYSTEMTIME st;
memset(&ft,0,sizeof ft);
SETUP_ATIME(st)
ok (SystemTimeToFileTime(&st,&ft), "Conversion Failed ATIME\n");
ok( (!((ft.dwHighDateTime != ATIME_HI) || (ft.dwLowDateTime!=ATIME_LOW))),
"Wrong time for ATIME: %08lx %08lx (correct %08x %08x)\n",
ft.dwLowDateTime, ft.dwHighDateTime, ATIME_LOW, ATIME_HI);
SETUP_2002(st)
ok (SystemTimeToFileTime(&st, &ft), "Conversion failed 2002\n");
ok( (!((ft.dwHighDateTime != MAYDAY_2002_HI) ||
(ft.dwLowDateTime!=MAYDAY_2002_LO))),
"Wrong time for 2002 %08lx %08lx (correct %08x %08x)\n", ft.dwLowDateTime,
ft.dwHighDateTime, MAYDAY_2002_LO, MAYDAY_2002_HI);
SETUP_1980(st)
ok((SystemTimeToFileTime(&st, &ft)), "Conversion failed 1980\n");
ok( (!((ft.dwHighDateTime!=NEWYEAR_1980_HI) ||
(ft.dwLowDateTime!=NEWYEAR_1980_LO))) ,
"Wrong time for 1980 %08lx %08lx (correct %08x %08x)\n", ft.dwLowDateTime,
ft.dwHighDateTime, NEWYEAR_1980_LO,NEWYEAR_1980_HI );
ok(DosDateTimeToFileTime(DOS_DATE(1980,1,1),DOS_TIME(0,0,0),&ft),
"DosDateTimeToFileTime() failed\n");
ok( (!((ft.dwHighDateTime!=NEWYEAR_1980_HI) ||
(ft.dwLowDateTime!=NEWYEAR_1980_LO))),
"Wrong time DosDateTimeToFileTime %08lx %08lx (correct %08x %08x)\n",
ft.dwHighDateTime, ft.dwLowDateTime, NEWYEAR_1980_HI, NEWYEAR_1980_LO);
}
static void test_invalid_arg(void)
{
FILETIME ft;
SYSTEMTIME st;
/* Invalid argument checks */
memset(&ft,0,sizeof ft);
ok( DosDateTimeToFileTime(DOS_DATE(1980,1,1),DOS_TIME(0,0,0),&ft), /* this is 1 Jan 1980 00:00:00 */
"DosDateTimeToFileTime() failed\n");
ok( (ft.dwHighDateTime==NEWYEAR_1980_HI) && (ft.dwLowDateTime==NEWYEAR_1980_LO),
"filetime for 1/1/80 00:00:00 was %08lx %08lx\n", ft.dwHighDateTime, ft.dwLowDateTime);
/* now check SystemTimeToFileTime */
memset(&ft,0,sizeof ft);
/* try with a bad month */
SETUP_1980(st)
st.wMonth = 0;
ok( !SystemTimeToFileTime(&st, &ft), "bad month\n");
/* with a bad hour */
SETUP_1980(st)
st.wHour = 24;
ok( !SystemTimeToFileTime(&st, &ft), "bad hour\n");
/* with a bad minute */
SETUP_1980(st)
st.wMinute = 60;
ok( !SystemTimeToFileTime(&st, &ft), "bad minute\n");
}
static void test_GetTimeZoneInformation(void)
{
TIME_ZONE_INFORMATION tzinfo, tzinfo1;
DWORD res = GetTimeZoneInformation(&tzinfo);
ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
ok(SetEnvironmentVariableA("TZ","GMT0") != 0,
"SetEnvironmentVariableA failed\n");
res = GetTimeZoneInformation(&tzinfo1);
ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
ok(((tzinfo.Bias == tzinfo1.Bias) &&
(tzinfo.StandardBias == tzinfo1.StandardBias) &&
(tzinfo.DaylightBias == tzinfo1.DaylightBias)),
"Bias influenced by TZ variable\n");
ok(SetEnvironmentVariableA("TZ",NULL) != 0,
"SetEnvironmentVariableA failed\n");
}
static void test_FileTimeToSystemTime(void)
{
FILETIME ft;
SYSTEMTIME st;
ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
BOOL ret;
ft.dwHighDateTime = 0;
ft.dwLowDateTime = 0;
ret = FileTimeToSystemTime(&ft, &st);
ok( ret,
"FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
ok(((st.wYear == 1601) && (st.wMonth == 1) && (st.wDay == 1) &&
(st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 0) &&
(st.wMilliseconds == 0)),
"Got Year %4d Month %2d Day %2d\n", st.wYear, st.wMonth, st.wDay);
ft.dwHighDateTime = (UINT)(time >> 32);
ft.dwLowDateTime = (UINT)time;
ret = FileTimeToSystemTime(&ft, &st);
ok( ret,
"FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
(st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
(st.wMilliseconds == 0)),
"Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
st.wMilliseconds);
}
static void test_FileTimeToLocalFileTime(void)
{
FILETIME ft, lft;
SYSTEMTIME st;
TIME_ZONE_INFORMATION tzinfo;
DWORD res = GetTimeZoneInformation(&tzinfo);
ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970 +
(LONGLONG)(tzinfo.Bias +
( res == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
( res == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ))) *
SECSPERMIN *TICKSPERSEC;
BOOL ret;
ok( res != TIME_ZONE_ID_INVALID , "GetTimeZoneInformation failed\n");
ft.dwHighDateTime = (UINT)(time >> 32);
ft.dwLowDateTime = (UINT)time;
ret = FileTimeToLocalFileTime(&ft, &lft);
ok( ret,
"FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
FileTimeToSystemTime(&lft, &st);
ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
(st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
(st.wMilliseconds == 0)),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -