time.c 39.8 KB
Newer Older
1 2 3 4
/*
 * Unit test suite for time functions
 *
 * Copyright 2004 Uwe Bonnes
5
 * Copyright 2007 Dmitry Timoshkov
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23
 */

#include "wine/test.h"
#include "winbase.h"
24
#include "winnls.h"
25
#include "winternl.h"
26

27 28
static BOOL (WINAPI *pTzSpecificLocalTimeToSystemTime)(LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
static BOOL (WINAPI *pSystemTimeToTzSpecificLocalTime)(LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
29
static BOOL (WINAPI *pGetSystemTimes)(LPFILETIME, LPFILETIME, LPFILETIME);
30 31
static int (WINAPI *pGetCalendarInfoA)(LCID,CALID,CALTYPE,LPSTR,int,LPDWORD);
static int (WINAPI *pGetCalendarInfoW)(LCID,CALID,CALTYPE,LPWSTR,int,LPDWORD);
32
static DWORD (WINAPI *pGetDynamicTimeZoneInformation)(DYNAMIC_TIME_ZONE_INFORMATION*);
33
static void (WINAPI *pGetSystemTimePreciseAsFileTime)(LPFILETIME);
34
static BOOL (WINAPI *pGetTimeZoneInformationForYear)(USHORT, PDYNAMIC_TIME_ZONE_INFORMATION, LPTIME_ZONE_INFORMATION);
35

36 37 38 39 40 41 42 43 44
#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)


45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#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;

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#define SETUP_ZEROTIME(st) \
    (st).wYear = 1601; \
    (st).wMonth = 1; \
    (st).wDay = 1; \
    (st).wHour = 0; \
    (st).wMinute = 0; \
    (st).wSecond = 0; \
    (st).wMilliseconds = 0;

#define SETUP_EARLY(st) \
    (st).wYear = 1600; \
    (st).wMonth = 12; \
    (st).wDay = 31; \
    (st).wHour = 23; \
    (st).wMinute = 59; \
    (st).wSecond = 59; \
    (st).wMilliseconds = 999;
105 106


107
static void test_conversions(void)
108 109 110 111 112 113
{
    FILETIME ft;
    SYSTEMTIME st;

    memset(&ft,0,sizeof ft);

114
    SetLastError(0xdeadbeef);
115 116
    SETUP_EARLY(st)
    ok (!SystemTimeToFileTime(&st, &ft), "Conversion succeeded EARLY\n");
117 118 119
    ok (GetLastError() == ERROR_INVALID_PARAMETER ||
        GetLastError() == 0xdeadbeef, /* win9x */
        "EARLY should be INVALID\n");
120 121 122 123 124 125 126 127

    SETUP_ZEROTIME(st)
    ok (SystemTimeToFileTime(&st, &ft), "Conversion failed ZERO_TIME\n");
    ok( (!((ft.dwHighDateTime != 0) || (ft.dwLowDateTime != 0))),
        "Wrong time for ATIME: %08x %08x (correct %08x %08x)\n",
        ft.dwLowDateTime, ft.dwHighDateTime, 0, 0);


128 129 130
    SETUP_ATIME(st)
    ok (SystemTimeToFileTime(&st,&ft), "Conversion Failed ATIME\n");
    ok( (!((ft.dwHighDateTime != ATIME_HI) || (ft.dwLowDateTime!=ATIME_LOW))),
131
        "Wrong time for ATIME: %08x %08x (correct %08x %08x)\n",
132 133 134 135 136 137 138 139
        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))),
140
        "Wrong time for 2002 %08x %08x (correct %08x %08x)\n", ft.dwLowDateTime,
141 142 143 144 145 146 147 148
        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))) ,
149
        "Wrong time for 1980 %08x %08x (correct %08x %08x)\n", ft.dwLowDateTime,
150 151 152 153 154 155 156
         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))),
157
        "Wrong time DosDateTimeToFileTime %08x %08x (correct %08x %08x)\n",
158 159 160 161
        ft.dwHighDateTime, ft.dwLowDateTime, NEWYEAR_1980_HI, NEWYEAR_1980_LO);

}

162
static void test_invalid_arg(void)
163 164 165 166 167 168 169
{
    FILETIME ft;
    SYSTEMTIME st;


    /* Invalid argument checks */

Jakob Eriksson's avatar
Jakob Eriksson committed
170
    memset(&ft,0,sizeof ft);
171 172 173 174
    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),
175
        "filetime for 1/1/80 00:00:00 was %08x %08x\n", ft.dwHighDateTime, ft.dwLowDateTime);
176 177 178 179 180 181 182 183 184 185 186 187 188

    /* 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)
189
    st.wHour = 24;
190 191 192 193 194

    ok( !SystemTimeToFileTime(&st, &ft), "bad hour\n");

    /* with a bad minute */
    SETUP_1980(st)
195
    st.wMinute = 60;
196 197 198

    ok( !SystemTimeToFileTime(&st, &ft), "bad minute\n");
}
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

static LONGLONG system_time_to_minutes(const SYSTEMTIME *st)
{
    BOOL ret;
    FILETIME ft;
    LONGLONG minutes;

    SetLastError(0xdeadbeef);
    ret = SystemTimeToFileTime(st, &ft);
    ok(ret, "SystemTimeToFileTime error %u\n", GetLastError());

    minutes = ((LONGLONG)ft.dwHighDateTime << 32) + ft.dwLowDateTime;
    minutes /= (LONGLONG)600000000; /* convert to minutes */
    return minutes;
}

static LONG get_tz_bias(const TIME_ZONE_INFORMATION *tzinfo, DWORD tz_id)
{
    switch (tz_id)
    {
    case TIME_ZONE_ID_DAYLIGHT:
220 221 222
        if (memcmp(&tzinfo->StandardDate, &tzinfo->DaylightDate, sizeof(tzinfo->DaylightDate)) != 0)
            return tzinfo->DaylightBias;
        /* fall through */
223 224 225 226 227 228 229 230 231 232 233

    case TIME_ZONE_ID_STANDARD:
        return tzinfo->StandardBias;

    default:
        trace("unknown time zone id %d\n", tz_id);
        /* fall through */
    case TIME_ZONE_ID_UNKNOWN:
        return 0;
    }
}
234
 
