macTime.mm
Engine/source/platformMac/macTime.mm
Public Variables
Detailed Description
Public Variables
U32 sgCurrentTime
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#import <CoreServices/CoreServices.h> 25#import <mach/mach_time.h> 26#import "platform/platformTimer.h" 27#import <time.h> 28#import <unistd.h> 29 30//-------------------------------------- 31 32static U32 sgCurrentTime = 0; 33 34//-------------------------------------- 35void Platform::getLocalTime(LocalTime <) 36{ 37 struct tm systime; 38 time_t long_time; 39 40 /// Get time as long integer. 41 time( &long_time ); 42 /// Convert to local time, thread safe. 43 localtime_r( &long_time, &systime ); 44 45 /// Fill the return struct 46 lt.sec = systime.tm_sec; 47 lt.min = systime.tm_min; 48 lt.hour = systime.tm_hour; 49 lt.month = systime.tm_mon; 50 lt.monthday = systime.tm_mday; 51 lt.weekday = systime.tm_wday; 52 lt.year = systime.tm_year; 53 lt.yearday = systime.tm_yday; 54 lt.isdst = systime.tm_isdst; 55} 56 57String Platform::localTimeToString( const LocalTime < ) 58{ 59 tm systime; 60 61 systime.tm_sec = lt.sec; 62 systime.tm_min = lt.min; 63 systime.tm_hour = lt.hour; 64 systime.tm_mon = lt.month; 65 systime.tm_mday = lt.monthday; 66 systime.tm_wday = lt.weekday; 67 systime.tm_year = lt.year; 68 systime.tm_yday = lt.yearday; 69 systime.tm_isdst = lt.isdst; 70 71 return asctime( &systime ); 72} 73 74/// Gets the time in seconds since the Epoch 75U32 Platform::getTime() 76{ 77 time_t epoch_time; 78 time( &epoch_time ); 79 return epoch_time; 80} 81 82/// Gets the time in milliseconds since some epoch. In this case, system start time. 83/// Storing milliseconds in a U32 overflows every 49.71 days 84U32 Platform::getRealMilliseconds() 85{ 86 const uint32_t oneMillion = 1000000; 87 static mach_timebase_info_data_t s_timebase_info; 88 89 if (s_timebase_info.denom == 0) { 90 (void) mach_timebase_info(&s_timebase_info); 91 } 92 93 // mach_absolute_time() returns billionth of seconds, 94 // so divide by one million to get milliseconds 95 return (U32)((mach_absolute_time() * s_timebase_info.numer) / (oneMillion * s_timebase_info.denom)); 96} 97 98U32 Platform::getVirtualMilliseconds() 99{ 100 return sgCurrentTime; 101} 102 103void Platform::advanceTime(U32 delta) 104{ 105 sgCurrentTime += delta; 106} 107 108/// Asks the operating system to put the process to sleep for at least ms milliseconds 109void Platform::sleep(U32 ms) 110{ 111 // note: this will overflow if you want to sleep for more than 49 days. just so ye know. 112 usleep( ms * 1000 ); 113} 114 115//---------------------------------------------------------------------------------- 116PlatformTimer* PlatformTimer::create() 117{ 118 return new DefaultPlatformTimer; 119} 120 121void Platform::fileToLocalTime(const FileTime & ft, LocalTime * lt) 122{ 123 if(!lt) 124 return; 125 126 time_t long_time = ft; 127 128 struct tm systime; 129 130 /// Convert to local time, thread safe. 131 localtime_r( &long_time, &systime ); 132 133 /// Fill the return struct 134 lt->sec = systime.tm_sec; 135 lt->min = systime.tm_min; 136 lt->hour = systime.tm_hour; 137 lt->month = systime.tm_mon; 138 lt->monthday = systime.tm_mday; 139 lt->weekday = systime.tm_wday; 140 lt->year = systime.tm_year; 141 lt->yearday = systime.tm_yday; 142 lt->isdst = systime.tm_isdst; 143} 144 145