Skip to content

Commit

Permalink
Return 64-bit unsigned integer from FILETIME
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Dec 1, 2015
1 parent e4e0968 commit e45be8b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
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^31ms", (new FILETIME(new Date((1L << 31) - epochDiff))).toDWordLong().longValue(), (1L << 31) * 10000);
assertEquals("Mismatched filetime for 2^32ms", (new FILETIME(new Date((1L << 32) - epochDiff))).toDWordLong().longValue(), (1L << 32) * 10000);
assertEquals("Mismatched filetime for 2^60ms", (new FILETIME(new Date((1L << 60) - epochDiff))).toDWordLong().longValue(), (1L << 60) * 10000);
}

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

0 comments on commit e45be8b

Please sign in to comment.