winTime.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platformWin32/platformWin32.h"
  24. #include "time.h"
  25. void Platform::sleep(U32 ms)
  26. {
  27. Sleep(ms);
  28. }
  29. //--------------------------------------
  30. void Platform::getLocalTime(LocalTime &lt)
  31. {
  32. struct tm *systime;
  33. time_t long_time;
  34. time( &long_time ); // Get time as long integer.
  35. systime = localtime( &long_time ); // Convert to local time.
  36. lt.sec = systime->tm_sec;
  37. lt.min = systime->tm_min;
  38. lt.hour = systime->tm_hour;
  39. lt.month = systime->tm_mon;
  40. lt.monthday = systime->tm_mday;
  41. lt.weekday = systime->tm_wday;
  42. lt.year = systime->tm_year;
  43. lt.yearday = systime->tm_yday;
  44. lt.isdst = systime->tm_isdst;
  45. }
  46. String Platform::localTimeToString( const LocalTime &lt )
  47. {
  48. // Converting a LocalTime to SYSTEMTIME
  49. // requires a few annoying adjustments.
  50. SYSTEMTIME st;
  51. st.wMilliseconds = 0;
  52. st.wSecond = lt.sec;
  53. st.wMinute = lt.min;
  54. st.wHour = lt.hour;
  55. st.wDay = lt.monthday;
  56. st.wDayOfWeek = lt.weekday;
  57. st.wMonth = lt.month + 1;
  58. st.wYear = lt.year + 1900;
  59. TCHAR buffer[1024] = {0};
  60. S32 result = 0;
  61. String outStr;
  62. // Note: The 'Ex' version of GetDateFormat and GetTimeFormat are preferred
  63. // and have better support for supplemental locales but are not supported
  64. // for version of windows prior to Vista.
  65. //
  66. // Would be nice if Torque was more aware of the OS version and
  67. // take full advantage of it.
  68. result = GetDateFormat( LOCALE_USER_DEFAULT,
  69. DATE_SHORTDATE,
  70. &st,
  71. NULL,
  72. (LPTSTR)buffer,
  73. 1024 );
  74. // Also would be nice to have a standard system for torque to
  75. // retrieve and display windows level errors using GetLastError and
  76. // FormatMessage...
  77. AssertWarn( result != 0, "Platform::getLocalTime" );
  78. outStr += buffer;
  79. outStr += "\t";
  80. result = GetTimeFormat( LOCALE_USER_DEFAULT,
  81. 0,
  82. &st,
  83. NULL,
  84. (LPTSTR)buffer,
  85. 1024 );
  86. AssertWarn( result != 0, "Platform::localTimeToString, error occured!" );
  87. outStr += buffer;
  88. return outStr;
  89. }
  90. U32 Platform::getTime()
  91. {
  92. time_t long_time;
  93. time( &long_time );
  94. return long_time;
  95. }
  96. void Platform::fileToLocalTime(const FileTime & ft, LocalTime * lt)
  97. {
  98. if(!lt)
  99. return;
  100. dMemset(lt, 0, sizeof(LocalTime));
  101. FILETIME winFileTime;
  102. winFileTime.dwLowDateTime = ft.v1;
  103. winFileTime.dwHighDateTime = ft.v2;
  104. SYSTEMTIME winSystemTime;
  105. // convert the filetime to local time
  106. FILETIME convertedFileTime;
  107. if(::FileTimeToLocalFileTime(&winFileTime, &convertedFileTime))
  108. {
  109. // get the time into system time struct
  110. if(::FileTimeToSystemTime((const FILETIME *)&convertedFileTime, &winSystemTime))
  111. {
  112. SYSTEMTIME * time = &winSystemTime;
  113. // fill it in...
  114. lt->sec = time->wSecond;
  115. lt->min = time->wMinute;
  116. lt->hour = time->wHour;
  117. lt->month = time->wMonth - 1;
  118. lt->monthday = time->wDay;
  119. lt->weekday = time->wDayOfWeek;
  120. lt->year = (time->wYear < 1900) ? 1900 : (time->wYear - 1900);
  121. // not calculated
  122. lt->yearday = 0;
  123. lt->isdst = false;
  124. }
  125. }
  126. }
  127. U32 Platform::getRealMilliseconds()
  128. {
  129. return GetTickCount();
  130. }
  131. U32 Platform::getVirtualMilliseconds()
  132. {
  133. return winState.currentTime;
  134. }
  135. void Platform::advanceTime(U32 delta)
  136. {
  137. winState.currentTime += delta;
  138. }