mirror of
https://github.com/4jcraft/4jcraft.git
synced 2026-04-27 05:03:36 +00:00
Merge pull request #2 from Voxxin/refactor/tropical-dev
feat: add stubs for Windows system time functions
This commit is contained in:
commit
fc69975d90
|
|
@ -20,6 +20,7 @@
|
|||
#include <fnmatch.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#define TRUE true
|
||||
#define FALSE false
|
||||
|
|
@ -176,6 +177,18 @@ typedef struct _LINUXSTUBS_FIND_HANDLE {
|
|||
char pattern[MAX_PATH];
|
||||
} _LINUXSTUBS_FIND_HANDLE;
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime
|
||||
typedef struct _SYSTEMTIME {
|
||||
WORD wYear;
|
||||
WORD wMonth;
|
||||
WORD wDayOfWeek;
|
||||
WORD wDay;
|
||||
WORD wHour;
|
||||
WORD wMinute;
|
||||
WORD wSecond;
|
||||
WORD wMilliseconds;
|
||||
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
|
||||
|
||||
#define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
|
||||
|
||||
typedef pthread_mutex_t RTL_CRITICAL_SECTION;
|
||||
|
|
@ -536,6 +549,88 @@ static inline BOOL FindClose(HANDLE hFindFile)
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
// internal helper: convert FILETIME (100ns since 1601) to time_t (seconds since 1970)
|
||||
static inline time_t _FileTimeToTimeT(const FILETIME& ft)
|
||||
{
|
||||
ULONGLONG val = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
|
||||
const ULONGLONG EPOCH_DIFF = 116444736000000000ULL; // 100ns intervals between 1601-01-01 and 1970-01-01
|
||||
return (time_t)((val - EPOCH_DIFF) / 10000000ULL);
|
||||
}
|
||||
|
||||
// internal helper: read the current wall clock into a timespec
|
||||
static inline void _CurrentTimeSpec(struct timespec *ts)
|
||||
{
|
||||
#ifdef CLOCK_REALTIME
|
||||
clock_gettime(CLOCK_REALTIME, ts);
|
||||
#else
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
ts->tv_sec = tv.tv_sec;
|
||||
ts->tv_nsec = tv.tv_usec * 1000;
|
||||
#endif
|
||||
}
|
||||
|
||||
// internal helper: fill SYSTEMTIME from a broken-down tm + nanosecond remainder
|
||||
static inline void _FillSystemTime(const struct tm *tm, long tv_nsec, LPSYSTEMTIME lpSystemTime)
|
||||
{
|
||||
lpSystemTime->wYear = tm->tm_year + 1900;
|
||||
lpSystemTime->wMonth = tm->tm_mon + 1;
|
||||
lpSystemTime->wDayOfWeek = tm->tm_wday; // 0 = Sunday
|
||||
lpSystemTime->wDay = tm->tm_mday;
|
||||
lpSystemTime->wHour = tm->tm_hour;
|
||||
lpSystemTime->wMinute = tm->tm_min;
|
||||
lpSystemTime->wSecond = tm->tm_sec;
|
||||
lpSystemTime->wMilliseconds = (WORD)(tv_nsec / 1000000); // ns to ms
|
||||
}
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtime
|
||||
static inline VOID GetSystemTime(LPSYSTEMTIME lpSystemTime)
|
||||
{
|
||||
struct timespec ts; _CurrentTimeSpec(&ts);
|
||||
struct tm tm; gmtime_r(&ts.tv_sec, &tm); // UTC
|
||||
_FillSystemTime(&tm, ts.tv_nsec, lpSystemTime);
|
||||
}
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlocaltime
|
||||
static inline VOID GetLocalTime(LPSYSTEMTIME lpSystemTime)
|
||||
{
|
||||
struct timespec ts; _CurrentTimeSpec(&ts);
|
||||
struct tm tm; localtime_r(&ts.tv_sec, &tm); // local time
|
||||
_FillSystemTime(&tm, ts.tv_nsec, lpSystemTime);
|
||||
}
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-systemtimetofiletime
|
||||
static inline BOOL SystemTimeToFileTime(const SYSTEMTIME *lpSystemTime, LPFILETIME lpFileTime)
|
||||
{
|
||||
struct tm tm = {};
|
||||
tm.tm_year = lpSystemTime->wYear - 1900;
|
||||
tm.tm_mon = lpSystemTime->wMonth - 1;
|
||||
tm.tm_mday = lpSystemTime->wDay;
|
||||
tm.tm_hour = lpSystemTime->wHour;
|
||||
tm.tm_min = lpSystemTime->wMinute;
|
||||
tm.tm_sec = lpSystemTime->wSecond;
|
||||
|
||||
time_t t = timegm(&tm);
|
||||
if (t == (time_t)-1) return FALSE;
|
||||
|
||||
ULONGLONG ft = ((ULONGLONG)t + 11644473600ULL) * 10000000ULL;
|
||||
ft += lpSystemTime->wMilliseconds * 10000ULL;
|
||||
lpFileTime->dwLowDateTime = (DWORD)(ft & 0xFFFFFFFF);
|
||||
lpFileTime->dwHighDateTime = (DWORD)(ft >> 32);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-filetimetosystemtime
|
||||
static inline BOOL FileTimeToSystemTime(const FILETIME *lpFileTime, LPSYSTEMTIME lpSystemTime)
|
||||
{
|
||||
ULONGLONG ft = ((ULONGLONG)lpFileTime->dwHighDateTime << 32) | lpFileTime->dwLowDateTime;
|
||||
time_t t = _FileTimeToTimeT(*lpFileTime);
|
||||
long remainder_ns = (long)((ft % 10000000ULL) * 100);
|
||||
|
||||
struct tm tm; gmtime_r(&t, &tm); // UTC
|
||||
_FillSystemTime(&tm, remainder_ns, lpSystemTime);
|
||||
return TRUE;
|
||||
}
|
||||
static inline DWORD GetTickCount()
|
||||
{
|
||||
struct timespec ts;
|
||||
|
|
@ -564,21 +659,23 @@ static inline BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
|
|||
return true;
|
||||
}
|
||||
|
||||
#ifndef _FINAL_BUILD
|
||||
VOID OutputDebugStringW(LPCWSTR lpOutputString)
|
||||
{
|
||||
fwprintf(stderr, lpOutputString);
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-outputdebugstringa
|
||||
static inline VOID OutputDebugStringA(LPCSTR lpOutputString)
|
||||
{
|
||||
if (!lpOutputString) return;
|
||||
fputs(lpOutputString, stderr);
|
||||
}
|
||||
|
||||
VOID OutputDebugString(LPCSTR lpOutputString)
|
||||
{
|
||||
fprintf(stderr, lpOutputString);
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-outputdebugstringw
|
||||
static inline VOID OutputDebugStringW(LPCWSTR lpOutputString)
|
||||
{
|
||||
if (!lpOutputString) return;
|
||||
fprintf(stderr, "%ls", lpOutputString);
|
||||
}
|
||||
|
||||
VOID OutputDebugStringA(LPCSTR lpOutputString)
|
||||
{
|
||||
fprintf(stderr, lpOutputString);
|
||||
static inline VOID OutputDebugString(LPCSTR lpOutputString)
|
||||
{
|
||||
return OutputDebugStringA(lpOutputString);
|
||||
}
|
||||
#endif // _CONTENT_PACKAGE
|
||||
|
||||
#endif // LINUXSTUBS_H
|
||||
Loading…
Reference in a new issue