Skip to content

Commit

Permalink
Added Kernel32#SetSystemTime.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyor Goldstein authored and dblock committed Aug 8, 2014
1 parent 554d7a2 commit ef71874
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Features
* [#350](https://github.com/twall/jna/pull/350): Added `jnacontrib.x11.api.X.Window.getSubwindows` - [@rm5248](https://github.com/rm5248).
* Improved `contrib/msoffice` sample - [@wolftobias](https://github.com/wolftobias).
* [#352](https://github.com/twall/jna/pull/352): Performance improvements due to reduced locking in `com.sun.jna.Library$Handler` and fewer vararg checks in `com.sun.jna.Function` - [@Boereck](https://github.com/Boereck).
* [#357](https://github.com/twall/jna/pull/357): Added `com.sun.jna.platform.win32.Kernel32.SetSystemTime` - [@lgoldstein](https://github.com/lgoldstein), [@thomasjoulin](https://github.com/thomasjoulin).

Bug Fixes
---------
Expand Down
14 changes: 14 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. <B>Note:</B> 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.
*
Expand Down
34 changes: 34 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ef71874

Please sign in to comment.