Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Kernel32#SetSystemTime (fixes #345) #357

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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