235
static void test_GetTimeZoneInformation(void)
236
{
237
    char std_name[32], dlt_name[32];
238
    TIME_ZONE_INFORMATION tzinfo, tzinfo1;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    BOOL res;
    DWORD tz_id;
    SYSTEMTIME st, current, utc, local;
    FILETIME l_ft, s_ft;
    LONGLONG l_time, s_time;
    LONG diff;

    GetSystemTime(&st);
    s_time = system_time_to_minutes(&st);

    SetLastError(0xdeadbeef);
    res = SystemTimeToFileTime(&st, &s_ft);
    ok(res, "SystemTimeToFileTime error %u\n", GetLastError());
    SetLastError(0xdeadbeef);
    res = FileTimeToLocalFileTime(&s_ft, &l_ft);
    ok(res, "FileTimeToLocalFileTime error %u\n", GetLastError());
    SetLastError(0xdeadbeef);
    res = FileTimeToSystemTime(&l_ft, &local);
    ok(res, "FileTimeToSystemTime error %u\n", GetLastError());
    l_time = system_time_to_minutes(&local);

    tz_id = GetTimeZoneInformation(&tzinfo);
    ok(tz_id != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");

263 264 265 266 267 268
    trace("tz_id %u (%s)\n", tz_id,
          tz_id == TIME_ZONE_ID_DAYLIGHT ? "TIME_ZONE_ID_DAYLIGHT" :
          (tz_id == TIME_ZONE_ID_STANDARD ? "TIME_ZONE_ID_STANDARD" :
          (tz_id == TIME_ZONE_ID_UNKNOWN ? "TIME_ZONE_ID_UNKNOWN" :
          "TIME_ZONE_ID_INVALID")));

269 270 271
    WideCharToMultiByte(CP_ACP, 0, tzinfo.StandardName, -1, std_name, sizeof(std_name), NULL, NULL);
    WideCharToMultiByte(CP_ACP, 0, tzinfo.DaylightName, -1, dlt_name, sizeof(dlt_name), NULL, NULL);
    trace("bias %d, %s - %s\n", tzinfo.Bias, std_name, dlt_name);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    trace("standard (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
        tzinfo.StandardDate.wDay, tzinfo.StandardDate.wMonth,
        tzinfo.StandardDate.wYear, tzinfo.StandardDate.wDayOfWeek,
        tzinfo.StandardDate.wHour, tzinfo.StandardDate.wMinute,
        tzinfo.StandardDate.wSecond, tzinfo.StandardDate.wMilliseconds,
        tzinfo.StandardBias);
    trace("daylight (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
        tzinfo.DaylightDate.wDay, tzinfo.DaylightDate.wMonth,
        tzinfo.DaylightDate.wYear, tzinfo.DaylightDate.wDayOfWeek,
        tzinfo.DaylightDate.wHour, tzinfo.DaylightDate.wMinute,
        tzinfo.DaylightDate.wSecond, tzinfo.DaylightDate.wMilliseconds,
        tzinfo.DaylightBias);

    diff = (LONG)(s_time - l_time);
    ok(diff == tzinfo.Bias + get_tz_bias(&tzinfo, tz_id),
       "system/local diff %d != tz bias %d\n",
       diff, tzinfo.Bias + get_tz_bias(&tzinfo, tz_id));

290 291 292
    ok(SetEnvironmentVariableA("TZ","GMT0") != 0,
       "SetEnvironmentVariableA failed\n");
    res =  GetTimeZoneInformation(&tzinfo1);
293
    ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
294 295 296 297 298 299 300

    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");
301

302 303
    if (!pSystemTimeToTzSpecificLocalTime)
    {
304
        win_skip("SystemTimeToTzSpecificLocalTime not available\n");
305 306 307
        return;
    }

308 309 310 311 312
    diff = get_tz_bias(&tzinfo, tz_id);

    utc = st;
    SetLastError(0xdeadbeef);
    res = pSystemTimeToTzSpecificLocalTime(&tzinfo, &utc, &current);
313 314
    if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
315
        win_skip("SystemTimeToTzSpecificLocalTime is not implemented\n");
316 317 318
        return;
    }

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    ok(res, "SystemTimeToTzSpecificLocalTime error %u\n", GetLastError());
    s_time = system_time_to_minutes(&current);

    tzinfo.StandardBias -= 123;
    tzinfo.DaylightBias += 456;

    res = pSystemTimeToTzSpecificLocalTime(&tzinfo, &utc, &local);
    ok(res, "SystemTimeToTzSpecificLocalTime error %u\n", GetLastError());
    l_time = system_time_to_minutes(&local);
    ok(l_time - s_time == diff - get_tz_bias(&tzinfo, tz_id), "got %d, expected %d\n",
       (LONG)(l_time - s_time), diff - get_tz_bias(&tzinfo, tz_id));

    /* pretend that there is no transition dates */
    tzinfo.DaylightDate.wDay = 0;
    tzinfo.DaylightDate.wMonth = 0;
    tzinfo.DaylightDate.wYear = 0;
    tzinfo.StandardDate.wDay = 0;
    tzinfo.StandardDate.wMonth = 0;
    tzinfo.StandardDate.wYear = 0;

    res = pSystemTimeToTzSpecificLocalTime(&tzinfo, &utc, &local);
    ok(res, "SystemTimeToTzSpecificLocalTime error %u\n", GetLastError());
    l_time = system_time_to_minutes(&local);
    ok(l_time - s_time == diff, "got %d, expected %d\n",
       (LONG)(l_time - s_time), diff);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

    /* test 23:01, 31st of December date */
    memset(&tzinfo, 0, sizeof(tzinfo));
    tzinfo.StandardDate.wMonth = 10;
    tzinfo.StandardDate.wDay = 5;
    tzinfo.StandardDate.wHour = 2;
    tzinfo.StandardDate.wMinute = 0;
    tzinfo.DaylightDate.wMonth = 4;
    tzinfo.DaylightDate.wDay = 1;
    tzinfo.DaylightDate.wHour = 2;
    tzinfo.Bias = 0;
    tzinfo.StandardBias = 0;
    tzinfo.DaylightBias = -60;
    utc.wYear = 2012;
    utc.wMonth = 12;
    utc.wDay = 31;
    utc.wHour = 23;
    utc.wMinute = 1;
    res = pSystemTimeToTzSpecificLocalTime(&tzinfo, &utc, &local);
    ok(res, "SystemTimeToTzSpecificLocalTime error %u\n", GetLastError());
    ok(local.wYear==2012 && local.wMonth==12 && local.wDay==31 && local.wHour==23 && local.wMinute==1,
            "got (%d-%d-%d %02d:%02d), expected (2012-12-31 23:01)\n",
            local.wYear, local.wMonth, local.wDay, local.wHour, local.wMinute);
367 368
}

