aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os/time.c
diff options
context:
space:
mode:
authorJan Edmund Lazo <jan.lazo@mail.utoronto.ca>2020-03-20 05:50:16 -0400
committerJan Edmund Lazo <jan.lazo@mail.utoronto.ca>2020-03-29 09:41:17 -0400
commit573671b1fbcbc43b622391f50f4df30594f14751 (patch)
treea83617d57514e5d9f21f84179981fe945a181ddc /src/nvim/os/time.c
parente700a88bb6bdc96f4d62a384aecd3bec2040ac3b (diff)
downloadrneovim-573671b1fbcbc43b622391f50f4df30594f14751.tar.gz
rneovim-573671b1fbcbc43b622391f50f4df30594f14751.tar.bz2
rneovim-573671b1fbcbc43b622391f50f4df30594f14751.zip
vim-patch:8.1.1313: warnings for using localtime() and ctime()
Problem: Warnings for using localtime() and ctime(). Solution: Use localtime_r() if available. Avoid using ctime(). https://github.com/vim/vim/commit/63d2555c9cefbbeeca3ec87fdd5d241e9488f9dd
Diffstat (limited to 'src/nvim/os/time.c')
-rw-r--r--src/nvim/os/time.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/nvim/os/time.c b/src/nvim/os/time.c
index 4dd0614fe2..fc04cdd55b 100644
--- a/src/nvim/os/time.c
+++ b/src/nvim/os/time.c
@@ -144,6 +144,39 @@ struct tm *os_localtime(struct tm *result) FUNC_ATTR_NONNULL_ALL
return os_localtime_r(&rawtime, result);
}
+/// Portable version of POSIX ctime_r()
+///
+/// @param clock[in]
+/// @param result[out] Pointer to a 'char' where the result should be placed
+/// @param result_len length of result buffer
+/// @return human-readable string of current local time
+char *os_ctime_r(const time_t *restrict clock, char *restrict result,
+ size_t result_len)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
+{
+ struct tm clock_local;
+ struct tm *clock_local_ptr = os_localtime_r(clock, &clock_local);
+ // MSVC returns NULL for an invalid value of seconds.
+ if (clock_local_ptr == NULL) {
+ snprintf(result, result_len, "%s\n", _("(Invalid)"));
+ } else {
+ strftime(result, result_len, "%a %b %d %H:%M:%S %Y\n", clock_local_ptr);
+ }
+ return result;
+}
+
+/// Gets the current Unix timestamp and adjusts it to local time.
+///
+/// @param result[out] Pointer to a 'char' where the result should be placed
+/// @param result_len length of result buffer
+/// @return human-readable string of current local time
+char *os_ctime(char *result, size_t result_len)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
+{
+ time_t rawtime = time(NULL);
+ return os_ctime_r(&rawtime, result, result_len);
+}
+
/// Obtains the current Unix timestamp.
///
/// @return Seconds since epoch.