From 575d41481edd35b1e0515403ba3433c416370338 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 18 Mar 2019 17:41:13 -0400 Subject: [PATCH] unix,win: add uv_gettimeofday() PR-URL: https://github.com/libuv/libuv/pull/2221 Reviewed-By: Anna Henningsen Reviewed-By: Richard Lau Reviewed-By: Refael Ackermann Reviewed-By: Ben Noordhuis --- CMakeLists.txt | 1 + Makefile.am | 1 + docs/src/misc.rst | 7 +++++++ include/uv.h | 2 ++ src/unix/core.c | 15 +++++++++++++++ src/win/util.c | 17 +++++++++++++++++ test/test-gettimeofday.c | 39 +++++++++++++++++++++++++++++++++++++++ test/test-list.h | 3 +++ test/test.gyp | 1 + 9 files changed, 86 insertions(+) create mode 100644 test/test-gettimeofday.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 01a7c47e..0ecbad50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,6 +62,7 @@ set(uv_test_sources test/test-getnameinfo.c test/test-getsockname.c test/test-getters-setters.c + test/test-gettimeofday.c test/test-handle-fileno.c test/test-homedir.c test/test-hrtime.c diff --git a/Makefile.am b/Makefile.am index 595a5aea..e8d30190 100644 --- a/Makefile.am +++ b/Makefile.am @@ -201,6 +201,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \ test/test-gethostname.c \ test/test-getnameinfo.c \ test/test-getsockname.c \ + test/test-gettimeofday.c \ test/test-handle-fileno.c \ test/test-homedir.c \ test/test-hrtime.c \ diff --git a/docs/src/misc.rst b/docs/src/misc.rst index 14e9acce..ee819b19 100644 --- a/docs/src/misc.rst +++ b/docs/src/misc.rst @@ -578,3 +578,10 @@ API zero on success, and a non-zero error value otherwise. .. versionadded:: 1.25.0 + +.. c:function:: int uv_gettimeofday(uv_timeval_t* tv) + + Cross-platform implementation of :man:`gettimeofday(2)`. The timezone + argument to `gettimeofday()` is not supported, as it is considered obsolete. + + .. versionadded:: 1.28.0 diff --git a/include/uv.h b/include/uv.h index 43895ac8..5bd0e8b0 100644 --- a/include/uv.h +++ b/include/uv.h @@ -1586,6 +1586,8 @@ UV_EXTERN void uv_key_delete(uv_key_t* key); UV_EXTERN void* uv_key_get(uv_key_t* key); UV_EXTERN void uv_key_set(uv_key_t* key, void* value); +UV_EXTERN int uv_gettimeofday(uv_timeval_t* tv); + typedef void (*uv_thread_cb)(void* arg); UV_EXTERN int uv_thread_create(uv_thread_t* tid, uv_thread_cb entry, void* arg); diff --git a/src/unix/core.c b/src/unix/core.c index ca0e345d..96b92e43 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -41,6 +41,7 @@ #include /* getrusage */ #include #include +#include #ifdef __sun # include @@ -1429,3 +1430,17 @@ int uv__getsockpeername(const uv_handle_t* handle, *namelen = (int) socklen; return 0; } + +int uv_gettimeofday(uv_timeval_t* tv) { + struct timeval time; + + if (tv == NULL) + return UV_EINVAL; + + if (gettimeofday(&time, NULL) != 0) + return UV__ERR(errno); + + tv->tv_sec = time.tv_sec; + tv->tv_usec = time.tv_usec; + return 0; +} diff --git a/src/win/util.c b/src/win/util.c index 395d958c..129d4b95 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -1778,3 +1778,20 @@ error: buffer->machine[0] = '\0'; return r; } + +int uv_gettimeofday(uv_timeval_t* tv) { + /* Based on https://doxygen.postgresql.org/gettimeofday_8c_source.html */ + const uint64_t epoch = (uint64_t) 116444736000000000ULL; + FILETIME file_time; + ULARGE_INTEGER ularge; + + if (tv == NULL) + return UV_EINVAL; + + GetSystemTimeAsFileTime(&file_time); + ularge.LowPart = file_time.dwLowDateTime; + ularge.HighPart = file_time.dwHighDateTime; + tv->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L); + tv->tv_usec = (long) (((ularge.QuadPart - epoch) % 10000000L) / 10); + return 0; +} diff --git a/test/test-gettimeofday.c b/test/test-gettimeofday.c new file mode 100644 index 00000000..af8736a5 --- /dev/null +++ b/test/test-gettimeofday.c @@ -0,0 +1,39 @@ +/* Copyright libuv project contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "task.h" + +TEST_IMPL(gettimeofday) { + uv_timeval_t tv; + int r; + + tv.tv_sec = 0; + r = uv_gettimeofday(&tv); + ASSERT(r == 0); + ASSERT(tv.tv_sec != 0); + + /* Test invalid input. */ + r = uv_gettimeofday(NULL); + ASSERT(r == UV_EINVAL); + + return 0; +} diff --git a/test/test-list.h b/test/test-list.h index f498c7dc..de820d48 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -252,6 +252,7 @@ TEST_DECLARE (getnameinfo_basic_ip4_sync) TEST_DECLARE (getnameinfo_basic_ip6) TEST_DECLARE (getsockname_tcp) TEST_DECLARE (getsockname_udp) +TEST_DECLARE (gettimeofday) TEST_DECLARE (fail_always) TEST_DECLARE (pass_always) TEST_DECLARE (socket_buffer_size) @@ -781,6 +782,8 @@ TASK_LIST_START TEST_ENTRY (getsockname_tcp) TEST_ENTRY (getsockname_udp) + TEST_ENTRY (gettimeofday) + TEST_ENTRY (poll_duplex) TEST_ENTRY (poll_unidirectional) TEST_ENTRY (poll_close) diff --git a/test/test.gyp b/test/test.gyp index 9c13e25d..8d886a35 100644 --- a/test/test.gyp +++ b/test/test.gyp @@ -43,6 +43,7 @@ 'test-gethostname.c', 'test-getnameinfo.c', 'test-getsockname.c', + 'test-gettimeofday.c', 'test-handle-fileno.c', 'test-homedir.c', 'test-hrtime.c',