robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. Keyesfeat(input)!: CmdAtom event #4129764a301184e

master
6.6 KiB216 linesraw
1#include <inttypes.h>
2#include <limits.h>
3#include <stdbool.h>
4#include <string.h>
5#include <time.h>
6
7#include <uv.h>
8
9#include "auto/config.h"
10#include "nvim/event/loop.h"
11#include "nvim/event/multiqueue.h"
12#include "nvim/gettext_defs.h"
13#include "nvim/globals.h"
14#include "nvim/log.h"
15#include "nvim/main.h"
16#include "nvim/memory.h"
17#include "nvim/os/input.h"
18#include "nvim/os/os.h"
19#include "nvim/os/time.h"
20
21#include "os/time.c.generated.h"
22
23/// Gets a high-resolution (nanosecond), monotonically-increasing time relative
24/// to an arbitrary time in the past.
25///
26/// Not related to the time of day and therefore not subject to clock drift.
27///
28/// @return Relative time value with nanosecond precision.
29uint64_t os_hrtime(void)
30  FUNC_ATTR_WARN_UNUSED_RESULT
31{
32  return uv_hrtime();
33}
34
35/// Obtain the current system time from a high-resolution
36/// real-time clock source.
37///
38/// The real-time clock counts from the UNIX epoch (1970-01-01)
39/// and is subject to time adjustments; it can jump back in time.
40///
41/// @return Nanoseconds since epoch or 0
42int64_t os_realtime(void)
43  FUNC_ATTR_WARN_UNUSED_RESULT
44{
45  uv_timespec64_t ts = { 0 };
46  int error_number;
47  if ((error_number = uv_clock_gettime(UV_CLOCK_REALTIME, &ts)) != 0) {
48    ELOG("uv_clock_gettime failed: %d %s", error_number, uv_err_name(error_number));
49    return 0;
50  }
51  return ts.tv_sec * NS_PER_SEC + ts.tv_nsec;
52}
53
54/// Gets a millisecond-resolution, monotonically-increasing time relative to an
55/// arbitrary time in the past.
56///
57/// Not related to the time of day and therefore not subject to clock drift.
58/// The value is cached by the loop, it will not change until the next
59/// loop-tick (unless uv_update_time is called).
60///
61/// @return Relative time value with millisecond precision.
62uint64_t os_now(void)
63  FUNC_ATTR_WARN_UNUSED_RESULT
64{
65  return uv_now(&main_loop.uv);
66}
67
68/// Sleeps for `ms` milliseconds.
69///
70/// @see uv_sleep() (libuv v1.34.0)
71///
72/// @param ms          Number of milliseconds to sleep
73/// @param ignoreinput If true, only SIGINT (CTRL-C) can interrupt.
74void os_delay(uint64_t ms, bool ignoreinput)
75{
76  DLOG("%" PRIu64 " ms", ms);
77  if (ms > INT_MAX) {
78    ms = INT_MAX;
79  }
80  LOOP_PROCESS_EVENTS_UNTIL(&main_loop, NULL, (int)ms,
81                            ignoreinput ? got_int : os_input_ready(NULL));
82}
83
84/// Sleeps for `ms` milliseconds without checking for events or interrupts.
85///
86/// This blocks even "fast" events which is quite disruptive. This should only
87/// be used in debug code. Prefer os_delay() and decide if the delay should be
88/// interrupted by input or only a CTRL-C.
89///
90/// @see uv_sleep() (libuv v1.34.0)
91///
92/// @param us          Number of microseconds to sleep.
93void os_sleep(uint64_t ms)
94{
95  if (ms > UINT_MAX) {
96    ms = UINT_MAX;
97  }
98  uv_sleep((unsigned)ms);
99}
100
101// Cache of the current timezone name as retrieved from TZ, or an empty string
102// where unset, up to 64 octets long including trailing null byte.
103static char tz_cache[64];
104
105/// Portable version of POSIX localtime_r()
106///
107/// @return NULL in case of error
108struct tm *os_localtime_r(const time_t *restrict clock,
109                          struct tm *restrict result) FUNC_ATTR_NONNULL_ALL
110{
111#ifdef UNIX
112  // POSIX provides localtime_r() as a thread-safe version of localtime().
113  //
114  // Check to see if the environment variable TZ has changed since the last run.
115  // Call tzset(3) to update the global timezone variables if it has.
116  // POSIX standard doesn't require localtime_r() implementations to do that
117  // as it does with localtime(), and we don't want to call tzset() every time.
118  const char *tz = os_getenv_noalloc("TZ");
119  if (!tz) {
120    tz = "";
121  }
122  if (strncmp(tz_cache, tz, sizeof(tz_cache) - 1) != 0) {
123    tzset();
124    xstrlcpy(tz_cache, tz, sizeof(tz_cache));
125  }
126  return localtime_r(clock, result);  // NOLINT(runtime/threadsafe_fn)
127#else
128  // Windows version of localtime() is thread-safe.
129  // See http://msdn.microsoft.com/en-us/library/bf12f0hc%28VS.80%29.aspx
130  struct tm *local_time = localtime(clock);  // NOLINT(runtime/threadsafe_fn)
131  if (!local_time) {
132    return NULL;
133  }
134  *result = *local_time;
135  return result;
136#endif
137}
138
139/// Gets the current Unix timestamp and adjusts it to local time.
140///
141/// @param result Pointer to a 'struct tm' where the result should be placed
142/// @return A pointer to a 'struct tm' in the current time zone (the 'result'
143///         argument) or NULL in case of error
144struct tm *os_localtime(struct tm *result) FUNC_ATTR_NONNULL_ALL
145{
146  time_t rawtime = time(NULL);
147  return os_localtime_r(&rawtime, result);
148}
149
150/// Portable version of POSIX ctime_r()
151///
152/// @param clock[in]
153/// @param result[out] Pointer to a 'char' where the result should be placed
154/// @param result_len length of result buffer
155/// @return human-readable string of current local time
156char *os_ctime_r(const time_t *restrict clock, char *restrict result, size_t result_len,
157                 bool add_newline)
158  FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
159{
160  struct tm clock_local;
161  struct tm *clock_local_ptr = os_localtime_r(clock, &clock_local);
162  // MSVC returns NULL for an invalid value of seconds.
163  if (clock_local_ptr == NULL) {
164    xstrlcpy(result, _("(Invalid)"), result_len - 1);
165  } else {
166    // xgettext:no-c-format
167    if (strftime(result, result_len - 1, _("%a %b %d %H:%M:%S %Y"), clock_local_ptr) == 0) {
168      // Quoting "man strftime":
169      // > If the length of the result string (including the terminating
170      // > null byte) would exceed max bytes, then strftime() returns 0,
171      // > and the contents of the array are undefined.
172      xstrlcpy(result, _("(Invalid)"), result_len - 1);
173    }
174  }
175  if (add_newline) {
176    xstrlcat(result, "\n", result_len);
177  }
178  return result;
179}
180
181/// Gets the current Unix timestamp and adjusts it to local time.
182///
183/// @param result[out] Pointer to a 'char' where the result should be placed
184/// @param result_len length of result buffer
185/// @return human-readable string of current local time
186char *os_ctime(char *result, size_t result_len, bool add_newline)
187  FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
188{
189  time_t rawtime = time(NULL);
190  return os_ctime_r(&rawtime, result, result_len, add_newline);
191}
192
193/// Portable version of POSIX strptime()
194///
195/// @param str[in]  string to convert
196/// @param format[in]  format to parse "str"
197/// @param tm[out]  time representation of "str"
198/// @return Pointer to first unprocessed character or NULL
199char *os_strptime(const char *str, const char *format, struct tm *tm)
200  FUNC_ATTR_NONNULL_ALL
201{
202#ifdef HAVE_STRPTIME
203  return strptime(str, format, tm);
204#else
205  return NULL;
206#endif
207}
208
209/// Obtains the current Unix timestamp.
210///
211/// @return Seconds since epoch.
212Timestamp os_time(void)
213  FUNC_ATTR_WARN_UNUSED_RESULT
214{
215  return (Timestamp)time(NULL);
216}