diff options
author | Felipe Oliveira Carvalho <felipekde@gmail.com> | 2014-05-01 20:24:29 -0300 |
---|---|---|
committer | Felipe Oliveira Carvalho <felipekde@gmail.com> | 2014-05-06 09:22:39 -0300 |
commit | 151382d533c2bd77e26e1dfc254e186f7496e226 (patch) | |
tree | c3841dddf47c5d75cdc1a11fe5e726c2f1c6bbf2 /src/os/time.c | |
parent | ee62510d4e623e5fb4a0cc7d5b450ce18c24e25f (diff) | |
download | rneovim-151382d533c2bd77e26e1dfc254e186f7496e226.tar.gz rneovim-151382d533c2bd77e26e1dfc254e186f7496e226.tar.bz2 rneovim-151382d533c2bd77e26e1dfc254e186f7496e226.zip |
Introduce os_localtime_r() and os_get_local_time()
Replace localtime() with os_localtime_r() in `eval.c` and `undo.c`.
Diffstat (limited to 'src/os/time.c')
-rw-r--r-- | src/os/time.c | 25 |
1 files changed, 25 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); +} |