[wpiutil] Add GetSystemTime() (#3840)

This portably gets the time in microseconds since the Unix epoch.
This commit is contained in:
Peter Johnson
2021-12-28 01:06:31 -06:00
committed by GitHub
parent 1e82595ffb
commit 5999a26fba
4 changed files with 41 additions and 3 deletions

View File

@@ -62,6 +62,8 @@ public final class WPIUtilJNI {
public static native long now();
public static native long getSystemTime();
public static native void addPortForwarder(int port, String remoteHost, int remotePort);
public static native void removePortForwarder(int port);

View File

@@ -116,6 +116,18 @@ Java_edu_wpi_first_util_WPIUtilJNI_now
}
}
/*
* Class: edu_wpi_first_util_WPIUtilJNI
* Method: getSystemTime
* Signature: ()J
*/
JNIEXPORT jlong JNICALL
Java_edu_wpi_first_util_WPIUtilJNI_getSystemTime
(JNIEnv*, jclass)
{
return wpi::GetSystemTime();
}
/*
* Class: edu_wpi_first_util_WPIUtilJNI
* Method: addPortForwarder

View File

@@ -16,7 +16,7 @@
#endif
// offset in microseconds
static uint64_t zerotime() noexcept {
static uint64_t time_since_epoch() noexcept {
#ifdef _WIN32
FILETIME ft;
uint64_t tmpres = 0;
@@ -35,7 +35,7 @@ static uint64_t zerotime() noexcept {
#else
// 1-us intervals
return std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
std::chrono::system_clock::now().time_since_epoch())
.count();
#endif
}
@@ -66,7 +66,7 @@ static uint64_t update_frequency() {
}
#endif
static const uint64_t zerotime_val = zerotime();
static const uint64_t zerotime_val = time_since_epoch();
static const uint64_t offset_val = timestamp();
#ifdef _WIN32
static const uint64_t frequency_val = update_frequency();
@@ -96,6 +96,10 @@ uint64_t wpi::Now() {
return (now_impl.load())();
}
uint64_t wpi::GetSystemTime() {
return time_since_epoch();
}
extern "C" {
uint64_t WPI_NowDefault(void) {
@@ -110,4 +114,8 @@ uint64_t WPI_Now(void) {
return wpi::Now();
}
uint64_t WPI_GetSystemTime(void) {
return wpi::GetSystemTime();
}
} // extern "C"

View File

@@ -33,6 +33,14 @@ void WPI_SetNowImpl(uint64_t (*func)(void));
*/
uint64_t WPI_Now(void);
/**
* Return the current system time in microseconds since the Unix epoch
* (January 1st, 1970 00:00 UTC).
*
* @return Time in microseconds.
*/
uint64_t WPI_GetSystemTime(void);
#ifdef __cplusplus
} // extern "C"
#endif
@@ -62,6 +70,14 @@ void SetNowImpl(uint64_t (*func)());
*/
uint64_t Now(void);
/**
* Return the current system time in microseconds since the Unix epoch
* (January 1st, 1970 00:00 UTC).
*
* @return Time in microseconds.
*/
uint64_t GetSystemTime();
} // namespace wpi
#endif