POSIXTime.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef __APPLE__
  23. #include "platformPOSIX/platformPOSIX.h"
  24. #include "platform/platformTimer.h"
  25. #include "time.h"
  26. #include <errno.h>
  27. #include <time.h>
  28. #include <sys/time.h>
  29. #include <sys/resource.h>
  30. #include <unistd.h>
  31. static U32 sgCurrentTime = 0;
  32. U32 x86UNIXGetTickCount();
  33. //--------------------------------------
  34. void Platform::getLocalTime(LocalTime &lt)
  35. {
  36. struct tm *systime;
  37. time_t long_time;
  38. time( &long_time ); // Get time as long integer.
  39. systime = localtime( &long_time ); // Convert to local time.
  40. lt.sec = systime->tm_sec;
  41. lt.min = systime->tm_min;
  42. lt.hour = systime->tm_hour;
  43. lt.month = systime->tm_mon;
  44. lt.monthday = systime->tm_mday;
  45. lt.weekday = systime->tm_wday;
  46. lt.year = systime->tm_year;
  47. lt.yearday = systime->tm_yday;
  48. lt.isdst = systime->tm_isdst;
  49. }
  50. String Platform::localTimeToString( const LocalTime &lt )
  51. {
  52. tm systime;
  53. systime.tm_sec = lt.sec;
  54. systime.tm_min = lt.min;
  55. systime.tm_hour = lt.hour;
  56. systime.tm_mon = lt.month;
  57. systime.tm_mday = lt.monthday;
  58. systime.tm_wday = lt.weekday;
  59. systime.tm_year = lt.year;
  60. systime.tm_yday = lt.yearday;
  61. systime.tm_isdst = lt.isdst;
  62. return asctime( &systime );
  63. }
  64. U32 Platform::getTime()
  65. {
  66. time_t long_time;
  67. time( &long_time );
  68. return long_time;
  69. }
  70. U32 Platform::getRealMilliseconds()
  71. {
  72. // struct rusage usageStats;
  73. // getrusage(RUSAGE_SELF, &usageStats);
  74. // return usageStats.ru_utime.tv_usec;
  75. return x86UNIXGetTickCount();
  76. }
  77. U32 Platform::getVirtualMilliseconds()
  78. {
  79. return sgCurrentTime;
  80. }
  81. void Platform::advanceTime(U32 delta)
  82. {
  83. sgCurrentTime += delta;
  84. }
  85. void Platform::fileToLocalTime(const FileTime & ft, LocalTime * lt)
  86. {
  87. if(!lt)
  88. return;
  89. time_t long_time = ft;
  90. struct tm systime;
  91. /// Convert to local time, thread safe.
  92. localtime_r( &long_time, &systime );
  93. /// Fill the return struct
  94. lt->sec = systime.tm_sec;
  95. lt->min = systime.tm_min;
  96. lt->hour = systime.tm_hour;
  97. lt->month = systime.tm_mon;
  98. lt->monthday = systime.tm_mday;
  99. lt->weekday = systime.tm_wday;
  100. lt->year = systime.tm_year;
  101. lt->yearday = systime.tm_yday;
  102. lt->isdst = systime.tm_isdst;
  103. }
  104. PlatformTimer *PlatformTimer::create()
  105. {
  106. return new DefaultPlatformTimer();
  107. }
  108. //------------------------------------------------------------------------------
  109. //-------------------------------------- x86UNIX Implementation
  110. //
  111. //
  112. static bool sg_initialized = false;
  113. static U32 sg_secsOffset = 0;
  114. //--------------------------------------
  115. U32 x86UNIXGetTickCount()
  116. {
  117. // TODO: What happens when crossing a day boundary?
  118. //
  119. timeval t;
  120. if (sg_initialized == false) {
  121. sg_initialized = true;
  122. gettimeofday(&t, NULL);
  123. sg_secsOffset = t.tv_sec;
  124. }
  125. gettimeofday(&t, NULL);
  126. U32 secs = t.tv_sec - sg_secsOffset;
  127. U32 uSecs = t.tv_usec;
  128. // Make granularity 1 ms
  129. return (secs * 1000) + (uSecs / 1000);
  130. }
  131. void Platform::sleep(U32 ms)
  132. {
  133. // note: this will overflow if you want to sleep for more than 49 days. just so ye know.
  134. usleep( ms * 1000 );
  135. }
  136. #endif