From 8d44e36b1af2b8a0953722bb0a96234dabe3a404 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sat, 19 Jul 2014 14:36:21 +0200 Subject: os/time: include time.h in os/time.h Include what you use, also in the positive direction. --- src/nvim/os/time.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/nvim/os/time.h b/src/nvim/os/time.h index 8c73fc0c3b..b21808307f 100644 --- a/src/nvim/os/time.h +++ b/src/nvim/os/time.h @@ -3,6 +3,7 @@ #include #include +#include #ifdef INCLUDE_GENERATED_DECLARATIONS # include "os/time.h.generated.h" -- cgit From 6610b002ef6068c4a7d4bdec3bd87d61fb934e71 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sat, 19 Jul 2014 14:33:55 +0200 Subject: os/time: make os_get_localtime more portable gettimeofday() doesn't exist on Windows, as reported by @equalsraf. It seems a call to time() would be sufficient here, as only the seconds since the UNIX epoch are needed. --- src/nvim/os/time.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/nvim/os/time.c b/src/nvim/os/time.c index 00ffccfaa8..813cb073cf 100644 --- a/src/nvim/os/time.c +++ b/src/nvim/os/time.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include @@ -105,10 +105,6 @@ return result; /// argument) or NULL in case of error struct tm *os_get_localtime(struct tm *result) { - struct timeval tv; - if (gettimeofday(&tv, NULL) < 0) { - return NULL; - } - - return os_localtime_r(&tv.tv_sec, result); + time_t rawtime = time(NULL); + return os_localtime_r(&rawtime, result); } -- cgit From 636ddd075c8fdfffcac22281120baed9f293b266 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sun, 20 Jul 2014 16:53:18 +0200 Subject: os/time: fix os_localtime_r on win + add func_attr - it makes no sense for these functions to take NULL pointers - if `localtime()` on Windows returns a NULL pointer, the old code would try to dereference it. --- src/nvim/os/time.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nvim/os/time.c b/src/nvim/os/time.c index 813cb073cf..e3b76ac833 100644 --- a/src/nvim/os/time.c +++ b/src/nvim/os/time.c @@ -84,7 +84,8 @@ static void microdelay(uint64_t microseconds) /// Portable version of POSIX localtime_r() /// /// @return NULL in case of error -struct tm *os_localtime_r(const time_t *clock, struct tm *result) +struct tm *os_localtime_r(const time_t *restrict clock, + struct tm *restrict result) FUNC_ATTR_NONNULL_ALL { #ifdef UNIX // POSIX provides localtime_r() as a thread-safe version of localtime(). @@ -93,8 +94,11 @@ struct tm *os_localtime_r(const time_t *clock, struct tm *result) // Windows version of localtime() is thread-safe. // See http://msdn.microsoft.com/en-us/library/bf12f0hc%28VS.80%29.aspx struct tm *local_time = localtime(clock); // NOLINT + if (!local_time) { + return NULL; + } *result = *local_time; -return result; + return result; #endif } @@ -103,7 +107,7 @@ return result; /// @param result Pointer to a 'struct tm' where the result should be placed /// @return A pointer to a 'struct tm' in the current time zone (the 'result' /// argument) or NULL in case of error -struct tm *os_get_localtime(struct tm *result) +struct tm *os_get_localtime(struct tm *result) FUNC_ATTR_NONNULL_ALL { time_t rawtime = time(NULL); return os_localtime_r(&rawtime, result); -- cgit