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