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