From efd4ab5b38ac9e70a602c0801f36d68987c6d004 Mon Sep 17 00:00:00 2001 From: Lyor Goldstein Date: Thu, 7 Aug 2014 09:46:06 +0300 Subject: [PATCH] Added Kernel32#SetSystemTime --- .../com/sun/jna/platform/win32/Kernel32.java | 14 ++++++++ .../sun/jna/platform/win32/Kernel32Test.java | 34 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java index b3b1604ea7..06a668038e 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java @@ -159,6 +159,20 @@ boolean ReadFile(HANDLE hFile, Buffer lpBuffer, int nNumberOfBytesToRead, */ void GetSystemTime(WinBase.SYSTEMTIME lpSystemTime); + /** + * The SetSystemTime function modifies the current system date and time. + * The system time is expressed in Coordinated Universal Time (UTC). + * + * @param lpSystemTime + * Pointer to a SYSTEMTIME structure holding the new + * system date and time. Note: The {@code wDayOfWeek} + * member of the SYSTEMTIME structure is ignored. + * @return {@code true} if the function succeeds, {@code false} otherwise. + * If the function fails, call GetLastError to get extended error + * information. + */ + boolean SetSystemTime(WinBase.SYSTEMTIME lpSystemTime); + /** * Retrieves the current local date and time. * diff --git a/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java b/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java index 699795ca84..840f970152 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java @@ -78,6 +78,40 @@ public void testStructureOutArgument() { cal.get(Calendar.YEAR), time.wYear); } + public void testSetSystemTime() { + Kernel32 kernel = Kernel32.INSTANCE; + WinBase.SYSTEMTIME time = new WinBase.SYSTEMTIME(); + kernel.GetSystemTime(time); + try { + WinBase.SYSTEMTIME expected = new WinBase.SYSTEMTIME(); + expected.wYear = time.wYear; + expected.wMonth = time.wMonth; + expected.wDay = time.wDay; + expected.wHour = time.wHour; + expected.wMinute = time.wMinute; + expected.wSecond = time.wSecond; + expected.wMilliseconds = time.wMilliseconds; + + if (expected.wHour > 0) { + expected.wHour--; + } else { + expected.wHour++; + } + + if (!kernel.SetSystemTime(expected)) { + fail("Failed to modify time: error=" + kernel.GetLastError()); + } + + WinBase.SYSTEMTIME actual = new WinBase.SYSTEMTIME(); + kernel.GetSystemTime(actual); + assertEquals("Mismatched hour value", expected.wHour, actual.wHour); + } finally { + if (!kernel.SetSystemTime(time)) { + fail("Failed to restore original time: error=" + kernel.GetLastError()); + } + } + } + public void testGetLastError() { Kernel32 kernel = Kernel32.INSTANCE; int ERRCODE = 8;