Time.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_WINDOWS
  3. #include "os/Time.h"
  4. #include "os/Win32/WindowsHeaders.h"
  5. #include "utils/MathUtils.h"
  6. #define MTICKS_PER_SEC 10000000LL
  7. namespace il2cpp
  8. {
  9. namespace os
  10. {
  11. static LARGE_INTEGER s_PerformanceCounterFrequency;
  12. static inline void InitializePerformanceCounterFrequency()
  13. {
  14. if (!s_PerformanceCounterFrequency.QuadPart)
  15. {
  16. // From MSDN: On systems that run Windows XP or later, the function will always succeed and will thus never return zero.
  17. // so I'll just assume we never run on older than XP
  18. BOOL qpfResult = QueryPerformanceFrequency(&s_PerformanceCounterFrequency);
  19. IL2CPP_ASSERT(qpfResult != FALSE);
  20. }
  21. }
  22. uint32_t Time::GetTicksMillisecondsMonotonic()
  23. {
  24. InitializePerformanceCounterFrequency();
  25. LARGE_INTEGER value;
  26. QueryPerformanceCounter(&value);
  27. return static_cast<uint32_t>(value.QuadPart * 1000 / s_PerformanceCounterFrequency.QuadPart);
  28. }
  29. int64_t Time::GetTicks100NanosecondsMonotonic()
  30. {
  31. InitializePerformanceCounterFrequency();
  32. LARGE_INTEGER value;
  33. QueryPerformanceCounter(&value);
  34. return utils::MathUtils::A_Times_B_DividedBy_C(value.QuadPart, MTICKS_PER_SEC, s_PerformanceCounterFrequency.QuadPart);
  35. }
  36. /*
  37. * Magic number to convert FILETIME base Jan 1, 1601 to DateTime - base Jan, 1, 0001
  38. */
  39. const uint64_t FILETIME_ADJUST = ((uint64_t)504911232000000000LL);
  40. int64_t Time::GetTicks100NanosecondsDateTime()
  41. {
  42. ULARGE_INTEGER ft;
  43. IL2CPP_ASSERT(sizeof(ft) == sizeof(FILETIME));
  44. ::GetSystemTimeAsFileTime((FILETIME*)&ft);
  45. return FILETIME_ADJUST + ft.QuadPart;
  46. }
  47. int64_t Time::GetSystemTimeAsFileTime()
  48. {
  49. int64_t fileTime;
  50. ::GetSystemTimeAsFileTime(reinterpret_cast<FILETIME*>(&fileTime));
  51. return fileTime;
  52. }
  53. }
  54. }
  55. #endif