robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
64a301184e
master
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 ) 30FUNC_ATTR_WARN_UNUSED_RESULT 31{ 32return 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 ) 43FUNC_ATTR_WARN_UNUSED_RESULT 44{ 45uv_timespec64_t ts = {0 }; 46int error_number ; 47if ((error_number = uv_clock_gettime (UV_CLOCK_REALTIME ,& ts ))!= 0 ) { 48ELOG ("uv_clock_gettime failed: %d %s" ,error_number ,uv_err_name (error_number )); 49return 0 ; 50 } 51return 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 ) 63FUNC_ATTR_WARN_UNUSED_RESULT 64{ 65return 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{ 76DLOG ("%" PRIu64 " ms" ,ms ); 77if (ms > INT_MAX ) { 78ms = INT_MAX ; 79 } 80LOOP_PROCESS_EVENTS_UNTIL (& main_loop ,NULL , (int )ms , 81ignoreinput ?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{ 95if (ms > UINT_MAX ) { 96ms = UINT_MAX ; 97 } 98uv_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 * restrictclock , 109struct tm * restrictresult )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. 118const char * tz = os_getenv_noalloc ("TZ" ); 119if (!tz ) { 120tz = "" ; 121 } 122if (strncmp (tz_cache ,tz ,sizeof (tz_cache )- 1 )!= 0 ) { 123tzset (); 124xstrlcpy (tz_cache ,tz ,sizeof (tz_cache )); 125 } 126return 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 130struct tm * local_time = localtime (clock );// NOLINT(runtime/threadsafe_fn) 131if (!local_time ) { 132return NULL ; 133 } 134* result = * local_time ; 135return 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{ 146time_t rawtime = time (NULL ); 147return 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 * restrictclock ,char * restrictresult ,size_t result_len , 157bool add_newline ) 158FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET 159{ 160struct tm clock_local ; 161struct tm * clock_local_ptr = os_localtime_r (clock ,& clock_local ); 162// MSVC returns NULL for an invalid value of seconds. 163if (clock_local_ptr == NULL ) { 164xstrlcpy (result ,_ ("(Invalid)" ),result_len - 1 ); 165 }else { 166// xgettext:no-c-format 167if (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. 172xstrlcpy (result ,_ ("(Invalid)" ),result_len - 1 ); 173 } 174 } 175if (add_newline ) { 176xstrlcat (result ,"\n" ,result_len ); 177 } 178return 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 ) 187FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET 188{ 189time_t rawtime = time (NULL ); 190return 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 ) 200FUNC_ATTR_NONNULL_ALL 201{ 202#ifdef HAVE_STRPTIME 203return strptime (str ,format ,tm ); 204#else 205return NULL ; 206#endif 207} 208 209/// Obtains the current Unix timestamp. 210/// 211/// @return Seconds since epoch. 212Timestamp os_time (void ) 213FUNC_ATTR_WARN_UNUSED_RESULT 214{ 215return (Timestamp )time (NULL ); 216}