48 #if defined( MBG_TGT_WIN32 ) 57 size_t n =
snprintf_safe( s, max_len,
"gmtime() call failed" );
60 n +=
snprintf_safe( &s[n], max_len - n,
" in %s", calling_fnc );
62 #if defined( MBG_TGT_MISSING_64_BIT_TYPES ) 64 n +=
snprintf_safe( &s[n], max_len - n,
" for time_t %lu: %s",
67 n +=
snprintf_safe( &s[n], max_len - n,
" for time_t %" PRIu64
": %s",
77 #if defined( MBG_TGT_WIN32 ) 101 if ( clock_id == CLOCK_REALTIME )
103 #if defined( TIME_UTC ) // C11 / VS2015+ 104 int rc = timespec_get( tp, TIME_UTC );
105 return ( rc == 0 ) ? -1 : 0;
108 unsigned __int64 tmp;
110 tmp = ( (__int64) ft.dwHighDateTime << 32 ) | ft.dwLowDateTime;
111 tmp -= FILETIME_1970;
128 if ( clock_id == CLOCK_REALTIME )
137 t.ull = FILETIME_1970 +
139 (ULONGLONG) tp->tv_nsec / 100;
141 if ( !FileTimeToSystemTime( &t.ft, &st ) )
144 if ( !SetSystemTime( &st ) )
156 bool force_legacy_gstaft;
162 const char *info =
"";
164 HINSTANCE h = LoadLibrary(
"kernel32.dll" );
168 info =
"Precise system time may not be supported; failed to get handle for kernel32.dll.";
172 tmp_fnc = (GSTAFT_FNC) GetProcAddress( h,
"GetSystemTimePreciseAsFileTime" );
174 if ( tmp_fnc == NULL )
176 info =
"Precise system time NOT supported";
180 if ( force_legacy_gstaft )
182 info =
"Precise system time is supported, but legacy function used by request";
186 gstaft_fnc = tmp_fnc;
187 info =
"Precise system time is supported and used";
190 printf(
"%s\n", info );
216 char utc_offs_sign = (char) ( ( utc_offs < 0 ) ?
'-' :
'+' );
217 ulong abs_utc_offs = labs( utc_offs );
226 n +=
snprintf_safe( &s[n], max_len - n,
"%c%lu", utc_offs_sign, utc_offs_hours );
228 if ( utc_offs_mins || utc_offs_secs )
229 n +=
snprintf_safe( &s[n], max_len - n,
":%02lu", utc_offs_mins );
232 n +=
snprintf_safe( &s[n], max_len - n,
":%02lu", utc_offs_secs );
245 struct tm tm = { 0 };
259 n =
snprintf_safe( buf, len,
"%04d-%02d-%02d, %02d:%02d:%02d",
260 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
261 tm.tm_hour, tm.tm_min, tm.tm_sec );
271 const char* fmt,
char *buf,
size_t max_len )
284 char *buf,
size_t len )
289 return __to_iso_frac( tstamp, offs_hours, frac,
".%03lu", buf, len );
297 char *buf,
size_t len )
302 return __to_iso_frac( tstamp, offs_hours, frac,
".%06lu", buf, len );
310 char *buf,
size_t len )
315 return __to_iso_frac( tstamp, offs_hours, frac,
".%09lu", buf, len );
uint32_t fractions
binary fractional part of a second, 0xFFFFFFFF -> 0.9999999... s (resolution 2^-32s =~ 233 ps) ...
Structure that represents a timestamp in NTP Timestamp Format.
static __mbg_inline int __to_iso_frac(time_t tstamp, int offset, time_t frac, const char *fmt, char *buf, size_t max_len)
void check_precise_time_api(void)
int snprint_time_t_to_iso_ms(time_t tstamp, int offs_hours, time_t frac, char *buf, size_t len)
#define _int_from_size_t(_n)
int sn_cpy_str_safe(char *dst, size_t max_len, const char *src)
A function to copy a string safely, returning the number of characters copied.
int snprint_ntp_tstamp_to_iso_us(const NTP_TSTAMP *ts, char *buf, size_t len)
#define bin_frac_32_to_nsec(_bin)
int snprint_ntp_tstamp_to_iso_ms(const NTP_TSTAMP *ts, char *buf, size_t len)
static __mbg_inline time_t cvt_to_time_t(mbg_time_t t)
int mbg_clock_gettime(clockid_t clock_id, struct timespec *tp)
#define bin_frac_32_to_msec(_bin)
int snprint_gmtime_error(char *s, size_t max_len, int mbg_errno, time_t t, const char *calling_fnc)
#define bin_frac_32_to_usec(_bin)
uint32_t seconds
seconds since NTP epoch, see NTP_SEC_BIAS
static __mbg_inline int mbg_gmtime(struct tm *p_tm, const time_t *p_time)
int snprint_utc_offs(char *s, size_t max_len, const char *info, long utc_offs)
Print a UTC offset into a string.
int snprintf_safe(char *s, size_t max_len, const char *fmt,...)
A portable, safe implementation of snprintf()
int snprint_time_t_to_iso(time_t tstamp, int offs_hours, char *buf, size_t len)
#define mbg_rc_is_error(_rc)
const char * mbg_strerror(int mbg_errno)
Return an error string associated with the MBG_ERROR_CODES.
unsigned __int64 uint64_t
int snprint_time_t_to_iso_ns(time_t tstamp, int offs_hours, time_t frac, char *buf, size_t len)
int snprint_time_t_to_iso_us(time_t tstamp, int offs_hours, time_t frac, char *buf, size_t len)
int mbg_clock_settime(clockid_t clock_id, const struct timespec *tp)
#define NTP_SEC_BIAS
NTP epoch bias from ordinary time_t epoch.
int snprint_ntp_tstamp_to_iso_ns(const NTP_TSTAMP *ts, char *buf, size_t len)