aboutsummaryrefslogtreecommitdiff
path: root/src/os
diff options
context:
space:
mode:
Diffstat (limited to 'src/os')
-rw-r--r--src/os/time.c25
-rw-r--r--src/os/time.h11
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