369
static void test_FileTimeToSystemTime(void)
370 371 372 373
{
    FILETIME ft;
    SYSTEMTIME st;
    ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
374
    BOOL ret;
375 376 377

    ft.dwHighDateTime = 0;
    ft.dwLowDateTime  = 0;
378 379
    ret = FileTimeToSystemTime(&ft, &st);
    ok( ret,
380
       "FileTimeToSystemTime() failed with Error %d\n",GetLastError());
381 382 383 384 385 386 387
    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;
388 389
    ret = FileTimeToSystemTime(&ft, &st);
    ok( ret,
390
       "FileTimeToSystemTime() failed with Error %d\n",GetLastError());
391 392 393 394 395 396 397 398
    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);
}

399
static void test_FileTimeToLocalFileTime(void)
400 401 402 403 404
{
    FILETIME ft, lft;
    SYSTEMTIME st;
    TIME_ZONE_INFORMATION tzinfo;
    DWORD res =  GetTimeZoneInformation(&tzinfo);
405 406 407 408 409
    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;
410 411
    BOOL ret;

412
    ok( res != TIME_ZONE_ID_INVALID , "GetTimeZoneInformation failed\n");
413 414
    ft.dwHighDateTime = (UINT)(time >> 32);
    ft.dwLowDateTime  = (UINT)time;
415 416
    ret = FileTimeToLocalFileTime(&ft, &lft);
    ok( ret,
417
       "FileTimeToLocalFileTime() failed with Error %d\n",GetLastError());
418 419 420 421 422 423 424 425 426 427
    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)),
       "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);

    ok(SetEnvironmentVariableA("TZ","GMT") != 0,
       "SetEnvironmentVariableA failed\n");
428
    ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
429 430
    ret = FileTimeToLocalFileTime(&ft, &lft);
    ok( ret,
431
       "FileTimeToLocalFileTime() failed with Error %d\n",GetLastError());
432 433 434 435 436 437 438 439 440 441 442
    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)),
       "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);
    ok(SetEnvironmentVariableA("TZ",NULL) != 0,
       "SetEnvironmentVariableA failed\n");
}

443 444 445 446 447 448 449
typedef struct {
    int nr;             /* test case number for easier lookup */
    TIME_ZONE_INFORMATION *ptz; /* ptr to timezone */
    SYSTEMTIME slt;     /* system/local time to convert */
    WORD ehour;        /* expected hour */
} TZLT2ST_case;

