Skip to content

Commit e45be8b

Browse files
committed
Return 64-bit unsigned integer from FILETIME
1 parent e4e0968 commit e45be8b

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ Features
1717
* [#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).
1818
* [#535](https://github.com/java-native-access/jna/pull/535): Added `GetDesktopWindow` to `com.sun.jna.platform.win32.User32` - [@mlfreeman2](https://github.com/mlfreeman2).
1919
* [#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).
20-
* [#547](https://github.com/java-native-access/jna/pull/547): Added `GetSystemTimes` to `com.sun.jna.platform.win32.Kernel32` - [@dbwiddis](https://github.com/dbwiddis).
2120
* [#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).
21+
* [#547](https://github.com/java-native-access/jna/pull/547): Added `GetSystemTimes` to `com.sun.jna.platform.win32.Kernel32` - [@dbwiddis](https://github.com/dbwiddis).
22+
* [#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).
2223

2324
Bug Fixes
2425
---------

contrib/platform/src/com/sun/jna/platform/win32/WinBase.java

+34
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.sun.jna.Pointer;
2424
import com.sun.jna.Structure;
2525
import com.sun.jna.Union;
26+
import com.sun.jna.platform.win32.WinDef.DWORDLONG;
2627
import com.sun.jna.platform.win32.WinNT.HANDLE;
2728
import com.sun.jna.ptr.ByteByReference;
2829
import com.sun.jna.win32.StdCallLibrary;
@@ -264,13 +265,46 @@ public static long dateToFileTime(final Date date) {
264265
return ms_since_16010101 * 1000 * 10;
265266
}
266267

268+
/**
269+
* <p>Converts this filetime into a {@link Date}</p>
270+
* @return The {@link Date} represented by this filetime.
271+
*/
267272
public Date toDate() {
268273
return filetimeToDate(dwHighDateTime, dwLowDateTime);
269274
}
270275

276+
/**
277+
* <p>Converts this filetime into a number of milliseconds which have
278+
* passed since January 1, 1970 (UTC).</p>
279+
* @return This filetime as a number of milliseconds which have passed
280+
* since January 1, 1970 (UTC)
281+
*/
282+
public long toTime() {
283+
return toDate().getTime();
284+
}
285+
286+
/**
287+
* <p>Converts this filetime into a number of milliseconds which have
288+
* passed since January 1, 1970 (UTC).</p>
289+
* @return This filetime as a number of milliseconds which have passed
290+
* since January 1, 1970 (UTC)
291+
* @deprecated Replaced by {@link #toTime()}
292+
*/
293+
@Deprecated
271294
public long toLong() {
272295
return toDate().getTime();
273296
}
297+
298+
/**
299+
* <p>Converts the two 32-bit unsigned integer parts of this filetime
300+
* into a 64-bit unsigned integer representing the number of
301+
* 100-nanosecond intervals since January 1, 1601 (UTC).</p>
302+
* @return This filetime as a 64-bit unsigned integer number of
303+
* 100-nanosecond intervals since January 1, 1601 (UTC).
304+
*/
305+
public DWORDLONG toDWordLong() {
306+
return new DWORDLONG((long) dwHighDateTime << 32 | dwLowDateTime & 0xffffffffL);
307+
}
274308

275309
@Override
276310
public String toString() {

contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.sun.jna.Platform;
3535
import com.sun.jna.Pointer;
3636
import com.sun.jna.platform.win32.BaseTSD.SIZE_T;
37+
import com.sun.jna.platform.win32.WinBase.FILETIME;
3738
import com.sun.jna.platform.win32.WinBase.MEMORYSTATUSEX;
3839
import com.sun.jna.platform.win32.WinBase.SYSTEM_INFO;
3940
import com.sun.jna.platform.win32.WinDef.DWORD;
@@ -387,14 +388,14 @@ public void testGetSystemInfo() {
387388

388389
public void testGetSystemTimes() {
389390
Kernel32 kernel = Kernel32.INSTANCE;
390-
WinBase.FILETIME lpIdleTime = new WinBase.FILETIME();
391-
WinBase.FILETIME lpKernelTime = new WinBase.FILETIME();
392-
WinBase.FILETIME lpUserTime = new WinBase.FILETIME();
391+
FILETIME lpIdleTime = new FILETIME();
392+
FILETIME lpKernelTime = new FILETIME();
393+
FILETIME lpUserTime = new FILETIME();
393394
boolean succ = kernel.GetSystemTimes(lpIdleTime, lpKernelTime, lpUserTime);
394395
assertTrue(succ);
395-
long idleTime = WinBase.FILETIME.dateToFileTime(lpIdleTime.toDate());
396-
long kernelTime = WinBase.FILETIME.dateToFileTime(lpKernelTime.toDate());
397-
long userTime = WinBase.FILETIME.dateToFileTime(lpUserTime.toDate());
396+
long idleTime = lpIdleTime.toDWordLong().longValue();
397+
long kernelTime = lpKernelTime.toDWordLong().longValue();
398+
long userTime = lpUserTime.toDWordLong().longValue();
398399
// All should be >= 0. kernel includes idle.
399400
assertTrue(idleTime >= 0);
400401
assertTrue(kernelTime >= idleTime);

contrib/platform/test/com/sun/jna/platform/win32/WinBaseTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
package com.sun.jna.platform.win32;
1414

1515
import java.util.Calendar;
16+
import java.util.Date;
1617

1718
import com.sun.jna.platform.win32.WinBase.DCB;
19+
import com.sun.jna.platform.win32.WinBase.FILETIME;
1820
import com.sun.jna.platform.win32.WinBase.SYSTEMTIME;
1921

2022
import junit.framework.TestCase;
@@ -28,6 +30,16 @@ public WinBaseTest(String name) {
2830
super(name);
2931
}
3032

33+
public void testFiletime() {
34+
// subtract to convert ms after 1/1/1970 to ms after 1/1/1601
35+
long epochDiff = 11644473600000L;
36+
// Construct filetimes for ms after 1/1/1601, check for 100-ns after
37+
assertEquals("Mismatched filetime for 2ms", (new FILETIME(new Date(2L - epochDiff))).toDWordLong().longValue(), 2L * 10000);
38+
assertEquals("Mismatched filetime for 2^31ms", (new FILETIME(new Date((1L << 31) - epochDiff))).toDWordLong().longValue(), (1L << 31) * 10000);
39+
assertEquals("Mismatched filetime for 2^32ms", (new FILETIME(new Date((1L << 32) - epochDiff))).toDWordLong().longValue(), (1L << 32) * 10000);
40+
assertEquals("Mismatched filetime for 2^60ms", (new FILETIME(new Date((1L << 60) - epochDiff))).toDWordLong().longValue(), (1L << 60) * 10000);
41+
}
42+
3143
public void testCalendarToSystemTimeConversion() {
3244
Calendar expected = Calendar.getInstance();
3345
SYSTEMTIME sysTime = new SYSTEMTIME();

0 commit comments

Comments
 (0)