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

Return 64-bit unsigned integer from FILETIME #548

Merged
merged 1 commit into from
Dec 2, 2015
Merged
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
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Features
* [#535](https://github.com/java-native-access/jna/pull/535): Added `SHEmptyRecycleBin`, `ShellExecuteEx` to `com.sun.jna.platform.win32.Shell32` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#535](https://github.com/java-native-access/jna/pull/535): Added `GetDesktopWindow` to `com.sun.jna.platform.win32.User32` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#543](https://github.com/java-native-access/jna/pull/543): Added `ProcessIdToSessionId`, `LoadLibraryEx`, `FreeLibrary` and `Find/Load/Lock/SizeofResource` to `com.sun.jna.platform.win32.Kernel32` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#547](https://github.com/java-native-access/jna/pull/547): Added `GetSystemTimes` to `com.sun.jna.platform.win32.Kernel32` - [@dbwiddis](https://github.com/dbwiddis).
* [#545](https://github.com/java-native-access/jna/pull/545): Added `EnumResourceTypes` and `EnumResourceNames` to `com.sun.jna.platform.win32.Kernel32` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#547](https://github.com/java-native-access/jna/pull/547): Added `GetSystemTimes` to `com.sun.jna.platform.win32.Kernel32` - [@dbwiddis](https://github.com/dbwiddis).
* [#548](https://github.com/java-native-access/jna/pull/548): Return 64-bit unsigned integer from `com.sun.jna.platform.win32.WinBase.FILETIME` - [@dbwiddis](https://github.com/dbwiddis).

Bug Fixes
---------
Expand Down
34 changes: 34 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/WinBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.Union;
import com.sun.jna.platform.win32.WinDef.DWORDLONG;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.win32.StdCallLibrary;
Expand Down Expand Up @@ -264,13 +265,46 @@ public static long dateToFileTime(final Date date) {
return ms_since_16010101 * 1000 * 10;
}

/**
* <p>Converts this filetime into a {@link Date}</p>
* @return The {@link Date} represented by this filetime.
*/
public Date toDate() {
return filetimeToDate(dwHighDateTime, dwLowDateTime);
}

/**
* <p>Converts this filetime into a number of milliseconds which have
* passed since January 1, 1970 (UTC).</p>
* @return This filetime as a number of milliseconds which have passed
* since January 1, 1970 (UTC)
*/
public long toTime() {
return toDate().getTime();
}

/**
* <p>Converts this filetime into a number of milliseconds which have
* passed since January 1, 1970 (UTC).</p>
* @return This filetime as a number of milliseconds which have passed
* since January 1, 1970 (UTC)
* @deprecated Replaced by {@link #toTime()}
*/
@Deprecated
public long toLong() {
return toDate().getTime();
}

/**
* <p>Converts the two 32-bit unsigned integer parts of this filetime
* into a 64-bit unsigned integer representing the number of
* 100-nanosecond intervals since January 1, 1601 (UTC).</p>
* @return This filetime as a 64-bit unsigned integer number of
* 100-nanosecond intervals since January 1, 1601 (UTC).
*/
public DWORDLONG toDWordLong() {
return new DWORDLONG((long) dwHighDateTime << 32 | dwLowDateTime & 0xffffffffL);
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.BaseTSD.SIZE_T;
import com.sun.jna.platform.win32.WinBase.FILETIME;
import com.sun.jna.platform.win32.WinBase.MEMORYSTATUSEX;
import com.sun.jna.platform.win32.WinBase.SYSTEM_INFO;
import com.sun.jna.platform.win32.WinDef.DWORD;
Expand Down Expand Up @@ -387,14 +388,14 @@ public void testGetSystemInfo() {

public void testGetSystemTimes() {
Kernel32 kernel = Kernel32.INSTANCE;
WinBase.FILETIME lpIdleTime = new WinBase.FILETIME();
WinBase.FILETIME lpKernelTime = new WinBase.FILETIME();
WinBase.FILETIME lpUserTime = new WinBase.FILETIME();
FILETIME lpIdleTime = new FILETIME();
FILETIME lpKernelTime = new FILETIME();
FILETIME lpUserTime = new FILETIME();
boolean succ = kernel.GetSystemTimes(lpIdleTime, lpKernelTime, lpUserTime);
assertTrue(succ);
long idleTime = WinBase.FILETIME.dateToFileTime(lpIdleTime.toDate());
long kernelTime = WinBase.FILETIME.dateToFileTime(lpKernelTime.toDate());
long userTime = WinBase.FILETIME.dateToFileTime(lpUserTime.toDate());
long idleTime = lpIdleTime.toDWordLong().longValue();
long kernelTime = lpKernelTime.toDWordLong().longValue();
long userTime = lpUserTime.toDWordLong().longValue();
// All should be >= 0. kernel includes idle.
assertTrue(idleTime >= 0);
assertTrue(kernelTime >= idleTime);
Expand Down
12 changes: 12 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/WinBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
package com.sun.jna.platform.win32;

import java.util.Calendar;
import java.util.Date;

import com.sun.jna.platform.win32.WinBase.DCB;
import com.sun.jna.platform.win32.WinBase.FILETIME;
import com.sun.jna.platform.win32.WinBase.SYSTEMTIME;

import junit.framework.TestCase;
Expand All @@ -28,6 +30,16 @@ public WinBaseTest(String name) {
super(name);
}

public void testFiletime() {
// subtract to convert ms after 1/1/1970 to ms after 1/1/1601
long epochDiff = 11644473600000L;
// Construct filetimes for ms after 1/1/1601, check for 100-ns after
assertEquals("Mismatched filetime for 2ms", (new FILETIME(new Date(2L - epochDiff))).toDWordLong().longValue(), 2L * 10000);
assertEquals("Mismatched filetime for 2^16ms", (new FILETIME(new Date((1L << 16) - epochDiff))).toDWordLong().longValue(), (1L << 16) * 10000);
assertEquals("Mismatched filetime for 2^32ms", (new FILETIME(new Date((1L << 32) - epochDiff))).toDWordLong().longValue(), (1L << 32) * 10000);
assertEquals("Mismatched filetime for 2^49ms", (new FILETIME(new Date((1L << 49) - epochDiff))).toDWordLong().longValue(), (1L << 49) * 10000);
}

public void testCalendarToSystemTimeConversion() {
Calendar expected = Calendar.getInstance();
SYSTEMTIME sysTime = new SYSTEMTIME();
Expand Down