macTime.mm 4.3 KB

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