Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2a57169

Browse files
authored
Add GetSystemTimeAsTicks to System.Native (#17484)
* Add GetSystemTimeAsTicks to System.Native * Use reinterpret_cast * Use clock_gettime(CLOCK_REALTIME) if possible * Fix names and include pal_config.h * Another missing header * Fix unused variable warning
1 parent d1c4eea commit 2a57169

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/Native/Unix/Common/pal_config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#cmakedefine01 HAVE_GETDOMAINNAME_SIZET
5555
#cmakedefine01 HAVE_INOTIFY
5656
#cmakedefine01 HAVE_CLOCK_MONOTONIC
57+
#cmakedefine01 HAVE_CLOCK_REALTIME
5758
#cmakedefine01 HAVE_MACH_ABSOLUTE_TIME
5859
#cmakedefine01 HAVE_MACH_TIMEBASE_INFO
5960
#cmakedefine01 HAVE_CURLM_ADDED_ALREADY

src/Native/Unix/System.Native/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(NATIVE_SOURCES
1818
pal_tcpstate.cpp
1919
pal_time.cpp
2020
pal_uid.cpp
21+
pal_datetime.cpp
2122
)
2223

2324
if (CMAKE_SYSTEM_NAME STREQUAL Linux)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#include "pal_config.h"
6+
7+
#include <stdlib.h>
8+
#include <stdint.h>
9+
#include <time.h>
10+
#include <sys/time.h>
11+
12+
static const int64_t TICKS_PER_SECOND = 10000000; /* 10^7 */
13+
#if HAVE_CLOCK_REALTIME
14+
static const int64_t NANOSECONDS_PER_TICK = 100;
15+
#else
16+
static const int64_t TICKS_PER_MICROSECOND = 10; /* 1000 / 100 */
17+
#endif
18+
19+
//
20+
// SystemNative_GetSystemTimeAsTicks return the system time as ticks (100 nanoseconds)
21+
// since 00:00 01 January 1970 UTC (Unix epoch)
22+
//
23+
extern "C" int64_t SystemNative_GetSystemTimeAsTicks()
24+
{
25+
#if HAVE_CLOCK_REALTIME
26+
struct timespec time;
27+
if (clock_gettime(CLOCK_REALTIME, &time) == 0)
28+
{
29+
return static_cast<int64_t>(time.tv_sec) * TICKS_PER_SECOND + (time.tv_nsec / NANOSECONDS_PER_TICK);
30+
}
31+
#else
32+
struct timeval time;
33+
if (gettimeofday(&time, NULL) == 0)
34+
{
35+
return static_cast<int64_t>(time.tv_sec) * TICKS_PER_SECOND + (time.tv_usec * TICKS_PER_MICROSECOND);
36+
}
37+
#endif
38+
// in failure we return 00:00 01 January 1970 UTC (Unix epoch)
39+
return 0;
40+
}

src/Native/Unix/configure.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,21 @@ check_cxx_source_runs(
339339
"
340340
HAVE_CLOCK_MONOTONIC)
341341

342+
check_cxx_source_runs(
343+
"
344+
#include <stdlib.h>
345+
#include <time.h>
346+
#include <sys/time.h>
347+
int main()
348+
{
349+
int ret;
350+
struct timespec ts;
351+
ret = clock_gettime(CLOCK_REALTIME, &ts);
352+
exit(ret);
353+
}
354+
"
355+
HAVE_CLOCK_REALTIME)
356+
342357
check_function_exists(
343358
mach_absolute_time
344359
HAVE_MACH_ABSOLUTE_TIME)

0 commit comments

Comments
 (0)