Rework RtlTime with portable time conversion functions

This commit is contained in:
sephiroth99 2016-01-20 11:41:33 -05:00
parent bf910c2d9a
commit a74c88851b
1 changed files with 27 additions and 28 deletions

View File

@ -25,8 +25,10 @@
#include "xenia/kernel/xevent.h" #include "xenia/kernel/xevent.h"
#include "xenia/kernel/xthread.h" #include "xenia/kernel/xthread.h"
// For FileTimeToSystemTime and SystemTimeToFileTime: #if XE_PLATFORM_WIN32
#include "xenia/base/platform_win.h" #include "xenia/base/platform_win.h"
#define timegm _mkgmtime
#endif
namespace xe { namespace xe {
namespace kernel { namespace kernel {
@ -439,44 +441,41 @@ struct X_TIME_FIELDS {
}; };
static_assert(sizeof(X_TIME_FIELDS) == 16, "Must be LARGEINTEGER"); static_assert(sizeof(X_TIME_FIELDS) == 16, "Must be LARGEINTEGER");
// https://support.microsoft.com/en-us/kb/167296
void RtlTimeToTimeFields(lpqword_t time_ptr, void RtlTimeToTimeFields(lpqword_t time_ptr,
pointer_t<X_TIME_FIELDS> time_fields_ptr) { pointer_t<X_TIME_FIELDS> time_fields_ptr) {
// TODO(benvanik): replace with our own version. int64_t time_ms = time_ptr.value() / 10000 - 11644473600000LL;
FILETIME ft; time_t timet = time_ms / 1000;
ft.dwHighDateTime = static_cast<uint32_t>(time_ptr.value() >> 32); struct tm* tm = gmtime(&timet);
ft.dwLowDateTime = static_cast<uint32_t>(time_ptr.value());
SYSTEMTIME st;
FileTimeToSystemTime(&ft, &st);
time_fields_ptr->year = st.wYear; time_fields_ptr->year = tm->tm_year + 1900;
time_fields_ptr->month = st.wMonth; time_fields_ptr->month = tm->tm_mon + 1;
time_fields_ptr->day = st.wDay; time_fields_ptr->day = tm->tm_mday;
time_fields_ptr->hour = st.wHour; time_fields_ptr->hour = tm->tm_hour;
time_fields_ptr->minute = st.wMinute; time_fields_ptr->minute = tm->tm_min;
time_fields_ptr->second = st.wSecond; time_fields_ptr->second = tm->tm_sec;
time_fields_ptr->milliseconds = st.wMilliseconds; time_fields_ptr->milliseconds = time_ms % 1000;
time_fields_ptr->weekday = st.wDayOfWeek; time_fields_ptr->weekday = tm->tm_wday;
} }
DECLARE_XBOXKRNL_EXPORT(RtlTimeToTimeFields, ExportTag::kImplemented); DECLARE_XBOXKRNL_EXPORT(RtlTimeToTimeFields, ExportTag::kImplemented);
dword_result_t RtlTimeFieldsToTime(pointer_t<X_TIME_FIELDS> time_fields_ptr, dword_result_t RtlTimeFieldsToTime(pointer_t<X_TIME_FIELDS> time_fields_ptr,
lpqword_t time_ptr) { lpqword_t time_ptr) {
// TODO(benvanik): replace with our own version. struct tm tm;
SYSTEMTIME st; tm.tm_year = time_fields_ptr->year - 1900;
st.wYear = time_fields_ptr->year; tm.tm_mon = time_fields_ptr->month - 1;
st.wMonth = time_fields_ptr->month; tm.tm_mday = time_fields_ptr->day;
st.wDay = time_fields_ptr->day; tm.tm_hour = time_fields_ptr->hour;
st.wHour = time_fields_ptr->hour; tm.tm_min = time_fields_ptr->minute;
st.wMinute = time_fields_ptr->minute; tm.tm_sec = time_fields_ptr->second;
st.wSecond = time_fields_ptr->second; tm.tm_isdst = 0;
st.wMilliseconds = time_fields_ptr->milliseconds; time_t timet = timegm(&tm);
st.wDayOfWeek = time_fields_ptr->weekday; if (timet == -1) {
FILETIME ft;
if (!SystemTimeToFileTime(&st, &ft)) {
// set last error = ERROR_INVALID_PARAMETER // set last error = ERROR_INVALID_PARAMETER
return 0; return 0;
} }
uint64_t time = (uint64_t(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; uint64_t time =
((timet + 11644473600LL) * 1000 + time_fields_ptr->milliseconds) * 10000;
*time_ptr = time; *time_ptr = time;
return 1; return 1;
} }