#include "pch.h"
#include "TimeFunctions.h"

#pragma unmanaged

TimeFunctions::TimeFunctions()
{
	
}


void TimeFunctions::EpdTimeToFileTime( INT32 t, LPFILETIME pft, bool isMK2)  //MK3
{
	LONGLONG offset ;
	if (isMK2) offset = 116444736000000000; else offset = 125911584000000000;;
	LONGLONG ll;
	ll = Int32x32To64(t, 10000000) + offset;  //1601-1-1 -> 2000
	pft->dwLowDateTime = (DWORD)ll;
	pft->dwHighDateTime = ll >> 32;
}

void TimeFunctions::FileTimeToEpdTime( FILETIME pft, INT32* t, bool isMK2)
{
	LONGLONG offset;
	if (isMK2) offset = 116444736000000000; else offset = 125911584000000000;
	LONGLONG ll = ((((LONGLONG)pft.dwHighDateTime << 32) + pft.dwLowDateTime) - offset) / 10000000;
	*t = (DWORD)ll;
}

INT32 TimeFunctions::NowToEpdTime(bool isMK2)
{
	LONGLONG offset;
	if (isMK2) offset = 116444736000000000; else offset = 125911584000000000;
	SYSTEMTIME t;
	GetSystemTime(&t);
	FILETIME ft;
	SystemTimeToFileTime((const SYSTEMTIME *)&t, &ft);
	LONGLONG ll = ((((LONGLONG)ft.dwHighDateTime << 32U) + ft.dwLowDateTime) - offset) / 10000000;
	return (DWORD)ll;
}

void TimeFunctions::UnixTimeToFileTime(time_t t, LPFILETIME pft)
{
	// Note that LONGLONG is a 64-bit value
	LONGLONG ll;
	ll = Int32x32To64(t, 10000000) + 116444736000000000; //1601-1-1 1970-1-1  
	pft->dwLowDateTime = (DWORD)ll;
	pft->dwHighDateTime = ll >> 32;
}

 void TimeFunctions::UnixTimeToSystemTime(time_t t, LPSYSTEMTIME pst)
{
	FILETIME ft;
	UnixTimeToFileTime(t, &ft);
	FileTimeToSystemTime(&ft, pst);
}