450
static void test_TzSpecificLocalTimeToSystemTime(void)
451 452 453
{    
    TIME_ZONE_INFORMATION tzE, tzW, tzS;
    SYSTEMTIME result;
454
    int i, j, year;
455 456 457

    if (!pTzSpecificLocalTimeToSystemTime || !pSystemTimeToTzSpecificLocalTime)
    {
458
        win_skip("TzSpecificLocalTimeToSystemTime or SystemTimeToTzSpecificLocalTime not available\n");
459
        return;
460 461
    }

462 463 464 465 466 467 468 469
    ZeroMemory( &tzE, sizeof(tzE));
    ZeroMemory( &tzW, sizeof(tzW));
    ZeroMemory( &tzS, sizeof(tzS));
    /* timezone Eastern hemisphere */
    tzE.Bias=-600;
    tzE.StandardBias=0;
    tzE.DaylightBias=-60;
    tzE.StandardDate.wMonth=10;
470 471
    tzE.StandardDate.wDayOfWeek=0; /* Sunday */
    tzE.StandardDate.wDay=5;       /* last (Sunday) of the month */
472 473 474 475 476 477 478 479 480
    tzE.StandardDate.wHour=3;
    tzE.DaylightDate.wMonth=3;
    tzE.DaylightDate.wDay=5;
    tzE.DaylightDate.wHour=2;
    /* timezone Western hemisphere */
    tzW.Bias=240;
    tzW.StandardBias=0;
    tzW.DaylightBias=-60;
    tzW.StandardDate.wMonth=10;
481 482
    tzW.StandardDate.wDayOfWeek=0; /* Sunday */
    tzW.StandardDate.wDay=4;       /* 4th (Sunday) of the month */
483 484 485 486
    tzW.StandardDate.wHour=2;
    tzW.DaylightDate.wMonth=4;
    tzW.DaylightDate.wDay=1;
    tzW.DaylightDate.wHour=2;
487
    /* timezone Southern hemisphere */
488 489 490 491
    tzS.Bias=240;
    tzS.StandardBias=0;
    tzS.DaylightBias=-60;
    tzS.StandardDate.wMonth=4;
492 493
    tzS.StandardDate.wDayOfWeek=0; /*Sunday */
    tzS.StandardDate.wDay=1;       /* 1st (Sunday) of the month */
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    tzS.StandardDate.wHour=2;
    tzS.DaylightDate.wMonth=10;
    tzS.DaylightDate.wDay=4;
    tzS.DaylightDate.wHour=2;
    /*tests*/
        /* TzSpecificLocalTimeToSystemTime */
    {   TZLT2ST_case cases[] = {
            /* standard->daylight transition */
            { 1, &tzE, {2004,3,-1,28,1,0,0,0}, 15 },
            { 2, &tzE, {2004,3,-1,28,1,59,59,999}, 15},
            { 3, &tzE, {2004,3,-1,28,2,0,0,0}, 15},
            /* daylight->standard transition */
            { 4, &tzE, {2004,10,-1,31,2,0,0,0} , 15 },
            { 5, &tzE, {2004,10,-1,31,2,59,59,999}, 15 },
            { 6, &tzE, {2004,10,-1,31,3,0,0,0}, 17 },
            /* West and with fixed weekday of the month */
            { 7, &tzW, {2004,4,-1,4,1,0,0,0}, 5},
            { 8, &tzW, {2004,4,-1,4,1,59,59,999}, 5},
            { 9, &tzW, {2004,4,-1,4,2,0,0,0}, 5},
            { 10, &tzW, {2004,10,-1,24,1,0,0,0}, 4},
            { 11, &tzW, {2004,10,-1,24,1,59,59,999}, 4},
            { 12, &tzW, {2004,10,-1,24,2,0,0,0 }, 6},
516
            /* and now South */
517 518 519 520 521 522 523
            { 13, &tzS, {2004,4,-1,4,1,0,0,0}, 4},
            { 14, &tzS, {2004,4,-1,4,1,59,59,999}, 4},
            { 15, &tzS, {2004,4,-1,4,2,0,0,0}, 6},
            { 16, &tzS, {2004,10,-1,24,1,0,0,0}, 5},
            { 17, &tzS, {2004,10,-1,24,1,59,59,999}, 5},
            { 18, &tzS, {2004,10,-1,24,2,0,0,0}, 5},
            {0}
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
        };
    /*  days of transitions to put into the cases array */
        int yeardays[][6]=
        {
              {28,31,4,24,4,24}  /* 1999 */
            , {26,29,2,22,2,22}  /* 2000 */
            , {25,28,1,28,1,28}  /* 2001 */
            , {31,27,7,27,7,27}  /* 2002 */
            , {30,26,6,26,6,26}  /* 2003 */
            , {28,31,4,24,4,24}  /* 2004 */
            , {27,30,3,23,3,23}  /* 2005 */
            , {26,29,2,22,2,22}  /* 2006 */
            , {25,28,1,28,1,28}  /* 2007 */
            , {30,26,6,26,6,26}  /* 2008 */
            , {29,25,5,25,5,25}  /* 2009 */
            , {28,31,4,24,4,24}  /* 2010 */
            , {27,30,3,23,3,23}  /* 2011 */
            , {25,28,1,28,1,28}  /* 2012 */
            , {31,27,7,27,7,27}  /* 2013 */
            , {30,26,6,26,6,26}  /* 2014 */
            , {29,25,5,25,5,25}  /* 2015 */
            , {27,30,3,23,3,23}  /* 2016 */
            , {26,29,2,22,2,22}  /* 2017 */
            , {25,28,1,28,1,28}  /* 2018 */
            , {31,27,7,27,7,27}  /* 2019 */
            ,{0}
        };
        for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
            for (i=0; cases[i].nr; i++) {
                if(i) cases[i].nr += 18;
                cases[i].slt.wYear = year;
                cases[i].slt.wDay = yeardays[j][i/3];
                pTzSpecificLocalTimeToSystemTime( cases[i].ptz, &(cases[i].slt), &result);
                ok( result.wHour == cases[i].ehour,
                        "Test TzSpecificLocalTimeToSystemTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour =  %02d\n", 
                        cases[i].nr, result.wYear, result.wMonth, result.wDay,
                        result.wHour, result.wMinute, cases[i].ehour);
            }
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
        }
    }
        /* SystemTimeToTzSpecificLocalTime */
    {   TZLT2ST_case cases[] = {
            /* standard->daylight transition */
            { 1, &tzE, {2004,3,-1,27,15,0,0,0}, 1 },
            { 2, &tzE, {2004,3,-1,27,15,59,59,999}, 1},
            { 3, &tzE, {2004,3,-1,27,16,0,0,0}, 3},
            /* daylight->standard transition */
            { 4,  &tzE, {2004,10,-1,30,15,0,0,0}, 2 },
            { 5, &tzE, {2004,10,-1,30,15,59,59,999}, 2 },
            { 6, &tzE, {2004,10,-1,30,16,0,0,0}, 2 },
            /* West and with fixed weekday of the month */
            { 7, &tzW, {2004,4,-1,4,5,0,0,0}, 1},
            { 8, &tzW, {2004,4,-1,4,5,59,59,999}, 1},
            { 9, &tzW, {2004,4,-1,4,6,0,0,0}, 3},
            { 10, &tzW, {2004,10,-1,24,4,0,0,0}, 1},
            { 11, &tzW, {2004,10,-1,24,4,59,59,999}, 1},
            { 12, &tzW, {2004,10,-1,24,5,0,0,0 }, 1},
581
            /* and now South */
582 583 584 585 586 587 588 589
            { 13, &tzS, {2004,4,-1,4,4,0,0,0}, 1},
            { 14, &tzS, {2004,4,-1,4,4,59,59,999}, 1},
            { 15, &tzS, {2004,4,-1,4,5,0,0,0}, 1},
            { 16, &tzS, {2004,10,-1,24,5,0,0,0}, 1},
            { 17, &tzS, {2004,10,-1,24,5,59,59,999}, 1},
            { 18, &tzS, {2004,10,-1,24,6,0,0,0}, 3},

            {0}
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
        }; 
    /*  days of transitions to put into the cases array */
        int yeardays[][6]=
        {
              {27,30,4,24,4,24}  /* 1999 */
            , {25,28,2,22,2,22}  /* 2000 */
            , {24,27,1,28,1,28}  /* 2001 */
            , {30,26,7,27,7,27}  /* 2002 */
            , {29,25,6,26,6,26}  /* 2003 */
            , {27,30,4,24,4,24}  /* 2004 */
            , {26,29,3,23,3,23}  /* 2005 */
            , {25,28,2,22,2,22}  /* 2006 */
            , {24,27,1,28,1,28}  /* 2007 */
            , {29,25,6,26,6,26}  /* 2008 */
            , {28,24,5,25,5,25}  /* 2009 */
            , {27,30,4,24,4,24}  /* 2010 */
            , {26,29,3,23,3,23}  /* 2011 */
            , {24,27,1,28,1,28}  /* 2012 */
            , {30,26,7,27,7,27}  /* 2013 */
            , {29,25,6,26,6,26}  /* 2014 */
            , {28,24,5,25,5,25}  /* 2015 */
            , {26,29,3,23,3,23}  /* 2016 */
            , {25,28,2,22,2,22}  /* 2017 */
            , {24,27,1,28,1,28}  /* 2018 */
            , {30,26,7,27,7,27}  /* 2019 */
615
            , {0}
616 617 618 619 620 621 622 623 624 625 626 627
        };
        for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
            for (i=0; cases[i].nr; i++) {
                if(i) cases[i].nr += 18;
                cases[i].slt.wYear = year;
                cases[i].slt.wDay = yeardays[j][i/3];
                pSystemTimeToTzSpecificLocalTime( cases[i].ptz, &(cases[i].slt), &result);
                ok( result.wHour == cases[i].ehour,
                        "Test SystemTimeToTzSpecificLocalTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour = %02d\n", 
                        cases[i].nr, result.wYear, result.wMonth, result.wDay,
                        result.wHour, result.wMinute, cases[i].ehour);
            }
628 629 630 631 632
        }

    }        
}

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
static void test_FileTimeToDosDateTime(void)
{
    FILETIME ft = { 0 };
    WORD fatdate, fattime;
    BOOL ret;

    if (0)
    {
        /* Crashes */
        FileTimeToDosDateTime(NULL, NULL, NULL);
    }
    /* Parameter checking */
    SetLastError(0xdeadbeef);
    ret = FileTimeToDosDateTime(&ft, NULL, NULL);
    ok(!ret, "expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    SetLastError(0xdeadbeef);
    ret = FileTimeToDosDateTime(&ft, &fatdate, NULL);
    ok(!ret, "expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    SetLastError(0xdeadbeef);
    ret = FileTimeToDosDateTime(&ft, NULL, &fattime);
    ok(!ret, "expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    SetLastError(0xdeadbeef);
    ret = FileTimeToDosDateTime(&ft, &fatdate, &fattime);
    ok(!ret, "expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
}

670 671 672 673 674
static void test_GetCalendarInfo(void)
{
    char bufferA[20];
    WCHAR bufferW[20];
    DWORD val1, val2;
675
    int ret, ret2;
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

    if (!pGetCalendarInfoA || !pGetCalendarInfoW)
    {
        trace( "GetCalendarInfo missing\n" );
        return;
    }

    ret = pGetCalendarInfoA( 0x0409, CAL_GREGORIAN, CAL_ITWODIGITYEARMAX | CAL_RETURN_NUMBER,
                             NULL, 0, &val1 );
    ok( ret, "GetCalendarInfoA failed err %u\n", GetLastError() );
    ok( ret == sizeof(val1), "wrong size %u\n", ret );
    ok( val1 >= 2000 && val1 < 2100, "wrong value %u\n", val1 );

    ret = pGetCalendarInfoW( 0x0409, CAL_GREGORIAN, CAL_ITWODIGITYEARMAX | CAL_RETURN_NUMBER,
                             NULL, 0, &val2 );
    ok( ret, "GetCalendarInfoW failed err %u\n", GetLastError() );
    ok( ret == sizeof(val2)/sizeof(WCHAR), "wrong size %u\n", ret );
    ok( val1 == val2, "A/W mismatch %u/%u\n", val1, val2 );

    ret = pGetCalendarInfoA( 0x0409, CAL_GREGORIAN, CAL_ITWODIGITYEARMAX, bufferA, sizeof(bufferA), NULL );
    ok( ret, "GetCalendarInfoA failed err %u\n", GetLastError() );
    ok( ret == 5, "wrong size %u\n", ret );
    ok( atoi( bufferA ) == val1, "wrong value %s/%u\n", bufferA, val1 );

    ret = pGetCalendarInfoW( 0x0409, CAL_GREGORIAN, CAL_ITWODIGITYEARMAX, bufferW, sizeof(bufferW), NULL );
    ok( ret, "GetCalendarInfoW failed err %u\n", GetLastError() );
    ok( ret == 5, "wrong size %u\n", ret );
    memset( bufferA, 0x55, sizeof(bufferA) );
    WideCharToMultiByte( CP_ACP, 0, bufferW, -1, bufferA, sizeof(bufferA), NULL, NULL );
    ok( atoi( bufferA ) == val1, "wrong value %s/%u\n", bufferA, val1 );

    ret = pGetCalendarInfoA( 0x0409, CAL_GREGORIAN, CAL_ITWODIGITYEARMAX | CAL_RETURN_NUMBER,
                             NULL, 0, NULL );
    ok( !ret, "GetCalendarInfoA succeeded\n" );
    ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );

    ret = pGetCalendarInfoA( 0x0409, CAL_GREGORIAN, CAL_ITWODIGITYEARMAX, NULL, 0, NULL );
    ok( ret, "GetCalendarInfoA failed err %u\n", GetLastError() );
    ok( ret == 5, "wrong size %u\n", ret );

    ret = pGetCalendarInfoW( 0x0409, CAL_GREGORIAN, CAL_ITWODIGITYEARMAX | CAL_RETURN_NUMBER,
                             NULL, 0, NULL );
    ok( !ret, "GetCalendarInfoW succeeded\n" );
    ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );

    ret = pGetCalendarInfoW( 0x0409, CAL_GREGORIAN, CAL_ITWODIGITYEARMAX, NULL, 0, NULL );
    ok( ret, "GetCalendarInfoW failed err %u\n", GetLastError() );
    ok( ret == 5, "wrong size %u\n", ret );
724 725 726 727 728 729 730 731 732 733 734 735 736 737

    ret = pGetCalendarInfoA( LANG_SYSTEM_DEFAULT, CAL_GREGORIAN, CAL_SDAYNAME1,
                             bufferA, sizeof(bufferA), NULL);
    ok( ret, "GetCalendarInfoA failed err %u\n", GetLastError() );
    ret2 = pGetCalendarInfoA( LANG_SYSTEM_DEFAULT, CAL_GREGORIAN, CAL_SDAYNAME1,
                              bufferA, 0, NULL);
    ok( ret2, "GetCalendarInfoA failed err %u\n", GetLastError() );
    ok( ret == ret2, "got %d, expected %d\n", ret2, ret );

    ret2 = pGetCalendarInfoW( LANG_SYSTEM_DEFAULT, CAL_GREGORIAN, CAL_SDAYNAME1,
                              bufferW, sizeof(bufferW), NULL);
    ok( ret2, "GetCalendarInfoW failed err %u\n", GetLastError() );
    ret2 = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
    ok( ret == ret2, "got %d, expected %d\n", ret, ret2 );
738 739 740 741 742 743 744 745 746 747 748 749 750
}

static void test_GetDynamicTimeZoneInformation(void)
{
    DYNAMIC_TIME_ZONE_INFORMATION dyninfo;
    TIME_ZONE_INFORMATION tzinfo;
    DWORD ret, ret2;

    if (!pGetDynamicTimeZoneInformation)
    {
        win_skip("GetDynamicTimeZoneInformation() is not supported.\n");
        return;
    }
751

752 753 754 755 756 757 758 759 760 761 762 763 764 765
    ret = pGetDynamicTimeZoneInformation(&dyninfo);
    ret2 = GetTimeZoneInformation(&tzinfo);
    ok(ret == ret2, "got %d, %d\n", ret, ret2);

    ok(dyninfo.Bias == tzinfo.Bias, "got %d, %d\n", dyninfo.Bias, tzinfo.Bias);
    ok(!lstrcmpW(dyninfo.StandardName, tzinfo.StandardName), "got std name %s, %s\n",
        wine_dbgstr_w(dyninfo.StandardName), wine_dbgstr_w(tzinfo.StandardName));
    ok(!memcmp(&dyninfo.StandardDate, &tzinfo.StandardDate, sizeof(dyninfo.StandardDate)), "got different StandardDate\n");
    ok(dyninfo.StandardBias == tzinfo.StandardBias, "got %d, %d\n", dyninfo.StandardBias, tzinfo.StandardBias);
    ok(!lstrcmpW(dyninfo.DaylightName, tzinfo.DaylightName), "got daylight name %s, %s\n",
        wine_dbgstr_w(dyninfo.DaylightName), wine_dbgstr_w(tzinfo.DaylightName));
    ok(!memcmp(&dyninfo.DaylightDate, &tzinfo.DaylightDate, sizeof(dyninfo.DaylightDate)), "got different DaylightDate\n");
    ok(dyninfo.TimeZoneKeyName[0] != 0, "got empty tz keyname\n");
    trace("Dyn TimeZoneKeyName %s\n", wine_dbgstr_w(dyninfo.TimeZoneKeyName));
766 767
}

768 769 770
static ULONGLONG get_longlong_time(FILETIME *time)
{
    ULARGE_INTEGER uli;
771 772
    uli.u.LowPart = time->dwLowDateTime;
    uli.u.HighPart = time->dwHighDateTime;
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    return uli.QuadPart;
}

static void test_GetSystemTimePreciseAsFileTime(void)
{
    FILETIME ft;
    ULONGLONG time1, time2;
    LONGLONG diff;

    if (!pGetSystemTimePreciseAsFileTime)
    {
        win_skip("GetSystemTimePreciseAsFileTime() is not supported.\n");
        return;
    }

    GetSystemTimeAsFileTime(&ft);
    time1 = get_longlong_time(&ft);
    pGetSystemTimePreciseAsFileTime(&ft);
    time2 = get_longlong_time(&ft);
    diff = time2 - time1;
    if (diff < 0)
        diff = -diff;
    ok(diff < 1000000, "Difference between GetSystemTimeAsFileTime and GetSystemTimePreciseAsFileTime more than 100 ms\n");

    pGetSystemTimePreciseAsFileTime(&ft);
    time1 = get_longlong_time(&ft);
    do {
        pGetSystemTimePreciseAsFileTime(&ft);
        time2 = get_longlong_time(&ft);
    } while (time2 == time1);
    diff = time2 - time1;
    ok(diff < 10000 && diff > 0, "GetSystemTimePreciseAsFileTime incremented by more than 1 ms\n");
}

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
static void test_GetSystemTimes(void)
{

    FILETIME idletime, kerneltime, usertime;
    int i;
    ULARGE_INTEGER ul1, ul2, ul3;
    SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi;
    SYSTEM_BASIC_INFORMATION sbi;
    ULONG ReturnLength;
    ULARGE_INTEGER total_usertime, total_kerneltime, total_idletime;

    if (!pGetSystemTimes)
    {
        win_skip("GetSystemTimes not available\n");
        return;
    }

824
    ok( pGetSystemTimes(NULL, NULL, NULL), "GetSystemTimes failed unexpectedly\n" );
825 826 827 828 829 830 831

    total_usertime.QuadPart = 0;
    total_kerneltime.QuadPart = 0;
    total_idletime.QuadPart = 0;
    memset( &idletime, 0x11, sizeof(idletime) );
    memset( &kerneltime, 0x11, sizeof(kerneltime) );
    memset( &usertime, 0x11, sizeof(usertime) );
832 833
    ok( pGetSystemTimes(&idletime, &kerneltime , &usertime),
        "GetSystemTimes failed unexpectedly\n" );
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

    ul1.LowPart = idletime.dwLowDateTime;
    ul1.HighPart = idletime.dwHighDateTime;
    ul2.LowPart = kerneltime.dwLowDateTime;
    ul2.HighPart = kerneltime.dwHighDateTime;
    ul3.LowPart = usertime.dwLowDateTime;
    ul3.HighPart = usertime.dwHighDateTime;

    ok( !NtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength),
                                  "NtQuerySystemInformation failed\n" );
    ok( sizeof(sbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength );

    /* Check if we have some return values */
    trace( "Number of Processors : %d\n", sbi.NumberOfProcessors );
    ok( sbi.NumberOfProcessors > 0, "Expected more than 0 processors, got %d\n",
        sbi.NumberOfProcessors );

    sppi = HeapAlloc( GetProcessHeap(), 0,
                      sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * sbi.NumberOfProcessors);

    ok( !NtQuerySystemInformation( SystemProcessorPerformanceInformation, sppi,
855 856
                                   sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * sbi.NumberOfProcessors,
                                   &ReturnLength),
857 858 859 860 861 862 863 864 865
                                   "NtQuerySystemInformation failed\n" );

    for (i = 0; i < sbi.NumberOfProcessors; i++)
    {
        total_usertime.QuadPart += sppi[i].UserTime.QuadPart;
        total_kerneltime.QuadPart += sppi[i].KernelTime.QuadPart;
        total_idletime.QuadPart += sppi[i].IdleTime.QuadPart;
    }

866 867 868
    ok( total_idletime.QuadPart - ul1.QuadPart < 10000000, "test idletime failed\n" );
    ok( total_kerneltime.QuadPart - ul2.QuadPart < 10000000, "test kerneltime failed\n" );
    ok( total_usertime.QuadPart - ul3.QuadPart < 10000000, "test usertime failed\n" );
869 870 871 872

    HeapFree(GetProcessHeap(), 0, sppi);
}

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
static WORD day_of_month(const SYSTEMTIME* systemtime, WORD year)
{
    SYSTEMTIME first_of_month = {0};
    FILETIME filetime;
    WORD result;

    if (systemtime->wYear != 0)
        return systemtime->wDay;

    first_of_month.wYear = year;
    first_of_month.wMonth = systemtime->wMonth;
    first_of_month.wDay = 1;

    /* round-trip conversion sets day of week field */
    SystemTimeToFileTime(&first_of_month, &filetime);
    FileTimeToSystemTime(&filetime, &first_of_month);

    result = 1 + ((systemtime->wDayOfWeek - first_of_month.wDayOfWeek + 7) % 7) +
        (7 * (systemtime->wDay - 1));

    if (systemtime->wDay == 5)
    {
        /* make sure this isn't in the next month */
        SYSTEMTIME result_date;

        result_date = first_of_month;
        result_date.wDay = result;

        SystemTimeToFileTime(&result_date, &filetime);
        FileTimeToSystemTime(&filetime, &result_date);

        if (result_date.wDay != result)
            result = 1 + ((systemtime->wDayOfWeek - first_of_month.wDayOfWeek + 7) % 7) +
                (7 * (4 - 1));
    }

    return result;
}

static void test_GetTimeZoneInformationForYear(void)
{
    BOOL ret;
    SYSTEMTIME systemtime;
916
    TIME_ZONE_INFORMATION local_tzinfo, tzinfo, tzinfo2;
917 918 919 920 921 922 923
    DYNAMIC_TIME_ZONE_INFORMATION dyn_tzinfo;
    static const WCHAR std_tzname[] = {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',0};
    static const WCHAR dlt_tzname[] = {'G','r','e','e','n','l','a','n','d',' ','D','a','y','l','i','g','h','t',' ','T','i','m','e',0};
    WORD std_day, dlt_day;

    if (!pGetTimeZoneInformationForYear || !pGetDynamicTimeZoneInformation)
    {
924
        win_skip("GetTimeZoneInformationForYear not available\n");
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
        return;
    }

    GetLocalTime(&systemtime);

    GetTimeZoneInformation(&local_tzinfo);

    ret = pGetTimeZoneInformationForYear(systemtime.wYear, NULL, &tzinfo);
    ok(ret == TRUE, "GetTimeZoneInformationForYear failed, err %u\n", GetLastError());
    ok(tzinfo.Bias == local_tzinfo.Bias, "Expected Bias %d, got %d\n", local_tzinfo.Bias, tzinfo.Bias);
    ok(!lstrcmpW(tzinfo.StandardName, local_tzinfo.StandardName),
        "Expected StandardName %s, got %s\n", wine_dbgstr_w(local_tzinfo.StandardName), wine_dbgstr_w(tzinfo.StandardName));
    ok(!memcmp(&tzinfo.StandardDate, &local_tzinfo.StandardDate, sizeof(SYSTEMTIME)), "StandardDate does not match\n");
    ok(tzinfo.StandardBias == local_tzinfo.StandardBias, "Expected StandardBias %d, got %d\n", local_tzinfo.StandardBias, tzinfo.StandardBias);
    ok(!lstrcmpW(tzinfo.DaylightName, local_tzinfo.DaylightName),
        "Expected DaylightName %s, got %s\n", wine_dbgstr_w(local_tzinfo.DaylightName), wine_dbgstr_w(tzinfo.DaylightName));
    ok(!memcmp(&tzinfo.DaylightDate, &local_tzinfo.DaylightDate, sizeof(SYSTEMTIME)), "DaylightDate does not match\n");
    ok(tzinfo.DaylightBias == local_tzinfo.DaylightBias, "Expected DaylightBias %d, got %d\n", local_tzinfo.DaylightBias, tzinfo.DaylightBias);

    pGetDynamicTimeZoneInformation(&dyn_tzinfo);

    ret = pGetTimeZoneInformationForYear(systemtime.wYear, &dyn_tzinfo, &tzinfo);
    ok(ret == TRUE, "GetTimeZoneInformationForYear failed, err %u\n", GetLastError());
    ok(tzinfo.Bias == local_tzinfo.Bias, "Expected Bias %d, got %d\n", local_tzinfo.Bias, tzinfo.Bias);
    ok(!lstrcmpW(tzinfo.StandardName, local_tzinfo.StandardName),
        "Expected StandardName %s, got %s\n", wine_dbgstr_w(local_tzinfo.StandardName), wine_dbgstr_w(tzinfo.StandardName));
    ok(!memcmp(&tzinfo.StandardDate, &local_tzinfo.StandardDate, sizeof(SYSTEMTIME)), "StandardDate does not match\n");
    ok(tzinfo.StandardBias == local_tzinfo.StandardBias, "Expected StandardBias %d, got %d\n", local_tzinfo.StandardBias, tzinfo.StandardBias);
    ok(!lstrcmpW(tzinfo.DaylightName, local_tzinfo.DaylightName),
        "Expected DaylightName %s, got %s\n", wine_dbgstr_w(local_tzinfo.DaylightName), wine_dbgstr_w(tzinfo.DaylightName));
    ok(!memcmp(&tzinfo.DaylightDate, &local_tzinfo.DaylightDate, sizeof(SYSTEMTIME)), "DaylightDate does not match\n");
    ok(tzinfo.DaylightBias == local_tzinfo.DaylightBias, "Expected DaylightBias %d, got %d\n", local_tzinfo.DaylightBias, tzinfo.DaylightBias);

    memset(&dyn_tzinfo, 0xaa, sizeof(dyn_tzinfo));
    lstrcpyW(dyn_tzinfo.TimeZoneKeyName, std_tzname);
    dyn_tzinfo.DynamicDaylightTimeDisabled = FALSE;

    ret = pGetTimeZoneInformationForYear(2015, &dyn_tzinfo, &tzinfo);
    ok(ret == TRUE, "GetTimeZoneInformationForYear failed, err %u\n", GetLastError());
    ok(tzinfo.Bias == 180, "Expected Bias 180, got %d\n", tzinfo.Bias);
    ok(tzinfo.StandardDate.wMonth == 10, "Expected standard month 10, got %d\n", tzinfo.StandardDate.wMonth);
    std_day = day_of_month(&tzinfo.StandardDate, 2015);
    ok(std_day == 24, "Expected standard day 24, got %d\n", std_day);
    ok(tzinfo.StandardBias == 0, "Expected StandardBias 0, got %d\n", tzinfo.StandardBias);
    ok(tzinfo.DaylightDate.wMonth == 3, "Expected daylight month 3, got %d\n", tzinfo.DaylightDate.wMonth);
    dlt_day = day_of_month(&tzinfo.DaylightDate, 2015);
    ok(dlt_day == 28, "Expected daylight day 28, got %d\n", dlt_day);
    ok(tzinfo.DaylightBias == -60, "Expected DaylightBias -60, got %d\n", tzinfo.DaylightBias);

974 975 976 977 978 979 980 981 982
    ret = pGetTimeZoneInformationForYear(2016, &dyn_tzinfo, &tzinfo2);
    ok(ret == TRUE, "GetTimeZoneInformationForYear failed, err %u\n", GetLastError());
    ok(!lstrcmpW(tzinfo.StandardName, tzinfo2.StandardName),
        "Got differing StandardName values %s and %s\n",
        wine_dbgstr_w(tzinfo.StandardName), wine_dbgstr_w(tzinfo2.StandardName));
    ok(!lstrcmpW(tzinfo.DaylightName, tzinfo2.DaylightName),
        "Got differing DaylightName values %s and %s\n",
        wine_dbgstr_w(tzinfo.DaylightName), wine_dbgstr_w(tzinfo2.DaylightName));

983 984 985 986 987 988 989 990 991 992
    memset(&dyn_tzinfo, 0xaa, sizeof(dyn_tzinfo));
    lstrcpyW(dyn_tzinfo.TimeZoneKeyName, dlt_tzname);

    SetLastError(0xdeadbeef);
    ret = pGetTimeZoneInformationForYear(2015, &dyn_tzinfo, &tzinfo);
    ok((ret == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND) ||
       broken(ret == TRUE) /* vista,7 */,
       "GetTimeZoneInformationForYear err %u\n", GetLastError());
}

993 994
START_TEST(time)
{
995
    HMODULE hKernel = GetModuleHandleA("kernel32");
996 997
    pTzSpecificLocalTimeToSystemTime = (void *)GetProcAddress(hKernel, "TzSpecificLocalTimeToSystemTime");
    pSystemTimeToTzSpecificLocalTime = (void *)GetProcAddress( hKernel, "SystemTimeToTzSpecificLocalTime");
998
    pGetSystemTimes = (void *)GetProcAddress( hKernel, "GetSystemTimes");
999 1000
    pGetCalendarInfoA = (void *)GetProcAddress(hKernel, "GetCalendarInfoA");
    pGetCalendarInfoW = (void *)GetProcAddress(hKernel, "GetCalendarInfoW");
1001
    pGetDynamicTimeZoneInformation = (void *)GetProcAddress(hKernel, "GetDynamicTimeZoneInformation");
1002
    pGetSystemTimePreciseAsFileTime = (void *)GetProcAddress(hKernel, "GetSystemTimePreciseAsFileTime");
1003
    pGetTimeZoneInformationForYear = (void *)GetProcAddress(hKernel, "GetTimeZoneInformationForYear");
1004

1005 1006
    test_conversions();
    test_invalid_arg();
1007 1008 1009
    test_GetTimeZoneInformation();
    test_FileTimeToSystemTime();
    test_FileTimeToLocalFileTime();
1010
    test_TzSpecificLocalTimeToSystemTime();
1011
    test_GetSystemTimes();
1012
    test_FileTimeToDosDateTime();
1013
    test_GetCalendarInfo();
1014
    test_GetDynamicTimeZoneInformation();
1015
    test_GetSystemTimePreciseAsFileTime();
1016
    test_GetTimeZoneInformationForYear();
1017
}