diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2014-05-06 21:50:25 -0700 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-05-06 21:50:25 -0700 |
commit | df4f88fe3198acea6267c6ed3b129af7d8972bf1 (patch) | |
tree | 12aa58e43bdd457f8fccd5fef49dc6831368f0f3 /src/os | |
parent | 85459327ba76d674572ad96dc459c97e4a71e88d (diff) | |
parent | 151382d533c2bd77e26e1dfc254e186f7496e226 (diff) | |
download | rneovim-df4f88fe3198acea6267c6ed3b129af7d8972bf1.tar.gz rneovim-df4f88fe3198acea6267c6ed3b129af7d8972bf1.tar.bz2 rneovim-df4f88fe3198acea6267c6ed3b129af7d8972bf1.zip |
Merge pull request #644 from philix/log
Macro-based log utility for Neovim
Diffstat (limited to 'src/os')
-rw-r--r-- | src/os/time.c | 25 | ||||
-rw-r--r-- | src/os/time.h | 11 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/os/time.c b/src/os/time.c index 0f7c990e23..1dc7ca68d4 100644 --- a/src/os/time.c +++ b/src/os/time.c @@ -1,5 +1,6 @@ #include <stdint.h> #include <stdbool.h> +#include <sys/time.h> #include <uv.h> @@ -59,3 +60,27 @@ static void microdelay(uint64_t microseconds) uv_mutex_unlock(&delay_mutex); } + +struct tm *os_localtime_r(const time_t *clock, struct tm *result) +{ +#ifdef UNIX + // POSIX provides localtime_r() as a thread-safe version of localtime(). + return localtime_r(clock, result); +#else + // 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 + *result = *local_time; +return result; +#endif +} + +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); +} diff --git a/src/os/time.h b/src/os/time.h index 42feaa3eba..ef795d03be 100644 --- a/src/os/time.h +++ b/src/os/time.h @@ -19,6 +19,17 @@ void os_delay(uint64_t milliseconds, bool ignoreinput); /// @param ignoreinput If true, allow a SIGINT to interrupt us void os_microdelay(uint64_t microseconds, bool ignoreinput); +/// Portable version of POSIX localtime_r() +/// +/// @return NULL in case of error +struct tm *os_localtime_r(const time_t *clock, struct tm *result); + +/// Obtains the current UNIX timestamp and adjusts it to local time +/// +/// @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); #endif // NEOVIM_OS_TIME_H |